詳解Nginx輪詢算法底層實(shí)現(xiàn)的方法
輪詢算法簡(jiǎn)介
在工作中很多人都使用到了nginx,對(duì)nginx得配置也是爛熟于心,今天我主要想介紹一下nginx輪詢算法得幾種底層實(shí)現(xiàn)方式。
簡(jiǎn)單輪詢算法
這種算法比較簡(jiǎn)單,舉個(gè)例子就是你有三臺(tái)服務(wù)器
| 第一臺(tái)服務(wù)器 | 192.168.1.1 |
| 第二臺(tái)服務(wù)器 | 192.168.1.2 |
| 第三臺(tái)服務(wù)器 | 192.168.1.3 |
第一個(gè)請(qǐng)求過(guò)來(lái)之后默認(rèn)訪問(wèn)第一臺(tái),第二個(gè)請(qǐng)求過(guò)來(lái)訪問(wèn)第二臺(tái),第三次請(qǐng)求過(guò)來(lái)訪問(wèn)第三臺(tái),第四次請(qǐng)求過(guò)來(lái)訪問(wèn)第一臺(tái),以此類推。以下是我代碼實(shí)現(xiàn)簡(jiǎn)單得算法:
public class SimplePolling {
/**
* key是ip
*/
public static List <String> ipService = new LinkedList <>();
static {
ipService.add("192.168.1.1");
ipService.add("192.168.1.2");
ipService.add("192.168.1.3");
}
public static int pos = 0;
public static String getIp(){
if(pos >= ipService.size()){
//防止索引越界
pos = 0;
}
String ip = ipService.get(pos);
pos ++;
return ip;
}
public static void main(String[] args) {
for (int i = 0; i < 4; i++) {
System.out.println(getIp());
}
}
}
模擬執(zhí)行4次執(zhí)行結(jié)果是

此時(shí)如果我有一臺(tái)服務(wù)器性能比較好(比如192.168.1.1),我想讓這臺(tái)服務(wù)器處理多一點(diǎn)請(qǐng)求,此時(shí)就涉及到了權(quán)重得概率,這種算法就不能實(shí)現(xiàn),請(qǐng)看我后面描述的輪詢升級(jí)版算法。
加權(quán)輪詢算法
此時(shí)我需要把我前面3臺(tái)服務(wù)器都設(shè)置權(quán)重,比如第一臺(tái)設(shè)置5,第二臺(tái)設(shè)置1,第三臺(tái)設(shè)置1
| 第一臺(tái)服務(wù)器 | 192.168.1.1 | 5 |
| 第二臺(tái)服務(wù)器 | 192.168.1.2 | 1 |
| 第三臺(tái)服務(wù)器 | 192.168.1.3 | 1 |
此時(shí)前5個(gè)請(qǐng)求都會(huì)訪問(wèn)到第一臺(tái)服務(wù)器,第六個(gè)請(qǐng)求會(huì)訪問(wèn)到第二臺(tái)服務(wù)器,第七個(gè)請(qǐng)求會(huì)訪問(wèn)到第三臺(tái)服務(wù)器。
以下是我給出的代碼案例:
public class WeightPolling {
/**
* key是ip,value是權(quán)重
*/
public static Map<String, Integer> ipService = new LinkedHashMap<>();
static {
ipService.put("192.168.1.1", 5);
ipService.put("192.168.1.2", 1);
ipService.put("192.168.1.3", 1);
}
public static int requestId = 0;
public static int getAndIncrement() {
return requestId++;
}
public static String getIp(){
//獲取總的權(quán)重
int totalWeight =0;
for (Integer value : ipService.values()) {
totalWeight+= value;
}
//獲取當(dāng)前輪詢的值
int andIncrement = getAndIncrement();
int pos = andIncrement% totalWeight;
for (String ip : ipService.keySet()) {
if(pos < ipService.get(ip)){
return ip;
}
pos -= ipService.get(ip);
}
return null;
}
public static void main(String[] args) {
for (int i = 0; i < 7; i++) {
System.out.println(getIp());
}
}
}
此時(shí)運(yùn)行結(jié)果是

