根据响应时间计算权重,响应越长,权重越低,权重越低的服务器,被选择的可能性就越低

//定时统计权重
serverWeightTimer.schedule(new DynamicServerWeightTask(), 0, serverWeightTaskTimerInterval);

//权重数组(server存储的位置对应权重数组的位置)
//index处的权重为前面所有权重及index本身权重之和
private volatile List<Double> accumulatedWeights = new ArrayList<Double>();

//随机0-最大权重
double randomWeight = random.nextDouble() * maxTotalWeight;
// pick the server index based on the randomIndex
int n = 0;
for (Double d : currentWeights) {
if (d >= randomWeight) { //选出落在这个权重位置的index
serverIndex = n;
break;
} else {
n++;
}
}

server = allList.get(serverIndex);
 

 

相关文章:

  • 2021-08-27
  • 2022-01-07
  • 2021-07-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-06
  • 2022-03-04
  • 2022-12-23
  • 2022-12-23
  • 2021-05-18
  • 2022-01-19
  • 2021-08-20
相关资源
相似解决方案