代码
package cn.com.codingce.随做.平滑加权轮询算法;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SmoothWeightedRobinTest {
private List<ServerConfig> serverList = new ArrayList<>();
private Integer weightSum;
public SmoothWeightedRobinTest(List<ServerConfig> serverList) {
this.serverList = serverList;
this.weightSum = serverList.stream().map(ServerConfig::getWeight).reduce((x, y) -> x += y).get();
}
public static void main(String[] args) {
List<ServerConfig> list = new ArrayList<>();
list.add(new ServerConfig("192.168.1.1", 5, 5));
list.add(new ServerConfig("192.168.1.2", 1, 1));
list.add(new ServerConfig("192.168.1.3", 1, 1));
SmoothWeightedRobinTest test = new SmoothWeightedRobinTest(list);
test.test();
}
List<ServerConfig> tempList = new ArrayList<>();
List<String> chooseList = new ArrayList<>();
public void test() {
System.out.println("初始化当前权重:" + serverList.toString());
System.out.println();
for (int i = 1; i <= 20; i++) {
this.nextServerIndex(i);
}
System.out.println(chooseList);
}
public ServerConfig nextServerIndex(int index) {
ServerConfig max = Collections.max(serverList, Comparator.comparingInt(ServerConfig::getCurrentWeight));
ServerConfig tmpSv = null;
for (ServerConfig serverConfig : serverList) {
if (max.equals(serverConfig)) {
serverConfig.setCurrentWeight(serverConfig.getCurrentWeight() - weightSum);
if (tmpSv == null || serverConfig.getCurrentWeight() > tmpSv.getCurrentWeight()) {
tmpSv = serverConfig;
}
}
try {
tempList.add(serverConfig.clone());
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
serverConfig.setCurrentWeight(serverConfig.getCurrentWeight() + serverConfig.getWeight());
}
System.out.println("第" + index + "次选中后的当前权重:" + tempList.toString());
tempList.clear();
System.out.println("第" + index + "次选中的实例:" + tmpSv.getName());
chooseList.add(tmpSv.getName());
System.out.println("第" + (index + 1) + "次选中前的当前权重:" + serverList.toString());
System.out.println();
return tmpSv;
}
public static class ServerConfig {
public String name;
public int weight;
public int currentWeight;
public ServerConfig() {
}
public ServerConfig(String name, int weight) {
this.name = name;
this.weight = weight;
}
public ServerConfig(String name, int weight, int currentWeight) {
this.name = name;
this.weight = weight;
this.currentWeight = currentWeight;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getCurrentWeight() {
return currentWeight;
}
public void setCurrentWeight(int currentWeight) {
this.currentWeight = currentWeight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "ServerConfig{" + "name='" + name + '\'' + ", curr=" + currentWeight + '}';
}
@Override
public ServerConfig clone() throws CloneNotSupportedException {
ServerConfig serverConfig = new ServerConfig();
serverConfig.setCurrentWeight(this.currentWeight);
serverConfig.setName(this.name);
serverConfig.setWeight(this.weight);
return serverConfig;
}
}
}
- 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
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
日志
/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/bin/java -Dvisualvm.id=125005792986662 -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=53787:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/rt.jar:/Users/mxz/mxz-code/gitee/codingce-leetcode/new_code/out/production/new_code cn.com.codingce.随做.平滑加权轮询算法.SmoothWeightedRobinTest
初始化当前权重:[ServerConfig{name='192.168.1.1', curr=5}, ServerConfig{name='192.168.1.2', curr=1}, ServerConfig{name='192.168.1.3', curr=1}]
第1次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=-2}, ServerConfig{name='192.168.1.2', curr=1}, ServerConfig{name='192.168.1.3', curr=1}]
第1次选中的实例:192.168.1.1
第2次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=3}, ServerConfig{name='192.168.1.2', curr=2}, ServerConfig{name='192.168.1.3', curr=2}]
第2次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=-4}, ServerConfig{name='192.168.1.2', curr=2}, ServerConfig{name='192.168.1.3', curr=2}]
第2次选中的实例:192.168.1.1
第3次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=1}, ServerConfig{name='192.168.1.2', curr=3}, ServerConfig{name='192.168.1.3', curr=3}]
第3次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=1}, ServerConfig{name='192.168.1.2', curr=-4}, ServerConfig{name='192.168.1.3', curr=3}]
第3次选中的实例:192.168.1.2
第4次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=6}, ServerConfig{name='192.168.1.2', curr=-3}, ServerConfig{name='192.168.1.3', curr=4}]
第4次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=-1}, ServerConfig{name='192.168.1.2', curr=-3}, ServerConfig{name='192.168.1.3', curr=4}]
第4次选中的实例:192.168.1.1
第5次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=4}, ServerConfig{name='192.168.1.2', curr=-2}, ServerConfig{name='192.168.1.3', curr=5}]
第5次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=4}, ServerConfig{name='192.168.1.2', curr=-2}, ServerConfig{name='192.168.1.3', curr=-2}]
第5次选中的实例:192.168.1.3
第6次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=9}, ServerConfig{name='192.168.1.2', curr=-1}, ServerConfig{name='192.168.1.3', curr=-1}]
第6次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=2}, ServerConfig{name='192.168.1.2', curr=-1}, ServerConfig{name='192.168.1.3', curr=-1}]
第6次选中的实例:192.168.1.1
第7次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=7}, ServerConfig{name='192.168.1.2', curr=0}, ServerConfig{name='192.168.1.3', curr=0}]
第7次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=0}, ServerConfig{name='192.168.1.2', curr=0}, ServerConfig{name='192.168.1.3', curr=0}]
第7次选中的实例:192.168.1.1
第8次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=5}, ServerConfig{name='192.168.1.2', curr=1}, ServerConfig{name='192.168.1.3', curr=1}]
第8次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=-2}, ServerConfig{name='192.168.1.2', curr=1}, ServerConfig{name='192.168.1.3', curr=1}]
第8次选中的实例:192.168.1.1
第9次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=3}, ServerConfig{name='192.168.1.2', curr=2}, ServerConfig{name='192.168.1.3', curr=2}]
第9次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=-4}, ServerConfig{name='192.168.1.2', curr=2}, ServerConfig{name='192.168.1.3', curr=2}]
第9次选中的实例:192.168.1.1
第10次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=1}, ServerConfig{name='192.168.1.2', curr=3}, ServerConfig{name='192.168.1.3', curr=3}]
第10次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=1}, ServerConfig{name='192.168.1.2', curr=-4}, ServerConfig{name='192.168.1.3', curr=3}]
第10次选中的实例:192.168.1.2
第11次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=6}, ServerConfig{name='192.168.1.2', curr=-3}, ServerConfig{name='192.168.1.3', curr=4}]
第11次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=-1}, ServerConfig{name='192.168.1.2', curr=-3}, ServerConfig{name='192.168.1.3', curr=4}]
第11次选中的实例:192.168.1.1
第12次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=4}, ServerConfig{name='192.168.1.2', curr=-2}, ServerConfig{name='192.168.1.3', curr=5}]
第12次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=4}, ServerConfig{name='192.168.1.2', curr=-2}, ServerConfig{name='192.168.1.3', curr=-2}]
第12次选中的实例:192.168.1.3
第13次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=9}, ServerConfig{name='192.168.1.2', curr=-1}, ServerConfig{name='192.168.1.3', curr=-1}]
第13次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=2}, ServerConfig{name='192.168.1.2', curr=-1}, ServerConfig{name='192.168.1.3', curr=-1}]
第13次选中的实例:192.168.1.1
第14次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=7}, ServerConfig{name='192.168.1.2', curr=0}, ServerConfig{name='192.168.1.3', curr=0}]
第14次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=0}, ServerConfig{name='192.168.1.2', curr=0}, ServerConfig{name='192.168.1.3', curr=0}]
第14次选中的实例:192.168.1.1
第15次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=5}, ServerConfig{name='192.168.1.2', curr=1}, ServerConfig{name='192.168.1.3', curr=1}]
第15次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=-2}, ServerConfig{name='192.168.1.2', curr=1}, ServerConfig{name='192.168.1.3', curr=1}]
第15次选中的实例:192.168.1.1
第16次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=3}, ServerConfig{name='192.168.1.2', curr=2}, ServerConfig{name='192.168.1.3', curr=2}]
第16次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=-4}, ServerConfig{name='192.168.1.2', curr=2}, ServerConfig{name='192.168.1.3', curr=2}]
第16次选中的实例:192.168.1.1
第17次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=1}, ServerConfig{name='192.168.1.2', curr=3}, ServerConfig{name='192.168.1.3', curr=3}]
第17次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=1}, ServerConfig{name='192.168.1.2', curr=-4}, ServerConfig{name='192.168.1.3', curr=3}]
第17次选中的实例:192.168.1.2
第18次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=6}, ServerConfig{name='192.168.1.2', curr=-3}, ServerConfig{name='192.168.1.3', curr=4}]
第18次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=-1}, ServerConfig{name='192.168.1.2', curr=-3}, ServerConfig{name='192.168.1.3', curr=4}]
第18次选中的实例:192.168.1.1
第19次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=4}, ServerConfig{name='192.168.1.2', curr=-2}, ServerConfig{name='192.168.1.3', curr=5}]
第19次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=4}, ServerConfig{name='192.168.1.2', curr=-2}, ServerConfig{name='192.168.1.3', curr=-2}]
第19次选中的实例:192.168.1.3
第20次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=9}, ServerConfig{name='192.168.1.2', curr=-1}, ServerConfig{name='192.168.1.3', curr=-1}]
第20次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=2}, ServerConfig{name='192.168.1.2', curr=-1}, ServerConfig{name='192.168.1.3', curr=-1}]
第20次选中的实例:192.168.1.1
第21次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=7}, ServerConfig{name='192.168.1.2', curr=0}, ServerConfig{name='192.168.1.3', curr=0}]
[192.168.1.1, 192.168.1.1, 192.168.1.2, 192.168.1.1, 192.168.1.3, 192.168.1.1, 192.168.1.1, 192.168.1.1, 192.168.1.1, 192.168.1.2, 192.168.1.1, 192.168.1.3, 192.168.1.1, 192.168.1.1, 192.168.1.1, 192.168.1.1, 192.168.1.2, 192.168.1.1, 192.168.1.3, 192.168.1.1]
Process finished with exit code 0
- 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
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86