Nginx如何实现轮询算法

简单轮询算法

这种算法比较简单,举个例子就是你有三台服务器

第一台服务器 192.168.1.1
第二台服务器 192.168.1.2
第三台服务器 192.168.1.3

第一个请求过来之后默认访问第一台,第二个请求过来访问第二台,第三次请求过来访问第三台,第四次请求过来访问第一台,以此类推。以下是我代码实现简单得算法:

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 &gt;= ipservice.size()){       //防止索引越界       pos = 0;     }     string ip = ipservice.get(pos);     pos ++;     return ip;    }    public static void main(string[] args) {     for (int i = 0; i <p>模拟执行4次执行结果是<br></p> <p><img src="https://img.php.cn/upload/article/000/887/227/168467659564255.png" alt="nginx如何实现轮询算法"></p> <p>此时如果我有一台服务器性能比较好(比如192.168.1.1),我想让这台服务器处理多一点请求,此时就涉及到了权重得概率,这种算法就不能实现,请看我后面描述的轮询升级版算法。</p> <p><strong>加权轮询算法</strong></p> <p>此时我需要把我前面3台服务器都设置权重,比如第一台设置5,第二台设置1,第三台设置1</p> <table><tbody> <tr class="firstRow"> <td>第一台服务器</td> <td>192.168.1.1</td> <td>5</td> </tr> <tr> <td>第二台服务器</td> <td>192.168.1.2</td> <td>1</td> </tr> <tr> <td>第三台服务器</td> <td>192.168.1.3</td> <td>1</td> </tr> </tbody></table> <p>此时前5个请求都会访问到第一台服务器,第六个请求会访问到第二台服务器,第七个请求会访问到第三台服务器。</p> <p>以下是我给出的代码案例:</p> <pre class="brush:Java;">public class weightpolling {    /**    * key是ip,value是权重    */   public static map<string> 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 <p>此时运行结果是<br></p> <p><img src="https://img.php.cn/upload/article/000/887/227/168467659592116.png" alt="Nginx如何实现轮询算法"></p> <p>可以看的第一台服务器执行了5次,后面2台依次执行一次,依次类推。可能你觉得这种算法还不错。其实这种算法有一个缺点是,如果我第一台服务器设置权重过大可能我需要很多次请求都执行到第一台服务器上去,这样的情况分布是不均匀的,会造成某一台服务器压力过大导致崩溃。所以我后面要引入第三种算法来解决这个问题</p> <p><strong>平滑加权轮询算法</strong></p> <p>这种算法可能比较复杂,我第一次看也有点不太明白,后面看过相关资料在结合我自己的理解给大家图文解释一下,这里我举例的服务器配置和权重还是和上面一样</p> <table width="654" height="190"> <thead><tr class="firstRow"> <th>请求</th> <th>当前权重 = 自身权重+选中后当前权重</th> <th>总权重</th> <th>当前最大权重</th> <th>返回的ip</th> <th>选中后当前权重=当前最大权重-总权重</th> </tr></thead> <tbody> <tr> <td>1</td> <td>{5,1,1}</td> <td>7</td> <td>5</td> <td>192.168.1.1</td> <td>{-2,1,1}</td> </tr> <tr> <td>2</td> <td>{3,2,2}</td> <td>7</td> <td>3</td> <td>192.168.1.1</td> <td>{-4,2,2}</td> </tr> <tr> <td>3</td> <td>{1,3,3}</td> <td>7</td> <td>3</td> <td>192.168.1.2</td> <td>{1,-4,3}</td> </tr> <tr> <td>4</td> <td>{6,-3,4}</td> <td>7</td> <td>6</td> <td>192.168.1.1</td> <td>{-1,-3,4}</td> </tr> <tr> <td>5</td> <td>{4,-2,5}</td> <td>7</td> <td>5</td> <td>192.168.1.3</td> <td>{4,-2,-2}</td> </tr> <tr> <td>6</td> <td>{9,-1,-1}</td> <td>7</td> <td>9</td> <td>192.168.1.1</td> <td>{2,-1,-1}</td> </tr> <tr> <td>7</td> <td>{7,0,0}</td> <td>7</td> <td>7</td> <td>192.168.1.1</td> <td>{0,0,0}</td> </tr> </tbody> </table> <p>由上图可以看出第一台服务器虽然权重设置的是5,但并不是第五次请求过来都是第一台服务器执行,而是分散执行,调度序列是非常均匀的,且第 7 次调度时选中后当前权重又回到 {0, 0, 0},实例的状态同初始状态一致,所以后续可以一直重复调度操作。</p> <p>可能有的人还不能清楚的明白上一张图表示的含义,我这里大概描述一下:</p> <p>1.首先总权重不会变,默认就是当前设置的权重之和</p> <p>2.在第一次请求进来的时候我默认初始化当前权重选中值是{0,0,0},所以当前权重的值就是{5+0,1+0,1+0},这里的5,1,1就是我们前面每台服务器设置的权重。</p> <p>3.这里我们可以得出第一次请求过来的最大权重是5。然后返回第一台服务器ip</p> <p>4.然后我们设置选中后当前权重,这里就是当前最大权重减去总权重(5-7),没有选中的权重不变,这时候得到当前权重选中权重的值{5-7,1,1}</p> <p>5.在第二次请求过来的时候我们延续上面的2,3,4步骤执行.</p> <p>如果这里还有不懂得我下面会提供我自己用java代码实现的算法:</p> <pre class="brush:java;">public class polling {    /**    * key是ip,value是权重    */   public static map <string> 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> 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)-&gt;{         weight weights = new weight(ip, weight,0);         weightmap.put(ip,weights);       });     }     //给map中得对象设置当前权重     weightmap.foreach((ip,weight)-&gt;{       weight.setcurrentweight(weight.getweight() + weight.getcurrentweight());     });      //判断最大权重是否大于当前权重,如果为空或者小于当前权重,则把当前权重赋值给最大权重     weight maxweight = null;     for (weight weight : weightmap.values()) {       if(maxweight ==null || weight.getcurrentweight() &gt; 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 <p>这里代码得执行结果是:<br></p> <p><img src="https://img.php.cn/upload/article/000/887/227/168467659543814.png" alt="Nginx如何实现轮询算法"></p> <p>可以看出此处执行结果和表格里描述得结果一致。</p></string></string>

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享