1. 程式人生 > >輪詢與加權輪詢

輪詢與加權輪詢

這裡是負載均衡的常見演算法。

1 輪詢演算法(Round-Robin)

        輪詢演算法是最簡單的一種負載均衡演算法。它的原理是把來自使用者的請求輪流分配給內部的伺服器:從伺服器1開始,直到伺服器N,然後重新開始迴圈。

程式碼:

// 輪詢  
    public Server round() {  
        currentIndex = (currentIndex + 1) % totalServer;  
        return servers.get(currentIndex);  
    }  
      

其中currentindex是當前位置,totalserver是所有伺服器節點數量。

優點:簡單

缺點:各節點處理能力不同。

2. 加權輪詢演算法(WeightedRound-Robin)

        輪詢演算法並沒有考慮每臺伺服器的處理能力,實際中可能並不是這種情況。由於每臺伺服器的配置、安裝的業務應用等不同,其處理能力會不一樣。所以,加權輪詢演算法的原理就是:根據伺服器的不同處理能力,給每個伺服器分配不同的權值,使其能夠接受相應權值數的服務請求。

   這裡又有兩種,一種是普通加權,類似於一個數組,按權重大小排序。優先獲取大權重的節點。

缺點:是不平滑。


還有一種平滑加權輪詢實現,除依賴權重weight外,還依賴當前權重current_weight。是個可變數值。

思路:初始化時,獲取節點的大小,及節點全部權重的最大公約數gcd,最大權重數。

 遍歷列表,  獲取當前權重,如果current_weight-權重最大公約數gcd<=0.則當前權重為最大權重。

  判斷當前節點的權重》=當前權重,則返回對應節點,否則遍歷下一個節點。

因為是迴圈遍歷:每次從max_weight步進遞減去gcd.獲取出節點權重大於這個的節點。所以是平滑的,分佈相對均勻的。

程式碼:server類 程式碼來自:http://m635674608.iteye.com/blog/2396303

package com.daojia.math;

public class Server {
	private String ip;  
    private int weight;  
      
    public Server(String ip) {  
        super();  
        this.ip = ip;  
    }  
  
    public Server(String ip, int weight) {  
        this.ip     = ip;  
        this.weight = weight;  
    }  
      
    public String getIp() {  
        return ip;  
    }  
    public void setIp(String ip) {  
        this.ip = ip;  
    }  
    public int getWeight() {  
        return weight;  
    }  
    public void setWeight(int weight) {  
        this.weight = weight;  
    }  
  
    @Override  
    public String toString() {  
        return "Server [ip=" + ip + ", weight=" + weight + "]";  
    }
}

主程式碼:

package com.daojia.math;

import java.math.BigInteger;  
import java.util.ArrayList;  
import java.util.List;  
import java.util.concurrent.BrokenBarrierException;  
import java.util.concurrent.CyclicBarrier;  
  
public class WeightRoundRobin {  
    private List<Server> servers;  
  
    private int currentIndex;  
    private int totalServer;  
    private int currentWeight;  
    private int maxWeight;  
    private int gcdWeight;  
  
    public WeightRoundRobin() {  
        servers = new ArrayList<>();  
        servers.add(new Server("192.168.1.2", 5));  
        servers.add(new Server("192.168.1.3", 10));  
        servers.add(new Server("192.168.1.4", 15));  
        servers.add(new Server("192.168.1.5", 100));  
        servers.add(new Server("192.168.1.6", 5));  
        servers.add(new Server("192.168.1.7", 20));  
        servers.add(new Server("192.168.1.8", 30));  
        totalServer = servers.size();  
        currentIndex = totalServer - 1;  
        maxWeight = maxWeight();  
        gcdWeight = serverGcd();  
    }  
    
    /**
     * 遍歷全部節點,從最大的權重開始取。
     * 每次權重-公約數遞減。
     * 如果當前權重《=0.則從迴圈從最大權重開始。
     * @return
     */
    public Server round() {  
        while (true) {  
            currentIndex = (currentIndex + 1) % totalServer;  
            if (currentIndex == 0) {  
                currentWeight = currentWeight - gcdWeight;  
                if (currentWeight <= 0) {  
                    currentWeight = maxWeight;  
                    if(currentWeight == 0) {  
                        return null;  
                    }  
                }  
            }  
              
            if(servers.get(currentIndex).getWeight() >= currentWeight) {  
                return servers.get(currentIndex);  
            }  
        }  
    }  
  
    /** 
     * 返回所有伺服器的權重的最大公約數 
     *  
     * @return 
     */  
    private int serverGcd() {  
        int comDivisor = 0;  
        for (int i = 0; i < totalServer - 1; i++) {  
            if (comDivisor == 0) {  
                comDivisor = gcd(servers.get(i).getWeight(), servers.get(i + 1).getWeight());  
            } else {  
                comDivisor = gcd(comDivisor, servers.get(i + 1).getWeight());  
            }  
        }  
        return comDivisor;  
    }  
  
