标签:style blog ar color sp java for div log
查看Nginx 源码,将Nginx IP选择算法变为Java:
package com.csst.msm; public class Test { /** * @param args */ public static void main(String[] args) { int hash =89;//初始值 int serverNum =2;//服务器个数 int addrlen=3;//IPV4 只计算前3个字节 int p; int[] addr = new int[]{192,168,10,14}; for(int i=0;i<addrlen;i++){//只计算了前3个字节 hash = (hash*113+addr[i]) % 6271; } p =hash % serverNum; System.out.println("选择的服务器:"+p); //权重算法 int[] weights = new int[]{9,9}; int total_weight =weights[0]+weights[1]; int w =hash %total_weight; int j=0; for(j=0;j<serverNum;j++){ w -= weights[j]; if(w<0) break; } p = j; System.out.println("按权重,选择的服务器:"+p); } }
从该代码可以看出,运行这段代码可以看出Nginx根据IP选择服务器的规律。
标签:style blog ar color sp java for div log
原文地址:http://www.cnblogs.com/skyblog/p/4127743.html