可以看的第一臺(tái)服務(wù)器執(zhí)行了5次,后面2臺(tái)依次執(zhí)行一次,依次類推??赡苣阌X得這種算法還不錯(cuò)。其實(shí)這種算法有一個(gè)缺點(diǎn)是,如果我第一臺(tái)服務(wù)器設(shè)置權(quán)重過(guò)大可能我需要很多次請(qǐng)求都執(zhí)行到第一臺(tái)服務(wù)器上去,這樣的情況分布是不均勻的,會(huì)造成某一臺(tái)服務(wù)器壓力過(guò)大導(dǎo)致崩潰。所以我后面要引入第三種算法來(lái)解決這個(gè)問(wèn)題
平滑加權(quán)輪詢算法
這種算法可能比較復(fù)雜,我第一次看也有點(diǎn)不太明白,后面看過(guò)相關(guān)資料在結(jié)合我自己的理解給大家圖文解釋一下,這里我舉例的服務(wù)器配置和權(quán)重還是和上面一樣
| 請(qǐng)求 | 當(dāng)前權(quán)重 = 自身權(quán)重+選中后當(dāng)前權(quán)重 | 總權(quán)重 | 當(dāng)前最大權(quán)重 | 返回的ip | 選中后當(dāng)前權(quán)重=當(dāng)前最大權(quán)重-總權(quán)重 |
|---|---|---|---|---|---|
| 1 | {5,1,1} | 7 | 5 | 192.168.1.1 | {-2,1,1} |
| 2 | {3,2,2} | 7 | 3 | 192.168.1.1 | {-4,2,2} |
| 3 | {1,3,3} | 7 | 3 | 192.168.1.2 | {1,-4,3} |
| 4 | {6,-3,4} | 7 | 6 | 192.168.1.1 | {-1,-3,4} |
| 5 | {4,-2,5} | 7 | 5 | 192.168.1.3 | {4,-2,-2} |
| 6 | {9,-1,-1} | 7 | 9 | 192.168.1.1 | {2,-1,-1} |
| 7 | {7,0,0} | 7 | 7 | 192.168.1.1 | {0,0,0} |
由上圖可以看出第一臺(tái)服務(wù)器雖然權(quán)重設(shè)置的是5,但并不是第五次請(qǐng)求過(guò)來(lái)都是第一臺(tái)服務(wù)器執(zhí)行,而是分散執(zhí)行,調(diào)度序列是非常均勻的,且第 7 次調(diào)度時(shí)選中后當(dāng)前權(quán)重又回到 {0, 0, 0},實(shí)例的狀態(tài)同初始狀態(tài)一致,所以后續(xù)可以一直重復(fù)調(diào)度操作。
可能有的人還不能清楚的明白上一張圖表示的含義,我這里大概描述一下:
1.首先總權(quán)重不會(huì)變,默認(rèn)就是當(dāng)前設(shè)置的權(quán)重之和
2.在第一次請(qǐng)求進(jìn)來(lái)的時(shí)候我默認(rèn)初始化當(dāng)前權(quán)重選中值是{0,0,0},所以當(dāng)前權(quán)重的值就是{5+0,1+0,1+0},這里的5,1,1就是我們前面每臺(tái)服務(wù)器設(shè)置的權(quán)重。
3.這里我們可以得出第一次請(qǐng)求過(guò)來(lái)的最大權(quán)重是5。然后返回第一臺(tái)服務(wù)器ip
4.然后我們?cè)O(shè)置選中后當(dāng)前權(quán)重,這里就是當(dāng)前最大權(quán)重減去總權(quán)重(5-7),沒(méi)有選中的權(quán)重不變,這時(shí)候得到當(dāng)前權(quán)重選中權(quán)重的值{5-7,1,1}
5.在第二次請(qǐng)求過(guò)來(lái)的時(shí)候我們延續(xù)上面的2,3,4步驟執(zhí)行.
如果這里還有不懂得我下面會(huì)提供我自己用java代碼實(shí)現(xiàn)的算法:
public class Polling {
/**
* key是ip,value是權(quán)重
*/
public static Map <String,Integer> ipService = new LinkedHashMap <>();
static {
ipService.put("192.168.1.1",5);
ipService.put("192.168.1.2",1);
ipService.put("192.168.1.3",1);
}
private static Map<String,Weight> weightMap = new LinkedHashMap <>();
public static String getIp(){
//計(jì)算總的權(quán)重
int totalWeight = 0;
for (Integer value : ipService.values()) {
totalWeight+=value;
}
//首先判斷weightMap是否為空
if(weightMap.isEmpty()){
ipService.forEach((ip,weight)->{
Weight weights = new Weight(ip, weight,0);
weightMap.put(ip,weights);
});
}
//給map中得對(duì)象設(shè)置當(dāng)前權(quán)重
weightMap.forEach((ip,weight)->{
weight.setCurrentWeight(weight.getWeight() + weight.getCurrentWeight());
});
//判斷最大權(quán)重是否大于當(dāng)前權(quán)重,如果為空或者小于當(dāng)前權(quán)重,則把當(dāng)前權(quán)重賦值給最大權(quán)重
Weight maxWeight = null;
for (Weight weight : weightMap.values()) {
if(maxWeight ==null || weight.getCurrentWeight() > maxWeight.getCurrentWeight()){
maxWeight = weight;
}
}
//最后把當(dāng)前最大權(quán)重減去總的權(quán)重
maxWeight.setCurrentWeight(maxWeight.getCurrentWeight() - totalWeight);
//返回
return maxWeight.getIp();
}
public static void main(String[] args) {
//模擬輪詢7次取ip
for (int i = 0; i < 7; i++) {
System.out.println(getIp());
}
}
}
class Weight{
/**
* ip
*/
private String ip;
/**
* 設(shè)置得權(quán)重
*/
private int weight;
/**
* 當(dāng)前權(quán)重
*/
private int currentWeight;
public Weight(String ip, int weight,int currentWeight) {
this.ip = ip;
this.weight = weight;
this.currentWeight = currentWeight;
}
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;
}
public int getCurrentWeight() {
return currentWeight;
}
public void setCurrentWeight(int currentWeight) {
this.currentWeight = currentWeight;
}
}
這里代碼得執(zhí)行結(jié)果是:

