-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWeightRoundRobinV2.java
More file actions
39 lines (35 loc) · 1.27 KB
/
WeightRoundRobinV2.java
File metadata and controls
39 lines (35 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.HashMap;
import java.util.Map;
public class WeightRoundRobinV2 {
public static Map<String,Weight> weights = new HashMap<>();
public static String getServer() {
int totalWeights = 0;
for(Integer weight : ServerIps.WEIGHT_LIST.values()) {
totalWeights += weight;
}
//currentWeight 默认值 0
if(weights.isEmpty()) {
ServerIps.WEIGHT_LIST.forEach((ip,weight)->
{
weights.put(ip,new Weight(ip,weight,0));
});
}
for(Weight weight:weights.values()) {
weight.setCurrentWeight(weight.getCurrentWeight() + weight.getWeight());
}
//找最大值
Weight maxCurrentWeight = null;
for(Weight weight:weights.values()) {
if(maxCurrentWeight == null || weight.getCurrentWeight() > maxCurrentWeight.getCurrentWeight()) {
maxCurrentWeight = weight;
}
}
maxCurrentWeight.setCurrentWeight( maxCurrentWeight.getCurrentWeight() - totalWeights);
return maxCurrentWeight.getIp();
}
public static void main(String[] args) {
for (int i = 0; i < 50; i++) {
System.out.println(getServer());
}
}
}