Load balancers can be implemented in two ways: a server load balancer and a client load balancer. The Ribbon is the client load balancer.

The problem with a server-side load balancer is that it provides greater control over traffic but does not meet the needs of different consumers who want to use different load balancing policies, and scenarios do exist where different load balancing policies are used, so client-side load balancers provide this flexibility. However, client load balancing also has its drawbacks. If configured incorrectly, it can lead to hot service providers or no service at all, so let’s look at the specific rules for these seven built-in load balancing strategies in this article.

Ribbon is introduced

The Ribbon is an important framework in the Spring Cloud stack. It provides load balancing capabilities for Spring Cloud. For example, Fegin and OpenFegin are implemented based on the Ribbon. Even load balancing in Nacos uses the Ribbon framework.

The power of the Ribbon framework lies in the fact that it not only has seven built-in load balancing policies, but also supports user-defined load balancing policies. Its openness and convenience are the main reasons for its popularity.

The following figure shows the differences between a server load balancer and a client load balancer:The implementation principle of client load balancer is to pull the list of available services to the local (client) through a registry, such as Nacos. Then the client load balancer (load balancing policy) obtains the specific IP address and port of a server, and then requests services through the Http framework and gets the results. Its execution process is shown in the figure below:

Load Balancing Settings

Take the Ribbon load balancing Settings in Nacos as an example. Set the following Settings in the configuration file application.yml:

springcloud-nacos-provider: Service ID in nacos
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule Set a load balancing policy
Copy the code

Because the Ribbon is already built into Nacos, there is no need to add Ribbon dependencies in actual project development. This can be seen in the dependency tree of Nacos, as shown in the following figure:The Ribbon’s default load balancing policy is polling. The result of configuring three service providers is as follows:We then set the Ribbon load balancing policy to random mode as follows:

springcloud-nacos-provider: Service ID in nacos
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule Set random load balancing
Copy the code

Restart the client, and the execution result is as follows:

Seven load balancing policies

1. Polling policy

Polling strategy: RoundRobinRule, which calls service instances in a certain order. For example, if there are three services, call service 1 the first time, call service 2 the second time, call service 3 the third time, and so on. The configuration Settings for this policy are as follows:

springcloud-nacos-provider: Service ID in nacos
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule Set load balancing
Copy the code

2. Weight strategy

Weighting strategy: WeightedResponseTimeRule assigns a weight based on the response time of each service provider, and the longer the response time, the smaller the weight and the less likely it is to be selected. It works by starting with a polling strategy and starting a timer to collect the average response time of all service providers once every period of time, and then attaching a weight to each service provider. The higher the weight, the higher the probability of being selected. The configuration Settings for this policy are as follows:

springcloud-nacos-provider: Service ID in nacos
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightedResponseTimeRule
Copy the code

3. Random strategy

Random Strategy: RandomRule, which randomly selects a service instance from the list of service providers. The configuration Settings for this policy are as follows:

springcloud-nacos-provider: Service ID in nacos
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule Set load balancing
Copy the code

4. Minimum number of connections policy

Minimum number of connections policy: BestAvailableRule, also known as the minimum number of concurrent connections policy, which iterates through the list of service providers and selects the service instance with the smallest number of connections. If there is the same minimum number of connections, a polling policy is invoked to select them. The configuration Settings for this policy are as follows:

springcloud-nacos-provider: Service ID in nacos
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.BestAvailableRule Set load balancing
Copy the code

5. Retry policy

Retry policy: RetryRule. The system obtains services according to the polling policy. If the obtained service instance is null or invalid, the system tries again to obtain services within a specified period of time. The configuration Settings for this policy are as follows:

ribbon:
  ConnectTimeout: 2000 Timeout for connection requests
  ReadTimeout: 5000 # request processing timeout
springcloud-nacos-provider: Service ID in nacos
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule Set load balancing
Copy the code

6. Usability sensitive policies

Sensitivity available strategies: AvailabilityFilteringRule, filter out the first health service instance, and then select the number of connections smaller service instance. The configuration Settings for this policy are as follows:

springcloud-nacos-provider: Service ID in nacos
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.AvailabilityFilteringRule
Copy the code

7. Regional sensitivity strategy

Zone sensitive Policy: ZoneAvoidanceRule selects service instances based on the performance of the zone in which the service is located and the availability of the service, and this policy is similar to a polling policy in an environment without zones. The configuration Settings for this policy are as follows:

springcloud-nacos-provider: Service ID in nacos
  ribbon:
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.ZoneAvoidanceRule
Copy the code

Program source code

Gitee.com/mydb/spring…

conclusion

The Ribbon is a client-side load balancer that provides more flexibility than the uniform load balancing strategy for server-side load balancers. The Ribbon provides seven built-in load balancing policies: polling policy, weight policy, random policy, minimum number of connections policy, retry policy, availability sensitive policy, and culture sensitive policy. Users can customize load balancing policies through RoundRibbonRule.

Judge right and wrong from yourself, praise to listen to others, gain and loss in the number.

Public account: Java Chinese Community

Java Interview Collection: gitee.com/mydb/interv…