当前位置:   article > 正文

dubbo的轮询机制说明_dubbo轮训实现

dubbo轮训实现

针对dubbo的文章已经很多,

我们这里通过代码的解析,简单描述下各种负载均衡的实现方式

随机负载均衡(RandomLoadBalance:先统计所有服务器上该接口方法的权重总和,然后对这个总和随机nextInt一下,看生成的随机数落到哪个段内,就调哪个服务器上的该服务。

  1. protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
  2.         int length = invokers.size(); // 总个数
  3.         int totalWeight = 0; // 总权重
  4.         boolean sameWeight = true; // 权重是否都一样
  5.         for (int i = 0; i < length; i++) {
  6.             int weight = getWeight(invokers.get(i), invocation);
  7.             totalWeight += weight; // 累计总权重
  8.             if (sameWeight && i > 0
  9.                     && weight != getWeight(invokers.get(i - 1), invocation)) {
  10.                 sameWeight = false; // 计算所有权重是否一样
  11.             }
  12.         }
  13.         if (totalWeight > 0 && ! sameWeight) {
  14.             // 如果权重不相同且权重大于0则按总权重数随机
  15.             int offset = random.nextInt(totalWeight);
  16.             // 并确定随机值落在哪个片断上
  17.             for (int i = 0; i < length; i++) {
  18.                 offset -= getWeight(invokers.get(i), invocation);
  19.                 if (offset < 0) {
  20.                     return invokers.get(i);
  21.                 }
  22.             }
  23.         }
  24.         // 如果权重相同或权重为0则均等随机
  25.         return invokers.get(random.nextInt(length));
  26.     }
