轮询算法简介
轮询算法简介
在实际工程应用中,Nginx作为高性能反向代理服务器被广泛部署,其配置方法已为众多开发者所熟知。本文将聚焦于Nginx负载均衡模块中几种关键轮询算法的底层实现原理与机制。
简单轮询算法原理
简单轮询算法
该算法逻辑最为直观,以三台服务器为例进行说明。
| 第一台服务器 | 192.168.1.1 |
| 第二台服务器 | 192.168.1.2 |
| 第三台服务器 | 192.168.1.3 |
当第一个请求到达时,默认将其分发至第一台服务器;第二个请求分发至第二台;第三个请求分发至第三台;第四个请求则循环回到第一台,依此类推。以下是该简单算法的Java代码实现:
public class SimplePolling {
/**
* key是ip
*/
public static List 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());
}
}
}
模拟执行4次请求后的输出结果如下:

然而,若某台服务器(如IP为192.168.1.1)性能显著优于其他节点,希望其承担更多流量时,上述简单轮询无法支持差异化处理。此时需引入带有概率权重的升级算法,具体见下文描述。
加权轮询算法实现
加权轮询算法
在此场景下,为前述三台服务器分别设置权重值,例如第一台设为5,第二台设为1,第三台设为1。
| 第一台服务器 | 192.168.1.1 | 5 |
| 第二台服务器 | 192.168.1.2 | 1 |
| 第三台服务器 | 192.168.1.3 | 1 |
依据此权重配置,前5个请求将全部指向第一台服务器,第6个请求指向第二台,第7个请求指向第三台,随后循环往复。
以下是加权轮询算法的代码案例:
public class WeightPolling {
/**
* key是ip,value是权重
*/
public static Map 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(){
//获取总的权重
int totalWeight =0;
for (Integer value : ipService.values()) {
totalWeight+= value;
}
//获取当前轮询的值
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());
}
}
}
运行该代码后的输出结果为:

从结果可见,第一台服务器连续执行了5次,后续两台各执行1次,呈现周期性重复。尽管该算法实现了权重分配,但存在明显缺陷:若首台服务器权重过大,会导致大量请求集中涌入,造成负载分布极度不均,可能引发服务器过载甚至崩溃。为解决这一问题,下文将引入第三种更优算法。
平滑加权轮询算法详解
平滑加权轮询算法
该算法逻辑相对复杂,初次接触时可能难以理解。结合相关资料与个人分析,通过图文形式详细拆解其工作原理。此处沿用前述服务器配置及权重设定。
| 请求 | 当前权重 = 自身权重+选中后当前权重 | 总权重 | 当前最大权重 | 返回的ip | 选中后当前权重=当前最大权重-总权重 |
|---|---|---|---|---|---|
| 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} |
由上图数据可知,尽管第一台服务器权重为5,但并非连续接收5次请求,而是分散调度。整个调度序列分布均匀,且在第7次调度后,各实例当前权重恢复至初始状态{0, 0, 0},系统状态重置,从而支持后续调度的无限循环。
若对上述图表含义仍感困惑,以下进行逐步文字解析:
1. 总权重保持恒定,等于所有服务器权重之和。
2. 首次请求进入时,初始化当前权重选中值为{0,0,0},因此计算出的当前权重为{5+0, 1+0, 1+0},即{5,1,1},其中5、1、1对应各服务器预设权重。
3. 由此得出第一次请求的最大当前权重为5,故选择第一台服务器IP返回。
4. 更新选中服务器的当前权重:新值 = 原最大权重 - 总权重(即5-7=-2),未选中服务器权重不变。此时当前权重集合变为{-2, 1, 1}。
5.在第二次请求过来的时候我们延续上面的2,3,4步骤执行.
若仍有疑问,下方提供基于Java实现的平滑加权轮询算法代码:
public class Polling {
/**
* key是ip,value是权重
*/
public static Map 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 weightMap = new LinkedHashMap <>();
public static String getIp(){
//计算总的权重
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中得对象设置当前权重
weightMap.forEach((ip,weight)->{
weight.setCurrentWeight(weight.getWeight() + weight.getCurrentWeight());
});
//判断最大权重是否大于当前权重,如果为空或者小于当前权重,则把当前权重赋值给最大权重
Weight maxWeight = null;
for (Weight weight : weightMap.values()) {
if(maxWeight ==null || weight.getCurrentWeight() > maxWeight.getCurrentWeight()){
maxWeight = weight;
}
}
//最后把当前最大权重减去总的权重
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;
/**
* 设置得权重
*/
private int weight;
/**
* 当前权重
*/
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;
}
}
该代码的执行结果如下:

对比可见,代码输出结果与前文表格描述的调度序列完全一致。
算法总结与建议
总结
第三种算法虽理解门槛较高,但若对图表逻辑存疑,建议先运行代码并通过调试器(debugger)单步跟踪变量变化,即可清晰掌握其内部机制。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。