Dubbo LeastActiveLoadBalance 最少活跃数算法

简化后的
 /**
     * 每个服务维护一个活跃数计数器。当A机器开始处理请求,该计数器加1,此时A还未处理完成。
     * 若处理完毕则计数器减1。而B机器接受到请求后很快处理完毕。
     * 那么A,B的活跃数分别是1,0。当又产生了一个新的请求,
     * 则选择B机器去执行(B活跃数最小),这样使慢的机器A收到少的请求。
     * @param invokers
     * @param url
     * @param invocation
     * @param <T>
     * @return
     */
    @Override
    protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
        // Number of invokers 总个数
        int length = invokers.size();
        // The least active value of all invokers 最小的活跃数
        int leastActive = -1;
        // The number of invokers having the same least active value (leastActive) 相同最小活跃数的个数
        int leastCount = 0;
        // The index of invokers having the same least active value (leastActive) 相同最小活跃数的下标
        int[] leastIndexes = new int[length];
        //查找优先级 先找活跃最小值最小的
        for (int i = 0; i < length; i++) {
            Invoker<T> invoker = invokers.get(i);
            // 获取当前提供方机器活跃数越小,越优秀
            int active = RpcStatus.getStatus(invoker.getUrl(), invocation.getMethodName()).getActive();
            // 如果最小是-1或者是小于-1
            if (leastActive == -1 || active < leastActive) {
                leastActive = active;
                leastCount = 1;
                leastIndexes[0] = i;
            } else if (active == leastActive) {
            // 累计相同最小活跃数下标
                leastIndexes[leastCount++] = i;
            }
            // 如果只有一个最小则直接返回
            if (leastCount == 1) {
              
                return invokers.get(leastIndexes[0]);
            }
        // 。。。。。。。。
        }
        //  如果权重相同或权重为0则均等随机(权重代码已略去)
        return invokers.get(leastIndexes[ThreadLocalRandom.current().nextInt(leastCount)]);

    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

icool_ali

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值