Talk about Ribbon getting service list and load balancing strategy

Get into the habit of writing together! This is the 15th day of my participation in the "Nuggets Daily New Plan·April Update Challenge", click to view the details of the event .

Talk about Ribbon getting service list and load balancing strategy

When using the load balancer to select services, it is obtained from all service lists, so where does the service list come from?

The ZoneAwareLoadBalancer load balancer is defined in RibbonClientConfiguration and uses the ServerList object

Let's look at the construction method of the load balancer ZoneAwareLoadBalancer. Its construction method directly calls the construction method of the parent class DynamicServerListLoadBalancer. There is a restOfInit() method in the construction method:

Two important steps:

enableAndInitLearnNewServersFeature();
updateListOfServers();
复制代码

Let's take a look at the enableAndInitLearnNewServersFeature() method of DynamicServerListLoadBalancer

serverListUpdater.start(updateAction);
复制代码

Let's look at the definition of updateAction member traversal:

protected final ServerListUpdater.UpdateAction updateAction = new ServerListUpdater.UpdateAction() {
        @Override
        public void doUpdate() {
            updateListOfServers();
        }
    };
复制代码

The timed thread is defined in start(), and updateAction.doUpdate() is called at regular intervals to read the service list from Eureka or Nacos and save it to the local cache of Ribbon

scheduledFuture = getRefreshExecutor().scheduleWithFixedDelay(
                    wrapperRunnable,
                    initialDelayMs,
                    refreshIntervalMs,
                    TimeUnit.MILLISECONDS
            );
复制代码

So the essence is to get the service list through updateListOfServers

After finishing the acquisition of this service list, we are looking at two commonly used load balancers: RoundRobinRule and RandomRule

The RoundRobinRule class overrides the choose() method:

The RoundRobinRule polls the empty service more than 10 times by default, and returns empty

RandomRule random If the selection is empty, it will keep looping until an available service is selected

Random rand;

   public RandomRule() {
       rand = new Random();
   }
   
 
 public Server choose(ILoadBalancer lb, Object key) {
     ....
     int index = rand.nextInt(serverCount);
     server = upList.get(index);
     .....
 }         
复制代码

Use a random number to select an index value, then select a service from the list of services

ZoneAvoidanceRule does not need to be introduced too much. It is the default strategy. It inherits the PredicateBasedRule class, polls, and filters out nodes that do not conform to Zone.

RetryRule retry strategy, if the selection is empty or unavailable, it will keep retrying within 5000ms

AvailabilityFilteringRule is an available filtering policy. It first polls to select one, and then judges whether it times out and whether the number of connections exceeds the limit, and returns after there is no problem.

BestAvailableRule Minimum number of connections strategy, that is to select the service with the smallest number of connections, the number of connections information is saved in LoadBalancerStats

Summarize

This article mainly talks about how Ribbon obtains the service list and what are the load balancing strategies. The essence of obtaining the service list is to obtain the list information from the remote at regular intervals through a timed thread and save it to the ribbon's own cache. The load balancer has Many, the default is ZoneAvoidanceRule, we can choose the appropriate load balancer, or we can customize the load balancer

Guess you like

Origin juejin.im/post/7086665529318080549