加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
WeightRoundRobinV2.java 1.27 KB
一键复制 编辑 原始数据 按行查看 历史
zwbckmy 提交于 2021-06-03 20:28 . 负载均衡
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());
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化