开启dubbo之旅——四种负载均衡

正经学徒,佛系记录,不搞事情

客户端负载均衡使用方式:

application.properties配置文件中添加

dubbo.reference.loadbalance=leastactive

可配置参数包含:random,roundrobin,leastactive,consistenthash

第一种:RandomLoadBalance(系统默认的分配方式:随机)

直接搜索dubbo源码类RandomLoadBalance.java

    @Override
    protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
        int length = invokers.size(); // Number of invokers
        int totalWeight = 0; // The sum of weights
        boolean sameWeight = true; // Every invoker has the same weight?
        for (int i = 0; i < length; i++) {
            int weight = getWeight(invokers.get(i), invocation);
            totalWeight += weight; // Sum
            if (sameWeight && i > 0
                    && weight != getWeight(invokers.get(i - 1), invocation)) {
                sameWeight = false;
            }
        }
        if (totalWeight > 0 && !sameWeight) {
            // If (not every invoker has the same weight & at least one invoker's weight>0), select randomly based on totalWeight.
            int offset = random.nextInt(totalWeight);
            // Return a invoker based on the random value.
            for (int i = 0; i < length; i++) {
                offset -= getWeight(invokers.get(i), invocation);
                if (offset < 0) {
                    return invokers.get(i);
                }
            }
        }
        // If all invokers have the same weight value or totalWeight=0, return evenly.
        return invokers.get(random.nextInt(length));
    }

该方法即为随机负载均衡调用的方法,invokers表示目前服务提供者列表。

稍微解释一下程序:系统通过 getWeight() 公用方法获取每个服务的权重进行相加赋值给totalWeight 。在随机生成totalWeight 以内的随机数,循环所有的服务,递减totalWeight ,当值小于0时停止循环,返回当前递归的服务。

所以假设有A、B、C三个服务,权重分别是50,、100、200,则调用A的概率为50/(50+100+200),则调用B的概率为100/(50+100+200),则调用C的概率为200/(50+100+200)。

可以直接在管理平台调整权重

快捷调整方式:

 具体值调整方式

 第一种:RoundRobinLoadBalance(权重轮询)

    @Override
    protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
        String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName();
        int length = invokers.size(); // Number of invokers
        int maxWeight = 0; // The maximum weight
        int minWeight = Integer.MAX_VALUE; // The minimum weight
        final LinkedHashMap<Invoker<T>, IntegerWrapper> invokerToWeightMap = new LinkedHashMap<Invoker<T>, IntegerWrapper>();
        int weightSum = 0;
        for (int i = 0; i < length; i++) {
            int weight = getWeight(invokers.get(i), invocation);
            maxWeight = Math.max(maxWeight, weight); // Choose the maximum weight
            minWeight = Math.min(minWeight, weight); // Choose the minimum weight
            if (weight > 0) {
                invokerToWeightMap.put(invokers.get(i), new IntegerWrapper(weight));
                weightSum += weight;
            }
        }
        AtomicPositiveInteger sequence = sequences.get(key);
        if (sequence == null) {
            sequences.putIfAbsent(key, new AtomicPositiveInteger());
            sequence = sequences.get(key);
        }
        int currentSequence = sequence.getAndIncrement();
        if (maxWeight > 0 && minWeight < maxWeight) {
            int mod = currentSequence % weightSum;
            for (int i = 0; i < maxWeight; i++) {
                for (Map.Entry<Invoker<T>, IntegerWrapper> each : invokerToWeightMap.entrySet()) {
                    final Invoker<T> k = each.getKey();
                    final IntegerWrapper v = each.getValue();
                    if (mod == 0 && v.getValue() > 0) {
                        return k;
                    }
                    if (v.getValue() > 0) {
                        v.decrement();
                        mod--;
                    }
                }
            }
        }
        // Round robin
        return invokers.get(currentSequence % length);
    }

可见该方法会先获取到最大跟最小的权重,currentSequence记录当前调用了第几次,weightSum记录权重总和,invokerToWeightMap 记录服务和权重的映射。

  • 有A、B、C三个服务:

若权重相同则直接取模调用服务,循环调用ABC三个服务。

若权重不相同则根据权重的大小循环调用服务,服务每调用一次,currentSequence都会增加,在 currentSequence % weightSum的过程,mod一直在 0-weightSum 之间循环递增,使得各个服务在循环调用的过程,权重较低的只会存在于前几次循环中,如A的权重是1,B的权重是3,C的权重的5,则调用的过程是ABC  BC  BC  C  C

