Dubbo源码学习--LeastActiveLoadBalance负载均衡(三)

桃扇骨 2022-05-28 00:42 288阅读 0赞

LeastActive LoadBalance

  • 最少活跃调用数,相同活跃数的随机,活跃数指调用前后计数差。

  • 使慢的提供者收到更少请求,因为越慢的提供者的调用前后计数差会越大。

    public class LeastActiveLoadBalance extends AbstractLoadBalance {

    1. public static final String NAME = "leastactive";
    2. private final Random random = new Random();
    3. protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
    4. int length = invokers.size(); // 服务提供者总个数
    5. int leastActive = -1; // 最小的活跃数
    6. int leastCount = 0; // 相同活跃数的格式
    7. int[] leastIndexs = new int[length]; // The index of invokers having the same least active value (leastActive)
    8. int totalWeight = 0; // 总权重
    9. int firstWeight = 0; // 第一个权重
    10. boolean sameWeight = true; //是否所有权重相同
    11. for (int i = 0; i < length; i++) {
    12. Invoker<T> invoker = invokers.get(i);
    13. int active = RpcStatus.getStatus(invoker.getUrl(), invocation.getMethodName()).getActive(); // 每个服务的活跃数
    14. int weight = invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT); // 权重
    15. if (leastActive == -1 || active < leastActive) { //发现最小的活跃数,重新开始
    16. leastActive = active; // 记录当前活跃数
    17. leastCount = 1; // 活跃格式
    18. leastIndexs[0] = i; // 坐标id
    19. totalWeight = weight; // 权重
    20. firstWeight = weight; // 权重
    21. sameWeight = true; // 重新设置为权重相同
    22. } else if (active == leastActive) { // 如果活跃数相同,则记录权重
    23. leastIndexs[leastCount++] = i; // Record index number of this invoker
    24. totalWeight += weight; // Add this invoker's weight to totalWeight.
    25. // If every invoker has the same weight?
    26. if (sameWeight && i > 0
    27. && weight != firstWeight) {
    28. sameWeight = false;
    29. }
    30. }
    31. }
    32. // assert(leastCount > 0)
    33. if (leastCount == 1) {
    34. // If we got exactly one invoker having the least active value, return this invoker directly.
    35. return invokers.get(leastIndexs[0]);
    36. }
    37. //如果存在活跃数相关的服务,则随机一个权重值,看看权重值落到哪个服务中则返回某个服务
    38. if (!sameWeight && totalWeight > 0) {
    39. // If (not every invoker has the same weight & at least one invoker's weight>0), select randomly based on totalWeight.
    40. int offsetWeight = random.nextInt(totalWeight);
    41. // Return a invoker based on the random value.
    42. for (int i = 0; i < leastCount; i++) {
    43. int leastIndex = leastIndexs[i];
    44. offsetWeight -= getWeight(invokers.get(leastIndex), invocation);
    45. if (offsetWeight <= 0)
    46. return invokers.get(leastIndex);
    47. }
    48. }
    49. // If all invokers have the same weight value or totalWeight=0, return evenly.
    50. //如果权重相同或权重为0则均等随机
    51. return invokers.get(leastIndexs[random.nextInt(leastCount)]);
    52. }

    }

发表评论

表情:
评论列表 (有 0 条评论,288人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Dubbo - 负载均衡

    前言 负载均衡,英文名称为Load Balance,其含义就是指将负载(工作任务)进行平衡、分摊到多个操作单元上进行运行。 例如:在Dubbo中,同一个服务有多个服务提