Dubbo的几个负载均衡类--最短响应时间

这篇具有很好参考价值的文章主要介绍了Dubbo的几个负载均衡类--最短响应时间。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

-----------------看过之前一致性哈希和最少活跃书的可以跳过----------------- 

链接在此:Dubbo的几个负载均衡类--一致性哈希

Dubbo的几个负载均衡类--最少活跃数

Dubbo的几个负载均衡类--轮询

Dubbo的几个负载均衡类--随机

消费者发起调用过程中涉及如下几步

1:接口调用,比如DemoService.demoMethod

2:InvokerInvocationHandler.invoker:消费端启动时,通过JavassistProxyFactory.getProxy反射获取代理类,之后服务调用就直接调用这个Handler

3:MigrationInvoker.invoke:Dubbo 发起调用非常重要的一步,如果失败了,通过这个invoker做切换

4:其他

5:FailoverClusterInvoker.invoke(目前我们使用的,实际在AbstractClusterInvoker里面invoke逻辑是固定的)

6:其他

@Override
    public Result invoke(final Invocation invocation) throws RpcException {
        checkWhetherDestroyed();
 
        // binding attachments into invocation.
        Map<String, Object> contextAttachments = RpcContext.getContext().getObjectAttachments();
        if (contextAttachments != null && contextAttachments.size() != 0) {
            ((RpcInvocation) invocation).addObjectAttachments(contextAttachments);
        }
        //如果设置了标签规则,则通过list方法过滤出来符合标签的几个invoker
        List<Invoker<T>> invokers = list(invocation);
        LoadBalance loadbalance = initLoadBalance(invokers, invocation);
        RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation);
        //请求负载并且做好灾备降级
        return doInvoke(invocation, invokers, loadbalance);
    }
 
public Result doInvoke(Invocation invocation, final List<Invoker<T>> invokers, LoadBalance loadbalance) throws RpcException {
        List<Invoker<T>> copyInvokers = invokers;
        checkInvokers(copyInvokers, invocation);
        String methodName = RpcUtils.getMethodName(invocation);
        int len = calculateInvokeTimes(methodName);
        // retry loop.
        RpcException le = null; // last exception.
        List<Invoker<T>> invoked = new ArrayList<Invoker<T>>(copyInvokers.size()); // invoked invokers.
        Set<String> providers = new HashSet<String>(len);
        for (int i = 0; i < len; i++) {
            //Reselect before retry to avoid a change of candidate `invokers`.
            //NOTE: if `invokers` changed, then `invoked` also lose accuracy.
            if (i > 0) {
                checkWhetherDestroyed();
                copyInvokers = list(invocation);
                // check again
                checkInvokers(copyInvokers, invocation);
            }
            //这里通过loadBalance做负载
            Invoker<T> invoker = select(loadbalance, invocation, copyInvokers, invoked);
            invoked.add(invoker);
            RpcContext.getContext().setInvokers((List) invoked);
            try {
                Result result = invoker.invoke(invocation);
                if (le != null && logger.isWarnEnabled()) {
                    logger.warn("Although retry the method " + methodName
                            + " in the service " + getInterface().getName()
                            + " was successful by the provider " + invoker.getUrl().getAddress()
                            + ", but there have been failed providers " + providers
                            + " (" + providers.size() + "/" + copyInvokers.size()
                            + ") from the registry " + directory.getUrl().getAddress()
                            + " on the consumer " + NetUtils.getLocalHost()
                            + " using the dubbo version " + Version.getVersion() + ". Last error is: "
                            + le.getMessage(), le);
                }
                return result;
            } catch (RpcException e) {
                if (e.isBiz()) { // biz exception.
                    throw e;
                }
                le = e;
            } catch (Throwable e) {
                le = new RpcException(e.getMessage(), e);
            } finally {
                providers.add(invoker.getUrl().getAddress());
            }
        }
        throw new RpcException(le.getCode(), "Failed to invoke the method "
                + methodName + " in the service " + getInterface().getName()
                + ". Tried " + len + " times of the providers " + providers
                + " (" + providers.size() + "/" + copyInvokers.size()
                + ") from the registry " + directory.getUrl().getAddress()
                + " on the consumer " + NetUtils.getLocalHost() + " using the dubbo version "
                + Version.getVersion() + ". Last error is: "
                + le.getMessage(), le.getCause() != null ? le.getCause() : le);
    }

-----------------看过之前一致性哈希的可以跳过----------------- 