第三种:LeastActiveLoadBalance(最少活跃调用数)

    @Override
    protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
        int length = invokers.size(); // Number of invokers
        int leastActive = -1; // The least active value of all invokers
        int leastCount = 0; // The number of invokers having the same least active value (leastActive)
        int[] leastIndexs = new int[length]; // The index of invokers having the same least active value (leastActive)
        int totalWeight = 0; // The sum of weights
        int firstWeight = 0; // Initial value, used for comparision
        boolean sameWeight = true; // Every invoker has the same weight value?
        for (int i = 0; i < length; i++) {
            Invoker<T> invoker = invokers.get(i);
            int active = RpcStatus.getStatus(invoker.getUrl(), invocation.getMethodName()).getActive(); // Active number
            int weight = invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT); // Weight
            if (leastActive == -1 || active < leastActive) { // Restart, when find a invoker having smaller least active value.
                leastActive = active; // Record the current least active value
                leastCount = 1; // Reset leastCount, count again based on current leastCount
                leastIndexs[0] = i; // Reset
                totalWeight = weight; // Reset
                firstWeight = weight; // Record the weight the first invoker
                sameWeight = true; // Reset, every invoker has the same weight value?
            } else if (active == leastActive) { // If current invoker's active value equals with leaseActive, then accumulating.
                leastIndexs[leastCount++] = i; // Record index number of this invoker
                totalWeight += weight; // Add this invoker's weight to totalWeight.
                // If every invoker has the same weight?
                if (sameWeight && i > 0
                        && weight != firstWeight) {
                    sameWeight = false;
                }
            }
        }
        //过度层..........................................................
        // assert(leastCount > 0)
        if (leastCount == 1) {
            // If we got exactly one invoker having the least active value, return this invoker directly.
            return invokers.get(leastIndexs[0]);
        }
        if (!sameWeight && totalWeight > 0) {
            // If (not every invoker has the same weight & at least one invoker's weight>0), select randomly based on totalWeight.
            int offsetWeight = random.nextInt(totalWeight);
            // Return a invoker based on the random value.
            for (int i = 0; i < leastCount; i++) {
                int leastIndex = leastIndexs[i];
                offsetWeight -= getWeight(invokers.get(leastIndex), invocation);
                if (offsetWeight <= 0)
                    return invokers.get(leastIndex);
            }
        }
        // If all invokers have the same weight value or totalWeight=0, return evenly.
        return invokers.get(leastIndexs[random.nextInt(leastCount)]);
    }

这个模式可以根据过度层来分步理解:上半部分为了做统计分析,下半部分为了实现返回具体调用哪个服务

  • 上半部分:

循环所有的服务,记录服务的最低活跃度leastActive;统计处于最低活跃度的服务个数leastCount并且以此为下标记录具体的服务数组leastIndexs ;判断active(当前服务活跃度) == leastActive(最低活跃度),进而计算处于最低活跃度的各个服务的权重是否一样sameWeight ,所有最低活跃度服务的权重总和totalWeight

  • 下半部分:

将上半部分的得出的数据进一步分析,首先判断如果leastCount(最低活跃度的服务个数) ==1,表明当前只有一个服务处于最低活跃度,那就直接返回该服务;如果有多个处于最低活跃度的服务,你是否发现了规律,接下来的判断和随机负载均衡模式相似,即如果处于最低活跃度的服务权重相同,则随机返回0-leastCount的服务,如果权重不相同,则先随机生成0-totalWeight的值,通过循环服务,递减权重的方式判断需要返回的服务。

第四种:ConsistentHashLoadBalance(一致性hash)

    @Override
    protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
        String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName();
        int identityHashCode = System.identityHashCode(invokers);
        ConsistentHashSelector<T> selector = (ConsistentHashSelector<T>) selectors.get(key);
        if (selector == null || selector.identityHashCode != identityHashCode) {
            selectors.put(key, new ConsistentHashSelector<T>(invokers, invocation.getMethodName(), identityHashCode));
            selector = (ConsistentHashSelector<T>) selectors.get(key);
        }
        return selector.select(invocation);
    }

官方说明:

System.identityHashCode的作用是通过服务对象内存地址计算hash值,selector是存储hash值和服务对应关系的map,判断语句的作用是对selector进行赋值,当map中没有服务对应的hash值时或者hash值不一致(说明服务变更了),则添加 / 修改selector

更详细的一致性hash可参考:

https://blog.csdn.net/bntX2jSQfEHy7/article/details/79549368

https://cloud.tencent.com/developer/news/302262

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

My name is Red ^^

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

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

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

打赏作者

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

抵扣说明:

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

余额充值