How to use Spring Cloud Ribbon client load balancing correctly? Case explanation

The Ribbon client component provides a series of perfect configuration options, such as connection timeout, Retry, retry algorithm, etc., with built-in pluggable and customizable load balancing components. Here are some load balancing strategies used:

  • Simple polling load balancing
  • Weighted polling load balancing
  • Area aware polling load balancing
  • Random load balancing
    Besidesbe careful:The theory of light is not enough. 5 free gifts here JAVA Architecture project practical course and large factory interview question bank, you can get 783802103 for free if you are interested in it. Do not enter without foundation!

Write a class to simulate a IP List:

public class IpMap
{
    // Ip list to be routed, Key for Ip, Value for Ip weight
    public static HashMap<String, Integer> serverWeightMap = 
            new HashMap<String, Integer>();
    
    static
    {
        serverWeightMap.put("192.168.1.100", 1);
        serverWeightMap.put("192.168.1.101", 1);
        // Weight is 4
        serverWeightMap.put("192.168.1.102", 4);
        serverWeightMap.put("192.168.1.103", 1);
        serverWeightMap.put("192.168.1.104", 1);
        // Weight is 3
        serverWeightMap.put("192.168.1.105", 3);
        serverWeightMap.put("192.168.1.106", 1);
        // Weight 2
        serverWeightMap.put("192.168.1.107", 2);
        serverWeightMap.put("192.168.1.108", 1);
        serverWeightMap.put("192.168.1.109", 1);
        serverWeightMap.put("192.168.1.110", 1);
    }
}

Area aware load balancing

When selecting a server, the load balancer takes the following steps:

The built-in circuit trip logic of the area aware load balancer can be configured to select the target service instance based on the area affinity (that is, it is more inclined to select the managed area where the service that makes the call is located, which can reduce the delay and save the cost). It monitors the operation and maintenance behavior of instances running in each region, and can quickly discard an entire region in real time. This helps us to be more resilient in the face of failures across the region.

1. The load balancer checks and calculates the status of all available areas. If the average active requests for each server in a zone have reached the configured threshold, the zone will be excluded from the active servers list. If more than one zone has reached the threshold, the zone with the most active requests per server on average will be excluded.
2. After the worst area is excluded, one area will be selected according to the probability sampling method of the number of server instances from the remaining areas.
3. From the selected area, a server will be returned according to the given load balancing policy rules.

Simple polling algorithm

The requests are allocated to the back-end servers in turn. It treats each server in the back-end in a balanced way, regardless of the actual number of connections and the current system load of the server. The code implementation is roughly as follows:

public class RoundRobin
{
    private static Integer pos = 0;
    
    public static String getServer()
    {
        // Rebuild a Map to avoid the concurrent problems caused by the up and down line of the server
        Map<String, Integer> serverMap = 
                new HashMap<String, Integer>();
        serverMap.putAll(IpMap.serverWeightMap);
        
        // Get Ip address List
        Set<String> keySet = serverMap.keySet();
        ArrayList<String> keyList = new ArrayList<String>();
        keyList.addAll(keySet);
        
        String server = null;
        synchronized (pos)
        {
            if (pos > keySet.size())
                pos = 0;
            server = keyList.get(pos);
            pos ++;
        }
        
        return server;
    }
}
  • Advantage: try to achieve the absolute balance of request transfer.
  • Disadvantages: in order to achieve the absolute balance of request transfer, a considerable cost must be paid, because in order to ensure the mutual exclusion of pos variable modification, it is necessary to introduce a heavy pessimistic lock synchronized, which will lead to a significant drop in the concurrent throughput of the polling code.

Weighted Round Robin

Different back-end servers may not have the same machine configuration and current system load, so their compression resistance is not the same. The machines with high configuration and low load can be configured with higher weight to process more requests; while the machines with low configuration and high load can be allocated with lower weight to reduce the system load. Weighted polling can deal with this problem well and distribute the requests in order and according to the weight to the back end. The code is roughly as follows:

public class WeightRoundRobin
{
    private static Integer pos;
    
    public static String getServer()
    {
        // Rebuild a Map to avoid the concurrent problems caused by the up and down line of the server
        Map<String, Integer> serverMap = 
                new HashMap<String, Integer>();
        serverMap.putAll(IpMap.serverWeightMap);
        
        // Get Ip address List
        Set<String> keySet = serverMap.keySet();
        Iterator<String> iterator = keySet.iterator();
        
        List<String> serverList = new ArrayList<String>();
        while (iterator.hasNext())
        {
            String server = iterator.next();
            int weight = serverMap.get(server);
            for (int i = 0; i < weight; i++)
                serverList.add(server);
        }
        
        String server = null;
        synchronized (pos)
        {
            if (pos > keySet.size())
                pos = 0;
            server = serverList.get(pos);
            pos ++;
        }
        
        return server;
    }
}

Random load balancing

Through the random algorithm of the system, according to the list size value of the back-end server, one of the servers is randomly selected for access. According to the theory of probability and statistics, with the increasing number of client calls to the server, the actual effect is more and more close to the average allocation of calls to each server in the back end, that is, the result of polling. The approximate code is as follows:

public class Random
{
    public static String getServer()
    {
        //Rebuild a Map to avoid the concurrent problems caused by the up and down line of the server
        Map<String, Integer> serverMap = 
                new HashMap<String, Integer>();
        serverMap.putAll(IpMap.serverWeightMap);
        
        //Get Ip address List
        Set<String> keySet = serverMap.keySet();
        ArrayList<String> keyList = new ArrayList<String>();
        keyList.addAll(keySet);
        
        java.util.Random random = new java.util.Random();
        int randomPos = random.nextInt(keyList.size());
        
        return keyList.get(randomPos);
    }
}Is it all? Finally, it should be noted that the theory of light is not enough. In this free gift of 5 JAVA architecture project practical course and large factory interview question bank, interested can get into skirt 783802103, do not enter without foundation!

The text and pictures of this article come from the Internet and my own ideas. They are only for learning and communication. They have no commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for handling

Tags: Java

Posted on Thu, 04 Jun 2020 03:43:48 -0400 by cwls1184