@Override
    protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
        // Number of invokers
        int length = invokers.size();
        // Estimated shortest response time of all invokers
        long shortestResponse = Long.MAX_VALUE;
        // The number of invokers having the same estimated shortest response time
        int shortestCount = 0;
        // The index of invokers having the same estimated shortest response time
        int[] shortestIndexes = new int[length];
        // the weight of every invokers
        int[] weights = new int[length];
        // The sum of the warmup weights of all the shortest response  invokers
        int totalWeight = 0;
        // The weight of the first shortest response invokers
        int firstWeight = 0;
        // Every shortest response invoker has the same weight value?
        boolean sameWeight = true;

        // Filter out all the shortest response invokers
        for (int i = 0; i < length; i++) {
            Invoker<T> invoker = invokers.get(i);
            RpcStatus rpcStatus = RpcStatus.getStatus(invoker.getUrl(), invocation.getMethodName());
            // Calculate the estimated response time from the product of active connections and succeeded average elapsed time.
            long succeededAverageElapsed = rpcStatus.getSucceededAverageElapsed();
            int active = rpcStatus.getActive() + 1;
            long estimateResponse = succeededAverageElapsed * active;
            int afterWarmup = getWeight(invoker, invocation);
            weights[i] = afterWarmup;
            // Same as LeastActiveLoadBalance
            if (estimateResponse < shortestResponse) {
                shortestResponse = estimateResponse;
                shortestCount = 1;
                shortestIndexes[0] = i;
                totalWeight = afterWarmup;
                firstWeight = afterWarmup;
                sameWeight = true;
            } else if (estimateResponse == shortestResponse) {
                shortestIndexes[shortestCount++] = i;
                totalWeight += afterWarmup;
                if (sameWeight && i > 0
                        && afterWarmup != firstWeight) {
                    sameWeight = false;
                }
            }
        }
        if (shortestCount == 1) {
            return invokers.get(shortestIndexes[0]);
        }
        if (!sameWeight && totalWeight > 0) {
            int offsetWeight = ThreadLocalRandom.current().nextInt(totalWeight);
            for (int i = 0; i < shortestCount; i++) {
                int shortestIndex = shortestIndexes[i];
                offsetWeight -= weights[shortestIndex];
                if (offsetWeight < 0) {
                    return invokers.get(shortestIndex);
                }
            }
        }
        return invokers.get(shortestIndexes[ThreadLocalRandom.current().nextInt(shortestCount)]);
    }

源码中也说明了,这个和最少活跃数的逻辑差不多,只是吧活跃数改成了响应时长。响应时长是按照每个服务提供方的平均响应时间与活跃数的乘积来算。不做过多说明,可以看看这篇文章: ​​​​​​Dubbo的几个负载均衡类--最少活跃数

总结:最短响应时长和最少活跃数的相关数据都不是实时更新的,而是根据服务提供方的实际响应在后台异步更新的。所以会有一定时间的延迟

 文章来源地址https://www.toymoban.com/news/detail-816452.html

李梓萌:

[1,2,3]:出来的leastIndexes数组只有一个:[1,0,0],直接取第一个。(只走一次第一个if,后面两不符合条件)

[1,1,1]:出来的leastIndexes数组有三个:[1,1,1],如果权重再一样,随机取一个;如果权重不一样,3内随机一个权重值比如2,那么就选第三个(注意条件是:offsetWeight<0)(第一个1走的第一个if,后面两1,都是走的第二个if)

[2,1,2]:出来的leastIndexes数组有一个:[1,0,0],直接取第一个(走了两次第一个if,最后那个2不符合条件)

[2,2,1]:出来的leastIndexes数组有两个:[1,2,0],直接取第一个(先走了第一个if,再走第二个if,第三个1又走了第一个if)

[2,3,3,2,1]:出来的leastIndexes数组有两个:[1,2,0,0,0],直接取第一个(同上,中间3不符合条件)
 

 

