7 load balancing strategies in SpringCloud Ribbon

There are usually two ways to implement a load balancer, one is a server-side load balancer, and the other is a client-side load balancer, and our protagonist Ribbon today
belongs to the latter-the client-side load balancer.

The problem with the server-side load balancer is that it provides stronger traffic control rights, but it cannot meet the needs of different consumers who want to use different load balancing strategies, and there are indeed scenarios for using different load balancing strategies, so customers End-to-end load balancing provides this flexibility.

However, client-side load balancing also has its disadvantages. If it is improperly configured, it may cause hotspots for service providers, or they may not get any services at all, so we will learn about the specific rules of these 7 built-in load balancing strategies in this article. .

Ribbon introduction

Ribbon is a very important basic framework in the Spring Cloud technology stack. It provides Spring Cloud with load balancing capabilities. For example, Fegin and OpenFegin are both implemented based on Ribbon. Even the load balancing in Nacos also uses the Ribbon framework.

The strength of the Ribbon framework is that it not only has 7 built-in load balancing strategies, but also supports user-defined load balancing strategies, so its openness and convenience are also the main reasons for its popularity.

The difference between the server-side load balancer and the client-side load balancer is shown in the following figure: The picturerealization principle of the client-side load balancer is to pull the list of available services to the local (client) through the registration center, such as Nacos, and then pass The client load balancer (set load balancing strategy) obtains the specific ip and port of a server, and then requests the service through the Http framework and obtains the result. The execution process is shown in the following figure:picture

load balancing settings

Taking the Ribbon load balancing setting in Nacos as an example, set the following configuration in the configuration file application.yml:

springcloud-nacos-provider: # nacos中的服务id
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #设置负载均衡策略

Because Nacos has built-in Ribbon, there is no need to add Ribbon dependencies in actual project development. We can see this in the Nacos dependency tree, as shown in the figure below: The default load balancing strategy of Ribbon is polling picturemode , we configured the execution results of three service providers as shown in the figure below: pictureThen, we set the Ribbon load balancing strategy to random mode, and the configuration content is as follows:

springcloud-nacos-provider: # nacos中的服务id
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #设置随机负载均衡

Restart the client, and the execution result is shown in the figure below:picture

7 Load Balancing Strategies

1. Polling strategy

​ Polling strategy: RoundRobinRule, which calls service instances in a certain order. For example, there are 3 services in total. Service 1 is called for the first time, Service 2 is called for the second time, Service 3 is called for the third time, and so on. The configuration settings for this policy are as follows:

springcloud-nacos-provider: # nacos中的服务id
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule #设置负载均衡

2. Weight strategy

​ Weight strategy: WeightedResponseTimeRule, which assigns a weight according to the response time of each service provider. The longer the response time, the smaller the weight, and the lower the possibility of being selected. Its implementation principle is to start using the polling strategy and start a timer, collect the average response time of all service providers every once in a while, and then attach a weight to each service provider, the higher the weight is selected The probability is also greater. The configuration settings for this policy are as follows:

springcloud-nacos-provider: # nacos中的服务id
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule

3. Random strategy

​ Random strategy: RandomRule, randomly select a service instance from the list of service providers. The configuration settings for this policy are as follows:

springcloud-nacos-provider: # nacos中的服务id
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #设置负载均衡

4. Minimum number of connections strategy

​ Minimum number of connections strategy: BestAvailableRule, also known as the minimum concurrency strategy, traverses the list of service providers and selects a service instance with the smallest number of connections. If there are the same minimum number of connections, the polling strategy will be called for selection. The configuration settings for this policy are as follows:

springcloud-nacos-provider: # nacos中的服务id
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.BestAvailableRule #设置负载均衡

5. Retry strategy

​ Retry strategy: RetryRule, obtain the service according to the polling strategy. If the obtained service instance is null or has expired, it will continue to retry within the specified time to obtain the service. If it exceeds the specified time, it is still not obtained A service instance returns null. The configuration settings for this policy are as follows:

ribbon:
  ConnectTimeout: 2000 # 请求连接的超时时间
  ReadTimeout: 5000 # 请求处理的超时时间
springcloud-nacos-provider: # nacos 中的服务 id
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #设置负载均衡

6. Availability Sensitive Strategies

Available sensitivity policy: AvailabilityFilteringRule, first filter out unhealthy service instances, and then select service instances with a small number of connections. The configuration settings for this policy are as follows:

springcloud-nacos-provider: # nacos中的服务id
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.AvailabilityFilteringRule

7. Regional Sensitive Policy

Zone-sensitive policy: ZoneAvoidanceRule, which selects service instances based on the performance of the zone where the service is located and the availability of the service. In an environment without a zone, this policy is similar to the polling policy.

Summarize

Ribbon is a client-side load balancer, which provides more flexibility than the unified load balancing strategy of a server-side load balancer. Ribbon has 7 built-in load balancing strategies: polling strategy, weight strategy, random strategy, minimum number of connections strategy, retry strategy, availability-sensitive strategy, region-sensitive strategy, and users can implement custom load balancing strategies by inheriting RoundRibbonRule .

Guess you like

Origin blog.csdn.net/2202_75623950/article/details/132414490