[Spring Cloud] Several load balancing strategies in Ribbon

foreword

There are usually two ways to achieve load balancing, one is the server-side load balancer, the other is the 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. However, there are indeed scenarios for using different load balancing strategies, so client-side load balancing provides this flexibility. However, client-side load balancing also has its disadvantages. If it is not properly configured, it may cause hotspots for service providers, or they may not get any services at all. Therefore, in this article, we will learn about the specific rules of these 7 built-in load balancing strategies.

1. 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 server-side load balancer and client-side load balancer is shown in the figure below:

insert image description here

The implementation principle of the client load balancer is to pull the available service list to the local (client) through the registration center, such as Nacos, and then obtain the specific ip and port of a server through the client load balancer (set load balancing strategy), and then request the service through the Http framework and get the result. The execution process is shown in the following figure:
insert image description here

2. 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, which we can see in the dependency tree of Nacos, as shown in the following figure:

insert image description here

The default load balancing strategy of Ribbon is polling mode. We configure the execution results of three service providers as shown in the following figure:
insert image description here

Then, we set the Ribbon load balancing policy to random mode, 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:
insert image description here

Three, 7 kinds of load balancing strategies

3.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 #设置负载均衡

3.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, the greater the probability of being selected.
The configuration settings for this policy are as follows:

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

3.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 #设置负载均衡

3.4. Minimum connection strategy

Minimum number of connections strategy: BestAvailableRule, also known as 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 #设置负载均衡

3.5. Retry strategy

Retry strategy: RetryRule, according to the polling strategy to obtain the service, if the obtained service instance is null or has expired, it will continue to retry to obtain the service within the specified time, if the service instance is still not obtained after the specified time, return 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 #设置负载均衡

3.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

3.7. Region 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.
The configuration settings for this policy are as follows:

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

Guess you like

Origin blog.csdn.net/u011397981/article/details/131895226