Skip to content

Commit 99ba278

Browse files
committed
update 添加常见负载均衡相关代码
1 parent e515ffd commit 99ba278

20 files changed

+1019
-0
lines changed

code/loadbalance-java/.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/loadbalance-java/.idea/compiler.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/loadbalance-java/.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/loadbalance-java/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/loadbalance-java/.idea/uiDesigner.xml

Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.caojx.learn;
2+
3+
import java.util.Random;
4+
5+
/**
6+
* 随机轮询
7+
*
8+
* @author caojx created on 2022/6/20 8:34 PM
9+
*/
10+
public class RandomSelect {
11+
/**
12+
* 随机访问集合中的某一个值即可
13+
*
14+
* @return
15+
*/
16+
public static String getServer() {
17+
Random random = new Random();
18+
int rand = random.nextInt(ServerIps.LIST.size());
19+
20+
// 随机访问集合中的某一个值即可
21+
return ServerIps.LIST.get(rand);
22+
}
23+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.caojx.learn;
2+
3+
/**
4+
* 轮询
5+
*
6+
* @author caojx created on 2022/6/20 8:38 PM
7+
*/
8+
public class RoundRobin {
9+
10+
// 位置
11+
public static Integer POS = 0;
12+
13+
/**
14+
* 轮询按序遍历即可
15+
*
16+
* @return
17+
*/
18+
public static String getServer() {
19+
String ip = null;
20+
21+
synchronized (POS) {
22+
if (POS >= ServerIps.LIST.size()) {
23+
POS = 0;
24+
}
25+
26+
ip = ServerIps.LIST.get(POS);
27+
POS++;
28+
}
29+
30+
return ip;
31+
}
32+
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.caojx.learn;
2+
3+
import java.util.Arrays;
4+
import java.util.LinkedHashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
/**
9+
* 服务器id
10+
*
11+
* @author caojx created on 2022/6/20 8:34 PM
12+
*/
13+
public class ServerIps {
14+
15+
/**
16+
* A、B、C 假设是三台服务器
17+
*/
18+
public static final List<String> LIST = Arrays.asList("A", "B", "C");
19+
20+
/**
21+
* A、B、C 服务器,带权重
22+
* 即假如10次请求,A要请求2次,B要请求3次,C要请求5次
23+
*/
24+
public static final Map<String, Integer> WEIGHT_LIST = new LinkedHashMap<>();
25+
static {
26+
WEIGHT_LIST.put("A", 2);
27+
WEIGHT_LIST.put("B", 3);
28+
WEIGHT_LIST.put("C", 5);
29+
}
30+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.caojx.learn;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Random;
6+
7+
/**
8+
* 加权随机
9+
*
10+
* @author caojx created on 2022/6/20 8:49 PM
11+
*/
12+
public class WeightRandom {
13+
14+
/**
15+
* 缺点权重大ips越大占内存带权重随机
16+
*
17+
* @return
18+
*/
19+
public static String getServer() {
20+
21+
List<String> ips = new ArrayList<>();
22+
23+
for (String ip : ServerIps.WEIGHT_LIST.keySet()) {
24+
Integer weight = ServerIps.WEIGHT_LIST.get(ip);
25+
26+
// weight多少在ips里面存多少 例A权重为2 在ips里面存两个
27+
for (int i = 0; i < weight; i++) {
28+
ips.add(ip);
29+
}
30+
}
31+
32+
// 随机取,这里说的是概率
33+
Random random = new Random();
34+
int randomPos = random.nextInt(ips.size());
35+
return ips.get(randomPos);
36+
}
37+
}

0 commit comments

Comments
 (0)