View Nginx source code, the Nginx IP selection algorithm into Java:
Packagecom.csst.msm; Public classTest {/** * @paramargs*/ Public Static voidMain (string[] args) {inthash = 89;//Initial value intServernum = 2;//Number of servers intaddrlen=3;//IPV4 Compute only the first 3 bytes intp; int[] addr =New int[]{192,168,10,14}; for(inti=0;i<addrlen;i++) {//only the first 3 bytes are calculatedhash = (Hash*113+addr[i])% 6271; } P=hash%Servernum; System.out.println ("Selected server:" +p); //Weight Algorithm int[] weights =New int[]{9,9}; intTotal_weight =weights[0]+weights[1]; intW =hash%Total_weight; intJ=0; for(j=0;j<servernum;j++) {W-=Weights[j]; if(w<0) Break; } P=J; System.out.println ("By weight, select server:" +p); }}
From this code can be seen, run this code can see nginx according to IP Select the rules of the server.
Nginx IP cluster selection algorithm java