加入收藏 | 设为首页 | 会员中心 | 我要投稿 辽源站长网 (https://www.0437zz.com/)- 云专线、云连接、智能数据、边缘计算、数据安全!
当前位置: 首页 > 服务器 > 搭建环境 > Windows > 正文

谈谈Dubbo负载均衡是如何实现的?

发布时间:2019-09-10 16:18:42 所属栏目:Windows 来源:程序员界的彭于晏
导读:dubbo的负载均衡全部由AbstractLoadBalance的子类来实现 RandomLoadBalance 随机 在一个截面上碰撞的概率高,但调用量越大分布越均匀,而且按概率使用权重后也比较均匀,有利于动态调整提供者权重。 获取invoker的数量 获取第一个invoker的权重,并复制给f

使慢的提供者收到更少请求,因为越慢的提供者的调用前后计数差会越大。

  1. 遍历所有的invoker
  2. 获取当前invoker的活跃数,调用的是RpcStatus的getStatus方法,过滤器里面会记录每个方法的活跃数
  3. 获取当前invoker的权重
  4. 如果是第一次进来或者是当前invoker的活跃数比最小的活跃数还小
  5. 那么把leastActive设置为当前invoker的活跃数,设置leastCount为1,leastIndexes数组的第一个位置设置为1,记录一下totalWeight和firstWeight
  6. 如果不满足第4点的条件,那么判断当前invoker的活跃数和最小的活跃数是否相等
  7. 如果满足第6点,那么把当前的权重加入到totalWeight中,并把leastIndexes数组中记录一下最小活跃数相同的下标;再看一下是否所有的权重相同
  8. 如果invoker集合中只有一个invoker活跃数是最小的,那么直接返回
  9. 如果权重不相等,随机权重后,判断在哪个 Invoker 的权重区间中
  10. 权重相等,直接随机选择 Invoker 即可
  1. 最小活跃数算法实现: 
  2. 假定有3台dubbo provider: 
  3. 10.0.0.1:20884, weight=2,active=2 
  4. 10.0.0.1:20886, weight=3,active=4 
  5. 10.0.0.1:20888, weight=4,active=3 
  6. active=2最小,且只有一个2,所以选择10.0.0.1:20884 
  7. 假定有3台dubbo provider: 
  8. 10.0.0.1:20884, weight=2,active=2 
  9. 10.0.0.1:20886, weight=3,active=2 
  10. 10.0.0.1:20888, weight=4,active=3 
  11. active=2最小,且有2个,所以从[10.0.0.1:20884,10.0.0.1:20886 ]中选择; 
  12. 接下来的算法与随机算法类似: 
  13. 假设offset=1(即random.nextInt(5)=1) 
  14. 1-2=-1<0?是,所以选中 10.0.0.1:20884, weight=2 
  15. 假设offset=4(即random.nextInt(5)=4) 
  16. 4-2=2<0?否,这时候offset=2, 2-3<0?是,所以选中 10.0.0.1:20886, weight=3 
  17.  1: public class LeastActiveLoadBalance extends AbstractLoadBalance { 
  18.  2:  
  19.  3: public static final String NAME = "leastactive"; 
  20.  4:  
  21.  5: private final Random random = new Random(); 
  22.  6:  
  23.  7: @Override 
  24.  8: protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) { 
  25.  9: int length = invokers.size(); // 总个数 
  26. 10: int leastActive = -1; // 最小的活跃数 
  27. 11: int leastCount = 0; // 相同最小活跃数的个数 
  28. 12: int[] leastIndexes = new int[length]; // 相同最小活跃数的下标 
  29. 13: int totalWeight = 0; // 总权重 
  30. 14: int firstWeight = 0; // 第一个权重,用于于计算是否相同 
  31. 15: boolean sameWeight = true; // 是否所有权重相同 
  32. 16: // 计算获得相同最小活跃数的数组和个数 
  33. 17: for (int i = 0; i < length; i++) { 
  34. 18: Invoker<T> invoker = invokers.get(i); 
  35. 19: int active = RpcStatus.getStatus(invoker.getUrl(), invocation.getMethodName()).getActive(); // 活跃数 
  36. 20: int weight = invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT); // 权重 
  37. 21: if (leastActive == -1 || active < leastActive) { // 发现更小的活跃数,重新开始 
  38. 22: leastActive = active; // 记录最小活跃数 
  39. 23: leastCount = 1; // 重新统计相同最小活跃数的个数 
  40. 24: leastIndexes[0] = i; // 重新记录最小活跃数下标 
  41. 25: totalWeight = weight; // 重新累计总权重 
  42. 26: firstWeight = weight; // 记录第一个权重 
  43. 27: sameWeight = true; // 还原权重相同标识 
  44. 28: } else if (active == leastActive) { // 累计相同最小的活跃数 
  45. 29: leastIndexes[leastCount++] = i; // 累计相同最小活跃数下标 
  46. 30: totalWeight += weight; // 累计总权重 
  47. 31: // 判断所有权重是否一样 
  48. 32: if (sameWeight && weight != firstWeight) { 
  49. 33: sameWeight = false; 
  50. 34: } 
  51. 35: } 
  52. 36: } 
  53. 37: // assert(leastCount > 0) 
  54. 38: if (leastCount == 1) { 
  55. 39: // 如果只有一个最小则直接返回 
  56. 40: return invokers.get(leastIndexes[0]); 
  57. 41: } 
  58. 42: if (!sameWeight && totalWeight > 0) { 
  59. 43: // 如果权重不相同且权重大于0则按总权重数随机 
  60. 44: int offsetWeight = random.nextInt(totalWeight); 
  61. 45: // 并确定随机值落在哪个片断上 
  62. 46: for (int i = 0; i < leastCount; i++) { 
  63. 47: int leastIndex = leastIndexes[i]; 
  64. 48: offsetWeight -= getWeight(invokers.get(leastIndex), invocation); 
  65. 49: if (offsetWeight <= 0) { 
  66. 50: return invokers.get(leastIndex); 
  67. 51: } 
  68. 52: } 
  69. 53: } 
  70. 54: // 如果权重相同或权重为0则均等随机 
  71. 55: return invokers.get(leastIndexes[random.nextInt(leastCount)]); 
  72. 56: } 
  73. 57:  
  74. 58: } 

ConsistentHashLoadBalance 一致性 Hash

(编辑:辽源站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读