Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the load balancing strategy in Spring Cloud?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "what is the load balancing strategy in Spring Cloud". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

IRule

This is the parent interface of all load balancing policies, and the core method in it is the choose method, which is used to select a service instance.

AbstractLoadBalancerRule

AbstractLoadBalancerRule is an abstract class, which mainly defines an ILoadBalancer, which is the load balancer we mentioned above. We have already talked about the function of the load balancer in detail, so we will not repeat it here. The purpose of defining it here is to assist the balancer policy to select the appropriate server instance.

RandomRule

As you can see from the name, this load balancing strategy is to randomly select a service instance. Looking at the source code, we know that a Random object is initialized in the no-parameter construction method of RandomRule, and then the overloaded choose method choose (ILoadBalancer lb, Object key) is called in its rewritten choose method. In this overloaded choose method, the random object is used to generate a random number not greater than the total number of service instances each time. And use this number as a subscript, so get a service instance.

RoundRobinRule

The load balancing strategy of RoundRobinRule is called linear load balancing strategy, which is the default load balancing strategy used in the BaseLoadBalancer load balancer mentioned above. The overall logic of the choose (ILoadBalancer lb, Object key) function of this class is as follows: open a counter count, iterate through the service list in the while loop, and obtain a subscript through the incrementAndGetModulo method before getting the list. this subscript is obtained after adding 1 to the growing number and then modulating the total number of service lists (so the subscript is never out of bounds). Take the subscript and then fetch the service from the service list list. Each loop counter is incremented by 1, and if the service is not fetched 10 times in a row, a warning No available alive servers after 10 tries from load balancer: XXXX is reported.

RetryRule

If you look at the name, you can see that this load balancing strategy has a retry function. First, a subRule is defined in RetryRule, and its implementation class is RoundRobinRule. Then, in the choose (ILoadBalancer lb, Object key) method of RetryRule, the choose rule in RoundRobinRule is used to select a service instance every time. If the selected instance is normal, it is returned. If the selected service instance is null or has expired, retry continuously before the expiration time deadline (the policy for obtaining service when retrying is still the policy defined in RoundRobinRule). If the deadline is exceeded and still not fetched, a null will be returned.

WeightedResponseTimeRule

WeightedResponseTimeRule is a subclass of RoundRobinRule. The function of RoundRobinRule is extended in WeightedResponseTimeRule. In WeightedResponseTimeRule, a weight of each instance is calculated according to the operation of each instance, and then the instance is selected according to the weight, so that a better instance call can be achieved. There is a scheduled task called DynamicServerWeightTask in WeightedResponseTimeRule. By default, the weight of each service instance is calculated every 30 seconds, and the rule for calculating the weight is very simple. If the shorter the aPCge response time of a service, the greater the weight, the greater the probability that the service instance will be selected to execute the task.

ClientConfigEnabledRoundRobinRule

BestAvailableRule

BestAvailableRule inherits from ClientConfigEnabledRoundRobinRule. On the basis of ClientConfigEnabledRoundRobinRule, it mainly adds the function of filtering out invalid service instances according to the state information of service instances saved in loadBalancerStats, and then finds out the service instances with the least concurrent requests to use. However, it is possible that loadBalancerStats is null, and if loadBalancerStats is null, then BestAvailableRule will adopt the service selection policy of its parent class, ClientConfigEnabledRoundRobinRule (linear polling).

PredicateBasedRule

PredicateBasedRule is a subclass of ClientConfigEnabledRoundRobinRule. It first filters out a part of the list of service instances through an internally defined filter, and then selects a service instance from the filtered results by linear polling.

ZoneAvoidanceRule

ZoneAvoidanceRule is an implementation class of PredicateBasedRule, but there is one more filter condition here. The filter condition in ZoneAvoidanceRule is a combined filter condition called CompositePredicate, which is composed of ZoneAvoidancePredicate as the main filter condition and AvailabilityPredicate as the secondary filter condition. After the filter is successful, continue to use linear polling to select one from the filter results.

This is the end of the content of "what is the load balancing strategy in Spring Cloud". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report