轮询负载均衡( RoundRobinLoadBalance ): 如果所有服务器的该接口方法的权重一样,则直接内部的序列计数器( sequences +1 然后对服务器的数量进行取模来决定调用哪个服务器上的服务;如果服务器的该接口方法的权重不一样(就是说存在预热中的服务器),则找到其中最大的权重,然后将内部的权重计数器( weightSequences +1 并对该最大权重数取模,然后再找出权重比该取模后的值大服务器列表,最后通过内部的序列计数器( sequences +1 然后对服务器列表数量进行取模来决定调用哪个服务器上的服务。

  1.   protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
  2.         String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName();
  3.         int length = invokers.size(); // 总个数
  4.         int maxWeight = 0; // 最大权重
  5.         int minWeight = Integer.MAX_VALUE; // 最小权重
  6.         for (int i = 0; i < length; i++) {
  7.             int weight = getWeight(invokers.get(i), invocation);
  8.             maxWeight = Math.max(maxWeight, weight); // 累计最大权重
  9.             minWeight = Math.min(minWeight, weight); // 累计最小权重
  10.         }
  11.         if (maxWeight > 0 && minWeight < maxWeight) { // 权重不一样
  12.             AtomicPositiveInteger weightSequence = weightSequences.get(key);
  13.             if (weightSequence == null) {
  14.                 weightSequences.putIfAbsent(key, new AtomicPositiveInteger());
  15.                 weightSequence = weightSequences.get(key);
  16.             }
  17.             int currentWeight = weightSequence.getAndIncrement() % maxWeight;
  18.             List<Invoker<T>> weightInvokers = new ArrayList<Invoker<T>>();
  19.             for (Invoker<T> invoker : invokers) { // 筛选权重大于当前权重基数的Invoker
  20.                 if (getWeight(invoker, invocation) > currentWeight) {
  21.                     weightInvokers.add(invoker);
  22.                 }
  23.             }
  24.             int weightLength = weightInvokers.size();
  25.             if (weightLength == 1) {
  26.                 return weightInvokers.get(0);
  27.             } else if (weightLength > 1) {
  28.                 invokers = weightInvokers;
  29.                 length = invokers.size();
  30.             }
  31.         }
  32.         AtomicPositiveInteger sequence = sequences.get(key);
  33.         if (sequence == null) {
  34.             sequences.putIfAbsent(key, new AtomicPositiveInteger());
  35.             sequence = sequences.get(key);
  36.         }
  37.         // 取模轮循
  38.         return invokers.get(sequence.getAndIncrement() % length);
  39.     }
最少活跃负载均衡( LeastActiveLoadBalance :每个接口和接口方法都对应一个 RpcStatus 对象,记录了他们的活跃数、失败数等等相关统计信息,此种负载均衡方式是在活跃数最低的服务器中对其权重的总和取模来看结果是在哪个权重段中,则选择该服务器来调用,活跃数就像并发量降级中的计数器一样,开始调用时活跃数 +1 ,调用结束时活跃数 -1 ,所以活跃值越大,表明该提供者服务器的该接口方法耗时越长,而消费能力强的提供者接口往往活跃值很低。最少活跃负载均衡保证了“慢”提供者能接收到更少的服务器调用。

  1. protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
  2. int length = invokers.size(); // 总个数
  3. int leastActive = -1; // 最小的活跃数
  4. int leastCount = 0; // 相同最小活跃数的个数
  5. int[] leastIndexs = new int[length]; // 相同最小活跃数的下标
  6. int totalWeight = 0; // 总权重
  7. int firstWeight = 0; // 第一个权重,用于于计算是否相同
  8. boolean sameWeight = true; // 是否所有权重相同
  9. for (int i = 0; i < length; i++) {
  10. Invoker<T> invoker = invokers.get(i);
  11. int active = RpcStatus.getStatus(invoker.getUrl(), invocation.getMethodName()).getActive(); // 活跃数
  12. int weight = invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT); // 权重
  13. if (leastActive == -1 || active < leastActive) { // 发现更小的活跃数,重新开始
  14. leastActive = active; // 记录最小活跃数
  15. leastCount = 1; // 重新统计相同最小活跃数的个数
  16. leastIndexs[0] = i; // 重新记录最小活跃数下标
  17. totalWeight = weight; // 重新累计总权重
  18. firstWeight = weight; // 记录第一个权重
  19. sameWeight = true; // 还原权重相同标识
  20. } else if (active == leastActive) { // 累计相同最小的活跃数
  21. leastIndexs[leastCount ++] = i; // 累计相同最小活跃数下标
  22. totalWeight += weight; // 累计总权重
  23. // 判断所有权重是否一样
  24. if (sameWeight && i > 0
  25. && weight != firstWeight) {
  26. sameWeight = false;
  27. }
  28. }
  29. }
  30. // assert(leastCount > 0)
  31. if (leastCount == 1) {
  32. // 如果只有一个最小则直接返回
  33. return invokers.get(leastIndexs[0]);
  34. }
  35. if (! sameWeight && totalWeight > 0) {
  36. // 如果权重不相同且权重大于0则按总权重数随机
  37. int offsetWeight = random.nextInt(totalWeight);
  38. // 并确定随机值落在哪个片断上
  39. for (int i = 0; i < leastCount; i++) {
  40. int leastIndex = leastIndexs[i];
  41. offsetWeight -= getWeight(invokers.get(leastIndex), invocation);
  42. if (offsetWeight <= 0)
  43. return invokers.get(leastIndex);
  44. }
  45. }
  46. // 如果权重相同或权重为0则均等随机
  47. return invokers.get(leastIndexs[random.nextInt(leastCount)]);
  48. }

一致哈希负载均衡( ConsistentHashLoadBalance ): 一致性哈希算法的负载均衡保证了同样的请求(参数)将会落到同一台服务器上,这在某些场景是非常有用的, Dubbo 中默认采用了 160 个虚拟节点,因为 Dubbo 的请求 URL 中除了我们使用的参数,还有些额外的系统调用参数,比如 timestamp loadbalance pid application 等,有人可定会问, Dubbo 会对 URL 中哪些参数进行 hash Dubbo 默认除了对我们接口所有参数进行 hash 外,还会加上这些额外参数

  1. protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
  2. String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName();
  3. int identityHashCode = System.identityHashCode(invokers);
  4. ConsistentHashSelector<T> selector = (ConsistentHashSelector<T>) selectors.get(key);
  5. if (selector == null || selector.getIdentityHashCode() != identityHashCode) {
  6. selectors.put(key, new ConsistentHashSelector<T>(invokers, invocation.getMethodName(), identityHashCode));
  7. selector = (ConsistentHashSelector<T>) selectors.get(key);
  8. }
  9. return selector.select(invocation);
  10. }




声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
  

闽ICP备14008679号