    /** 
     * 獲得伺服器中的最大權重 
     *  
     * @return 
     */  
    private int maxWeight() {  
        int max = servers.get(0).getWeight();  
        int tmp;  
        for (int i = 1; i < totalServer; i++) {  
            tmp = servers.get(i).getWeight();  
            if (max < tmp) {  
                max = tmp;  
            }  
        }  
        return max;  
    }  
  
    /** 
     * 求兩個數的最大公約數 4和6最大公約數是2 
     *  
     * @param num1 
     * @param num2 
     * @return 
     */  
    private int gcd(int num1, int num2) {  
        BigInteger i1 = new BigInteger(String.valueOf(num1));  
        BigInteger i2 = new BigInteger(String.valueOf(num2));  
        return i1.gcd(i2).intValue();  
    }  
  
    public static void main(String[] args) {  
        final WeightRoundRobin wr = new WeightRoundRobin();  
        // 非併發情況  
        for (int i = 0; i < 100; i++) {  
            System.out.println(wr.round());  
        }  
          
        System.out.println();  
        System.out.println("==========");  
        System.out.println();  
          
        final CyclicBarrier b = new CyclicBarrier(30);  
        // 併發情況  
        for (int i = 0; i < 30; i++) {  
            new Thread(new Runnable() {  
                @Override  
                public void run() {  
                    try {  
                        b.await();  
                        System.out.println(Thread.currentThread().getName() + " " + wr.round());  
                    } catch (InterruptedException | BrokenBarrierException e) {  
                        e.printStackTrace();  
                    }  
                }  
            }, "thread" + i).start();  
        }  
    }  
  
}

執行結果:

Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.7, weight=20]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.4, weight=15]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.7, weight=20]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.3, weight=10]
Server [ip=192.168.1.4, weight=15]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.7, weight=20]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.2, weight=5]
Server [ip=192.168.1.3, weight=10]
Server [ip=192.168.1.4, weight=15]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.6, weight=5]
Server [ip=192.168.1.7, weight=20]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.7, weight=20]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.4, weight=15]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.7, weight=20]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.3, weight=10]
Server [ip=192.168.1.4, weight=15]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.7, weight=20]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.2, weight=5]
Server [ip=192.168.1.3, weight=10]
Server [ip=192.168.1.4, weight=15]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.6, weight=5]
Server [ip=192.168.1.7, weight=20]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.7, weight=20]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.4, weight=15]
Server [ip=192.168.1.5, weight=100]
Server [ip=192.168.1.7, weight=20]
Server [ip=192.168.1.8, weight=30]
Server [ip=192.168.1.3, weight=10]


==========


thread27 Server [ip=192.168.1.4, weight=15]
thread0 Server [ip=192.168.1.5, weight=100]
thread4 Server [ip=192.168.1.8, weight=30]
thread1 Server [ip=192.168.1.7, weight=20]
thread29 Server [ip=192.168.1.5, weight=100]
thread28 Server [ip=192.168.1.5, weight=100]
thread25 Server [ip=192.168.1.5, weight=100]
thread24 Server [ip=192.168.1.5, weight=100]
thread21 Server [ip=192.168.1.5, weight=100]
thread20 Server [ip=192.168.1.5, weight=100]
thread17 Server [ip=192.168.1.8, weight=30]
thread16 Server [ip=192.168.1.7, weight=20]
thread13 Server [ip=192.168.1.6, weight=5]
thread12 Server [ip=192.168.1.5, weight=100]
thread9 Server [ip=192.168.1.4, weight=15]
thread8 Server [ip=192.168.1.3, weight=10]
thread5 Server [ip=192.168.1.2, weight=5]
thread2 Server [ip=192.168.1.5, weight=100]
thread3 Server [ip=192.168.1.5, weight=100]
thread6 Server [ip=192.168.1.5, weight=100]
thread7 Server [ip=192.168.1.5, weight=100]
thread11 Server [ip=192.168.1.5, weight=100]
thread10 Server [ip=192.168.1.5, weight=100]
thread14 Server [ip=192.168.1.5, weight=100]
thread15 Server [ip=192.168.1.5, weight=100]
thread18 Server [ip=192.168.1.5, weight=100]
thread19 Server [ip=192.168.1.8, weight=30]
thread22 Server [ip=192.168.1.5, weight=100]
thread23 Server [ip=192.168.1.8, weight=30]
thread26 Server [ip=192.168.1.5, weight=100]

參考:http://m635674608.iteye.com/blog/2396303