Spring Cloud Ribbon Client Load Balancing

Ribbon client components provide a complete set of configuration options, such as connection timeout, retry, retry algorithm, etc. Built-in pluggable, customizable load balancing components.Here are some load balancing strategies used:

  • Simple Polling Load Balancing
  • Weighted Polling Load Balancing
  • Zone-aware polling load balancing
  • Random Load Balancing

Write a class to simulate an IP list first:

public class IpMap
{
    // List of Ips to Route, Key for Ip, Value for weight of Ip
    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 4
        serverWeightMap.put("192.168.1.102", 4);
        serverWeightMap.put("192.168.1.103", 1);
        serverWeightMap.put("192.168.1.104", 1);
        // Weight 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:

Zone-aware load balancers have built-in circuit tripping logic that can be configured to select a target service instance based on a Zone Affinity (that is, more likely to choose the hosted area in which the service making the call is located, thereby reducing latency and saving costs).It monitors the 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 request per server in a region has reached the configured threshold, that region will be excluded from the active server list.If more than one region has reached the threshold, the region with the most active requests per server on average will be excluded.
2. When the worst areas are excluded, one will be selected from the remaining areas according to the probability sampling method of the number of server instances.
3. From the selected area, a server will be returned according to the given load balancing policy rules.

Simple polling algorithm

Requests are distributed sequentially and in turn to the back-end server, which treats each server in the back-end equally, regardless of the actual number of connections to the server and the current system load.The code implementation is roughly as follows:

public class RoundRobin
{
    private static Integer pos = 0;
    
    public static String getServer()
    {
        // Rebuild a Map to avoid concurrency problems caused by the server going offline
        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 absolute balance of request transfer.
  • Disadvantages: In order to achieve an absolute balance of request transfers, a considerable cost must be incurred, as a heavy pessimistic lock synchronized is introduced to ensure mutually exclusive pos variable modifications, which will result in a significant drop in concurrent throughput for this polling code segment.

Weighted Round Robin

Different back-end servers may have different machine configurations and load on the current system, so they may not have the same resistance to compression.Configure higher weights for machines with high and low loads to handle more requests; while machines with low and high loads are assigned lower weights and lower system loads, weighted polling handles this problem well and assigns the requests in the order of 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 concurrency problems caused by the server going offline
        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, one of the servers is randomly selected for access according to the list size value of the back-end servers.From the probability statistics theory, it can be seen that as the number of client calls to the server increases, the actual effect is closer to the average allocation of calls to each server in the back end, that is, the result of polling.The general code is as follows:

public class Random
{
    public static String getServer()
    {
        // Rebuild a Map to avoid concurrency problems caused by the server going offline
        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);
    }
}

End-of-Text Benefits

Java material full link: https://pan.baidu.com/s/1pUCCPstPnlGDCljtBVUsXQ Password: b2xc
More information: Selected Ali Java, Architecture, Micro Services, etc. in 2020, plus VInformation about: qwerdd111

Reprint, please keep the original address, thank you~

Tags: Java

Posted on Wed, 03 Jun 2020 14:11:59 -0400 by etsauer