可以看出此處執(zhí)行結(jié)果和表格里描述得結(jié)果一致。
總結(jié)
可能第三種算法理解起來(lái)有點(diǎn)復(fù)雜,如果看不懂圖表得意思可以先執(zhí)行下代碼,debugger一步步調(diào)試后還是很好理解。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
linux查找當(dāng)前系統(tǒng)nginx路徑的兩種方法
工作中有很多服務(wù)器, 它們上面裝的 nginx 的路徑也太不相當(dāng), 當(dāng)我們拿到一個(gè)不熟悉的服務(wù)器時(shí), 我們?cè)趺粗? 當(dāng)前運(yùn)行的nginx的目錄是哪一個(gè)呢,本文小編給大家介紹了兩種linux查找當(dāng)前系統(tǒng)nginx的路徑的方法,需要的朋友可以參考下2023-11-11
解決Nginx網(wǎng)關(guān)超時(shí)出現(xiàn)504 GATEWAY TIMEOUT的問(wèn)題
這篇文章主要給大家介紹了如何解決Nginx網(wǎng)關(guān)超時(shí)出現(xiàn)504 GATEWAY TIMEOUT的問(wèn)題,文章通過(guò)代碼示例和圖文結(jié)合介紹的非常詳細(xì),有遇到相同問(wèn)題的朋友可以參考閱讀本文2023-11-11
淺談nginx基于請(qǐng)求頭或請(qǐng)求內(nèi)容的防護(hù)
本文主要介紹了淺談nginx基于請(qǐng)求頭或請(qǐng)求內(nèi)容的防護(hù),通常涉及到對(duì)請(qǐng)求進(jìn)行過(guò)濾,檢查其是否包含某些特定的值或模式,感興趣的可以了解一下2023-10-10
詳解Nginx + Tomcat 反向代理 如何在高效的在一臺(tái)服務(wù)器部署多個(gè)站點(diǎn)
本篇文章主要介紹了詳解Nginx + Tomcat 反向代理 如何在高效的在一臺(tái)服務(wù)器部署多個(gè)站點(diǎn),具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12
結(jié)合 Nginx 將 DoNetCore 部署到 阿里云的安裝配置方法
這篇文章主要介紹了結(jié)合 Nginx 將 DoNetCore 部署到 阿里云的方法 ,需要的朋友可以參考下2018-10-10
Nginx服務(wù)器作反向代理時(shí)的緩存配置要點(diǎn)解析
這篇文章主要介紹了Nginx服務(wù)器作反向代理時(shí)的緩存配置要點(diǎn)解析,需要的朋友可以參考下2016-04-04
Nginx服務(wù)器中瀏覽器本地緩存和虛擬機(jī)的相關(guān)設(shè)置
這篇文章主要介紹了Nginx服務(wù)器中瀏覽器本地緩存和虛擬機(jī)的相關(guān)設(shè)置,是Nginx服務(wù)器搭建過(guò)程中的基本配置,需要的朋友可以參考下2015-08-08

