开启dubbo之旅——启动预热过程

解析Dubbo服务框架中RoundRobinLoadBalance的权重分配机制与预热逻辑,探讨服务启动初期权重为何默认为1及如何逐渐恢复至设定值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

dubbo项目在刚启动时经常会出现服务超时,起初并不在意,后来测试负载均衡RoundRobinLoadBalance(权重轮询)调用时,发现调用的服务器老是不对,于是进入RoundRobinLoadBalance.java类断点调试。

    @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);
    }

 getWeight方法是用来获取每个服务的权重,但是项目启动时,发起请求,发现这个方法的返回值都是1,这就玄学了,自己设置的权重为什么没有生效,只能进入getWeight方法一探究竟

    protected int getWeight(Invoker<?> invoker, Invocation invocation) {
        int weight = invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT);
        if (weight > 0) {
            long timestamp = invoker.getUrl().getParameter(Constants.REMOTE_TIMESTAMP_KEY, 0L);
            if (timestamp > 0L) {
                int uptime = (int) (System.currentTimeMillis() - timestamp);
                int warmup = invoker.getUrl().getParameter(Constants.WARMUP_KEY, Constants.DEFAULT_WARMUP);
                if (uptime > 0 && uptime < warmup) {
                    weight = calculateWarmupWeight(uptime, warmup, weight);
                }
            }
        }
        return weight;
    }

原来这里第二行的方法才是真正获取设置权重的地方,并且调试到这里获取到的权重也确实是自己设置的权重,其中

uptime获取的是项目启动的时间

warmup按中式英文翻译表示记录的是预热时间,且默认值为10分钟

那剩下的问题就是研究为什么经过方法calculateWarmupWeight后权重值变更了。

    static int calculateWarmupWeight(int uptime, int warmup, int weight) {
        int ww = (int) ((float) uptime / ((float) warmup / (float) weight));
        return ww < 1 ? 1 : (ww > weight ? weight : ww);
    }

有一个公式:(启动时间/预热时间)/ 权重。

看起来是在计算百分比,整个过程连接起来的意思就是:

这样看来,当项目刚刚启动时,权重都会是1,并且随着时间慢慢的增加到真实权重,当过了系统的预热时间(十分钟),则进入正常的调用逻辑。

所以问题又归咎于为什么有预热时间这个东西,经查一些资料后得知:由于JVM也是带有预热的过程,所以为了不让服务提供者一启动项目就承担全部的流量,才会使用这个预热来进行缓冲。这个问题尚且只能理解成日常生活中烤箱,烤箱是需要预热的,如果在烤制之前没有对烤箱进行预热,会使得面糊团无法漂亮呈色或是会变得干燥。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为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、付费专栏及课程。

余额充值