This is the ninth day of my participation in the Gwen Challenge.More article challenges
Environment:
IDEA
JDK18.
Spring Cloud Hoxton.M3
Spring Boot 2.2. 0
Copy the code
Introduction to the Ribbon
Spring Cloud Ribbon is a client load balancing tool based on Http and TCP. It is implemented based on Netflix Ribbon. Feign integrates the Ribbon by default. It is a toolclass framework, not deployed in isolation like a service registry, configuration center, or API gateway, but it exists in almost every microservice infrastructure. The Ribbon is used to call microservices and forward REQUESTS to API gateways. Feign is also a tool based on the Ribbon.
The Ribbon already implements these configuration beans by default:
- IClientConfig ribbonClientConfig: DefaultClientConfigImpl
- IRule ribbonRule: ZoneAvoidanceRule
- IPing ribbonPing: NoOpPing
- ServerList ribbonServerList: ConfigurationBasedServerList
- ServerListFilter
- ribbonServerListFilter: ZonePreferenceServerListFilter
- ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer
2. Create a project
1.File —– New —–Project
2.Spring Initializr —– Next
3. The project is being created
4. Enter Group and Artifact and click Next
5. Select Spring Cloud Routing —– Ribbon and click Next
6. Click Finish
Third, improve the project
1. Introduce supplementary dependencies the Ribbon and Eureka dependencies
<! --eureka-client--><dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency><! --eureka ribbon--><dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency><! --springboot web--><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Copy the code
2. Add startup comments
@EnableEurekaClient
package cn.mcus;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class RibbonserverApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonserverApplication.class, args);
}
Copy the code
3. Add the RestTemplate
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
Copy the code
4. Add the configuration file
server.port=8764Spring. The application. The name = # RibbtonServer eureka. Registration to the service center address client. ServiceUrl. DefaultZone = HTTP://localhost:8761/eureka/Whether # had been registered to the server, eureka. Client. RegisterWithEureka =true# whether obtain registration information from eureka server eureka. Client. FetchRegistry =true# Whether to enable the self-protection mode. The default value istrue. eureka.server.enable-self-preservation=true
Copy the code
5. Write a Controller
@RestController
public class RibbonController {
@Autowired
private RibbonService ribbonService;
@RequestMapping("getRibbon")
public String getRibbon(String name){
String company = ribbonService.getRibbon(name);
returncompany; }}Copy the code
6. Write the Service
public interface RibbonService {
String getRibbon(String name);
}
Copy the code
7. Write ServiceImpl
@Service
public class RibbonServiceImpl implements RibbonService {
@Autowired
private RestTemplate restTemplate;
@Override// The ribbon automatically selects the service instance based on the application name instead of the service address
public String getRibbon(String name) {
String result = restTemplate.getForObject("http://Client-Server1/test? name=" + name, String.class);
return result;
}
Copy the code
Iv. Start the project
1. Start in sequence
EurekaServer ClientServer1 ClientServer2 RibbonServer Four items
2. Enter:http://localhost:8761/The following page appears
The red box indicates that the services are registered with the Eureka service. The ports are 8762,8763,8764
3. Enter the address of the Ribbon service in the browser:
Click refresh again: the port number is 8762,8763,8764
That means when we by calling the restTemplate. GetForObject () method, have done a load balancing, visited different port service instance.
5. Load balancing architecture
1. A service registry. EurekaServer port is 8761ClientServer1, ClientServer2, and RibbonServer instance ports are 8762,8763,8764 respectively.
2. When the RibbonServer calls ClientServer1 interface through the RestTemplate, the Ribbon is used for load balancing, and the service interfaces of ClientServer8762 and 8763 are called in turn.