In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to realize the bottom layer of Nginx polling algorithm, which is very detailed and has certain reference value. Friends who are interested must finish reading it!
Brief introduction of polling algorithm
Many people use nginx in their work, and they are familiar with the configuration of nginx. Today, I mainly want to introduce several low-level implementations of nginx polling algorithm.
Simple polling algorithm
This algorithm is relatively simple, for example, you have three servers.
First server 192.168.1.1 second server 192.168.1.2 third server 192.168.1.3
After the first request comes, it defaults to visit the first station, the second request to visit the second station, the third request to visit the third station, the fourth request to visit the first station, and so on. The following is a simple algorithm implemented by my code:
Public class SimplePolling {/ * key is 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 ()) {/ / prevent index from crossing boundaries 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次执行结果是At this time, if I have a server with good performance (for example, 192.168.1.1), I want this server to handle more requests, which involves the probability of weight, and this algorithm cannot be implemented. Please see the polling upgrade algorithm described later.
Weighted polling algorithm
At this point, I need to set weights for all three servers in front of me, such as the first set 5, the second set 1, and the third set 1.
First server 192.168.1.15 second server 192.168.1.21 third server 192.168.1.31
At this point, the first five requests will access the first server, the sixth request will access the second server, and the seventh request will access the third server.
Here is an example of the code I gave:
Public class WeightPolling {/ * key is the weight * / 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 () {/ / get the total weight int totalWeight = 0; for (Integer value: ipService.values ()) {totalWeight+= value;} / / get the current polling 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次,后面2台依次执行一次,依次类推。可能你觉得这种算法还不错。其实这种算法有一个缺点是,如果我第一台服务器设置权重过大可能我需要很多次请求都执行到第一台服务器上去,这样的情况分布是不均匀的,会造成某一台服务器压力过大导致崩溃。所以我后面要引入第三种算法来解决这个问题 平滑加权轮询算法 这种算法可能比较复杂,我第一次看也有点不太明白,后面看过相关资料在结合我自己的理解给大家图文解释一下,这里我举例的服务器配置和权重还是和上面一样 请求当前权重 = 自身权重+选中后当前权重总权重当前最大权重返回的ip选中后当前权重=当前最大权重-总权重1{5,1,1}75192.168.1.1{-2,1,1}2{3,2,2}73192.168.1.1{-4,2,2}3{1,3,3}73192.168.1.2{1,-4,3}4{6,-3,4}76192.168.1.1{-1,-3,4}5{4,-2,5}75192.168.1.3{4,-2,-2}6{9,-1,-1}79192.168.1.1{2,-1,-1}7{7,0,0}77192.168.1.1{0,0,0} 由上图可以看出第一台服务器虽然权重设置的是5,但并不是第五次请求过来都是第一台服务器执行,而是分散执行,调度序列是非常均匀的,且第 7 次调度时选中后当前权重又回到 {0, 0, 0},实例的状态同初始状态一致,所以后续可以一直重复调度操作。 可能有的人还不能清楚的明白上一张图表示的含义,我这里大概描述一下: 1.首先总权重不会变,默认就是当前设置的权重之和 2.在第一次请求进来的时候我默认初始化当前权重选中值是{0,0,0},所以当前权重的值就是{5+0,1+0,1+0},这里的5,1,1就是我们前面每台服务器设置的权重。 3.这里我们可以得出第一次请求过来的最大权重是5。然后返回第一台服务器ip 4.然后我们设置选中后当前权重,这里就是当前最大权重减去总权重(5-7),没有选中的权重不变,这时候得到当前权重选中权重的值{5-7,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);});} / / set the current weight weightMap.forEach for the object in map ((ip,weight)-> {weight.setCurrentWeight (weight.getWeight () + weight.getCurrentWeight ());}) / / determine whether the maximum weight is greater than the current weight, and if it is empty or less than the current weight, assign the current weight to the maximum weight Weight maxWeight = null; for (Weight weight: weightMap.values ()) {if (maxWeight = = null | | weight.getCurrentWeight () > maxWeight.getCurrentWeight ()) {maxWeight = weight }} / / finally subtract the current maximum weight from the total weight maxWeight.setCurrentWeight (maxWeight.getCurrentWeight ()-totalWeight); / / return return maxWeight.getIp ();} public static void main (String [] args) {/ / simulate polling 7 times to fetch ip for (int I = 0; I < 7; iweights +) {System.out.println (getIp ()) } class Weight {/ * ip * / private String ip; / * weight set * / private int weight; / * current 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;}}
Here the code has to be executed and the result is:
You can see that the execution results here are consistent with those described in the table.
Summary
Maybe the third algorithm is a little complicated to understand. If you don't understand the meaning of the chart, you can execute the code first. Debugger is still easy to understand after step-by-step debugging.
These are all the contents of the article "how to implement the underlying Nginx polling algorithm". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.