到了这里,关于Dubbo的几个负载均衡类--最短响应时间的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 中间件(二)dubbo负载均衡介绍

    支持轮询、随机、一致性hash和最小活跃数等。 ① sequences:内部的序列计数器 ② 服务器接口方法权重一样:(sequences+1)%服务器的数量=(决定调用)哪个服务器的服务。 ③ 服务器接口方法权重不一样:找到最大权重(权重数)%(sequences+1),然后找出权重比该取模后的值

    2024年02月12日
    浏览(27)
  • Dubbo负载均衡策略之 一致性哈希

    本文主要讲解了一致性哈希算法的原理以及其存在的数据倾斜的问题,然后引出解决数据倾斜问题的方法,最后分析一致性哈希算法在Dubbo中的使用。通过这篇文章,可以了解到一致性哈希算法的原理以及这种算法存在的问题和解决方案。 在这里引用dubbo官网的一段话——

    2024年02月08日
    浏览(53)
  • 2.1: Dubbo的基本应用-负载均衡,集群容错,服务降级

    官网地址:  http://dubbo.apache.org/zh/docs/v2.7/user/examples/loadbalance/ 如果在消费端和服务端都配置了负载均衡策略,   以消费端为准。 这其中比较难理解的就是最少活跃调用数是如何进行统计的? 讲道理 ,   最少活跃数应该是在 服务提供者端 进行统计的,   服务提供者统计有

    2024年02月10日
    浏览(25)
  • dubbo源码中设计模式——负载均衡中模版模式的应用

    在模板模式(Template Pattern)中,一个抽象类公开定义了执行它的方法的方式/模板。它的子类可以按需要重写方法实现,但调用将以抽象类中定义的方式进行。这种类型的设计模式属于行为型模式。 使用场景:定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。模板

    2024年02月19日
    浏览(38)
  • Dubbo负载均衡策略之 一致性哈希 | 京东云技术团队

    本文主要讲解了一致性哈希算法的原理以及其存在的数据倾斜的问题,然后引出解决数据倾斜问题的方法,最后分析一致性哈希算法在Dubbo中的使用。通过这篇文章,可以了解到一致性哈希算法的原理以及这种算法存在的问题和解决方案。 在这里引用dubbo官网的一段话——

    2024年02月08日
    浏览(39)
  • Java实习面试经验汇总,Dubbo-负载均衡原理解析,TCP的三次握手、四次挥手

    if (sameWeight i 0 !weight.equals(weights[i - 1])) { sameWeight = false; } } Integer sequenceNum = Sequence.getAndIncrement(); Integer offset = sequenceNum % totalWeight; offset = offset == 0 ? totalWeight : offset; if (!sameWeight) { for (String ip : ServerIps.WEIGHT_LIST.keySet()) { Integer weight = ServerIps.WEIGHT_LIST.get(ip); if (offset = weight) { ret

    2024年04月25日
    浏览(33)
  • 微服务间请求响应定义方式对比 (Apache Dubbo, IDL 定义,RESTful API)

    1. Apache Dubbo 分布式RPC Apache Dubbo 是一种分布式服务框架,它提供了一种透明的 RPC 机制,可以让服务之间像调用本地方法一样进行调用。Dubbo 使用 IDL 来定义服务接口,并使用各种协议(如 HTTP、Dubbo 协议等)进行通信。 优点: 透明化调用:无需关心服务部署细节,就像调用本

    2024年02月19日
    浏览(36)
  • nginx负载均衡的几种配置方式介绍

    目录 一.负载均衡含义简介 二.nginx负载均衡配置方式 准备三台设备: 2.190均衡服务器,2.191web服务器1,2.160web服务器2,三台设备均安装nginx,两台web服务器均有网页内容 upstream内参数 1.一般轮询负载均衡 (1)含义 (2)配置 (3)测试 2.加权轮询负载均衡 (1)含义 (2)配置

    2024年02月13日
    浏览(36)
  • 趁同事上厕所的时间,看完了 Dubbo SPI 的源码,瞬间觉得 JDK SPI 不香了

    👏作者简介:大家好,我是爱敲代码的小黄,独角兽企业的Java开发工程师,CSDN博客专家,阿里云专家博主 📕系列专栏:Java设计模式、Spring源码系列、Netty源码系列、Kafka源码系列、JUC源码系列、duubo源码系列 🔥如果感觉博主的文章还不错的话,请👍三连支持👍一下博主哦

    2024年02月16日
    浏览(29)
  • 【Spring Cloud】Ribbon 中的几种负载均衡策略

    负载均衡通常有两种实现手段,一种是服务端负载均衡器,另一种是客户端负载均衡器,而我们今天的主角 Ribbon 就属于后者——客户端负载均衡器。 服务端负载均衡器的问题是,它提供了更强的流量控制权,但无法满足不同的消费者希望使用不同负载均衡策略的需求,而使

    2024年02月15日
    浏览(54)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包