11package io .github .dunwu .javatech ;
22
3- import cn .hutool .core .util .StrUtil ;
4-
53import java .util .*;
64
75/**
6+ * 负载均衡算法测试例
7+ *
88 * @author peng.zhang
99 * @date 2021/1/19
1010 */
1111public class LoadBalanceDemo {
1212
1313 private static final Random random = new Random ();
1414
15+ public static String randomIpv4 () {
16+ int [][] range = { { 607649792 , 608174079 }, // 36.56.0.0-36.63.255.255
17+ { 1038614528 , 1039007743 }, // 61.232.0.0-61.237.255.255
18+ { 1783627776 , 1784676351 }, // 106.80.0.0-106.95.255.255
19+ { 2035023872 , 2035154943 }, // 121.76.0.0-121.77.255.255
20+ { 2078801920 , 2079064063 }, // 123.232.0.0-123.235.255.255
21+ { -1950089216 , -1948778497 }, // 139.196.0.0-139.215.255.255
22+ { -1425539072 , -1425014785 }, // 171.8.0.0-171.15.255.255
23+ { -1236271104 , -1235419137 }, // 182.80.0.0-182.92.255.255
24+ { -770113536 , -768606209 }, // 210.25.0.0-210.47.255.255
25+ { -569376768 , -564133889 }, // 222.16.0.0-222.95.255.255
26+ };
27+
28+ Random rdint = new Random ();
29+ int index = rdint .nextInt (10 );
30+ String ip = num2ip (range [index ][0 ]
31+ + new Random ().nextInt (range [index ][1 ] - range [index ][0 ]));
32+ return ip ;
33+ }
34+
35+ private static String num2ip (final int ip ) {
36+ int [] b = new int [4 ];
37+ String result = "" ;
38+ b [0 ] = (ip >> 24 ) & 0xff ;
39+ b [1 ] = ((ip >> 16 ) & 0xff );
40+ b [2 ] = ((ip >> 8 ) & 0xff );
41+ b [3 ] = (ip & 0xff );
42+ result = Integer .toString (b [0 ]) + "." + Integer .toString (b [1 ]) + "."
43+ + Integer .toString (b [2 ]) + "." + Integer .toString (b [3 ]);
44+ return result ;
45+ }
46+
1547 /**
16- * 生成 10 个不一样的 IP 地址
48+ * 生成 num 个随机 IP 地址
1749 */
18- private static List <String > init10IpList ( ) {
50+ private static List <String > initRandomIpList ( int num ) {
1951 List <String > list = new ArrayList <>();
20- for (int i = 1 ; i <= 10 ; i ++) {
21- list .add ("127.0.0." + i );
52+ for (int i = 1 ; i <= num ; i ++) {
53+ list .add (randomIpv4 () );
2254 }
2355 return list ;
2456 }
@@ -49,15 +81,16 @@ private static List<Node> initNodeList(Integer num, boolean sameWeight, boolean
4981 /**
5082 * 统计负载均衡命中次数,样本数为 10000 次访问
5183 */
52- private static Map <Node , Long > loadBalance10000 (LoadBalance <Node > algorithm , List <Node > nodes ) {
84+ private static Map <Node , Long > loadBalance10000 (LoadBalance <Node > algorithm , List <Node > nodes ,
85+ List <String > ipList ) {
5386 Map <Node , Long > staticMap = new TreeMap <>();
5487
55- List <String > ipList = init10IpList ();
5688 int ipLength = ipList .size ();
5789 for (int i = 0 ; i < 10000 ; i ++) {
5890 String ip = ipList .get (random .nextInt (ipLength ));
5991 Node node = algorithm .select (nodes , ip );
60- System .out .println (StrUtil .format ("ip = {}, node url = {}" , ip , node .getUrl ()));
92+ // 打印每一次负载均衡的选择结果
93+ // System.out.println(StrUtil.format("ip = {}, node url = {}", ip, node.getUrl()));
6194 if (staticMap .containsKey (node )) {
6295 Long value = staticMap .get (node );
6396 staticMap .put (node , ++value );
@@ -76,28 +109,34 @@ private static Map<Node, Long> loadBalance10000(LoadBalance<Node> algorithm, Lis
76109 }
77110
78111 public static void main (String [] args ) {
112+ // 构造 100 个候选服务器节点
79113 List <Node > nodes = initNodeList (100 , false , false );
114+ // 构造 100 个随机IP
115+ List <String > ipList = initRandomIpList (100 );
116+
117+ // ============================================================================
118+ // 基于以上构造数据,对每种算法都 负载均衡选择 10000 次,然后统计方差、标准差,查看负载均衡效果。
119+
120+ System .out .println ("======================= 随机负载均衡 =======================" );
121+ loadBalance10000 (new RandomLoadBalance <>(), nodes , ipList );
122+
123+ System .out .println ("======================= 加权随机负载均衡 =======================" );
124+ loadBalance10000 (new WeightRandomLoadBalance <>(), nodes , ipList );
125+
126+ System .out .println ("======================= 轮询负载均衡 =======================" );
127+ loadBalance10000 (new RoundRobinLoadBalance <>(), nodes , ipList );
128+
129+ System .out .println ("======================= 加权轮询负载均衡 =======================" );
130+ loadBalance10000 (new WeightRoundRobinLoadBalance <>(), nodes , ipList );
131+
132+ System .out .println ("======================= 源地址哈希负载均衡 =======================" );
133+ loadBalance10000 (new IpHashLoadBalance <>(), nodes , ipList );
80134
81- // System.out.println("======================= 随机负载均衡 =======================");
82- // loadBalance10000(new RandomLoadBalance<>(), nodes);
83- //
84- // System.out.println("======================= 加权随机负载均衡 =======================");
85- // loadBalance10000(new WeightRandomLoadBalance<>(), nodes);
86- //
87- // System.out.println("======================= 轮询负载均衡 =======================");
88- // loadBalance10000(new RoundRobinLoadBalance<>(), nodes);
89- //
90- // System.out.println("======================= 加权轮询负载均衡 =======================");
91- // loadBalance10000(new WeightRoundRobinLoadBalance<>(), nodes);
92- //
93- // System.out.println("======================= 源地址哈希负载均衡 =======================");
94- // loadBalance10000(new IpHashLoadBalance<>(), nodes);
95- //
96- // System.out.println("======================= 最小活跃数负载均衡 =======================");
97- // loadBalance10000(new LeastActiveLoadBalance<>(), nodes);
135+ System .out .println ("======================= 最小活跃数负载均衡 =======================" );
136+ loadBalance10000 (new LeastActiveLoadBalance <>(), nodes , ipList );
98137
99138 System .out .println ("======================= 一致性哈希负载均衡 =======================" );
100- loadBalance10000 (new ConsistentHashLoadBalance <>(), nodes );
139+ loadBalance10000 (new ConsistentHashLoadBalance <>(), nodes , ipList );
101140 }
102141
103142}
0 commit comments