Spring Cloud Ribbon
1 What is load balancing
When clients access back-end services, we can deploy multiple service levels to improve the throughput of the entire application, but which service should be accessed for a single request is what load balancing needs to do. It distributes the front-end requests evenly among back-end services. In real scenarios, due to different machine performance and network conditions, the distribution of requests can be controlled by configuring weights, which is also what load balancing does
2 Ribbon profile
Spring Cloud Ribbon is a client load balancing tool based on HTTP and TCP, which is implemented by Netflix Ribbon. The encapsulation of Spring Cloud makes it easy to automatically convert service-oriented REST template requests into client-side load-balancing service invocations. Spring Cloud Ribbon is just a tool class framework. Unlike service registries, configuration centers, and API gateways, it does not need to be independently deployed. However, it exists in almost every microservice and infrastructure built by Spring Cloud. The Ribbon is used to call microservices and forward REQUESTS to API gateways, including Feign, a tool based on the Ribbon. Therefore, understanding and using Spring Cloud Ribbon is very important for us to use Spring Cloud to build microservices.
3 Introduction Cases
We need to register at least two more services into the registry to provide load-balancing instances when making service calls
3.1 Added a startup portal
To see what happens we modify the/Hello interface to add a line of console print as follows
@GetMapping("/hello")
public String hello(a){
System.out.println("Get request....");
return "hello!";
}
Copy the code
3.2 Viewing the Instance after running
Two examples indicate that the registration is successful
3.3 Consumers Enable load balancing
3.3.1 Modifying the Startup Class
@SpringBootApplication
public class ServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class, args);
}
@Bean
@LoadBalanced// Enable load balancing
public RestTemplate restTemplate(a){
return newRestTemplate(); }}Copy the code
Code description:
1:Add the @loadBalanced annotation to the beans registered with the RestTemplate to enable load balancing
Copy the code
3.3.2 Added ports in load balancing mode
Add the following interfaces to ConsumerController
@GetMapping("/hello-loadbanlance")
public String hello2(a){
// 1. Build the URL from the service name of the registry
String url = "http://service-provider/hello";
// 2. Send request
return restTemplate.getForObject(url,String.class);
}
Copy the code
Code description:
1:The service-provider in the URL is the name of the service provider registered in the registry. The URL constructed in this way takes effect only when the load balancing enabled by the restTemplate is invoked by the restTemplate.
Copy the code
3.3.3 Start the consumer instance and initiate the call
Make three consecutive requests
http://localhost:8002/consumer/hello-loadbanlance
Copy the code
Looking at the logging of the two service provider instances, you can see that requests are load-balanced between the two instances
Example 1:
Example 2:
4 Load balancing policies
4.1 Load Balancing Policies
Ribbon built in a variety of load balancing strategy, responsible for internal top interface of load balancing are as follows: com.net flix. Loadbalancer. IRule, is implemented as follows:
- Com.net flix. Loadbalancer. RandomRule: random strategy
- Com.net flix. Loadbalancer. RetryRule: retry strategy
- Com.net flix. Loadbalancer. RoundRobinRule: a polling process for load balancing
- Com.net flix. Loadbalancer. WeightedResponseTimeRule: weight strategy, will also have a right of each service, the higher the greater the chance of the called
- Com.net flix. Loadbalancer. BestAvailableRule: best strategy, iterate through all the service instance, filter out the fault as an example, and returns the number of requests the smallest instance to return
- Com.net flix. Loadbalancer. AvailabilityFilteringRule: filtering strategy are available, and filter out the fault and please ask more than threshold of service instance, and from the rest of the polling calls in this instance (the default)
Modify the load balancing policy in the service consumer’s application.yml configuration file in the following format:
{service provider name}. Ribbon. NFLoadBalancerRuleClassName
4.2 Configuring load Balancing Policies
Add the following configuration to the consumer server configuration file
service-provider: The service name of the service provider in the registry
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule # Load balancing policy, here is random
Copy the code
When the service is restarted and invoked for several times, it can be seen that the two providers are invoked for an unbalanced number of times, indicating that the policy configuration takes effect:
Example 1:
Example 2: