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

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

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

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

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

dubbo的负载均衡全部由AbstractLoadBalance的子类来实现

RandomLoadBalance 随机

在一个截面上碰撞的概率高,但调用量越大分布越均匀,而且按概率使用权重后也比较均匀,有利于动态调整提供者权重。

  • 获取invoker的数量
  • 获取第一个invoker的权重,并复制给firstWeight
  • 循环invoker集合,把它们的权重全部相加,并复制给totalWeight,如果权重不相等,那么sameWeight为false
  • 如果invoker集合的权重并不是全部相等的,那么获取一个随机数在1到totalWeight之间,赋值给offset属性
  • 循环遍历invoker集合,获取权重并与offset相减,当offset减到小于零,那么就返回这个inovker
  • 如果权重相等,那么直接在invoker集合里面取一个随机数返回
  1. @Override 
  2.  protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) { 
  3.  int length = invokers.size(); // Number of invokers 
  4.  boolean sameWeight = true; // Every invoker has the same weight? 
  5.  int firstWeight = getWeight(invokers.get(0), invocation); 
  6.  int totalWeight = firstWeight; // The sum of weights 
  7.  for (int i = 1; i < length; i++) { 
  8.  int weight = getWeight(invokers.get(i), invocation); 
  9.  totalWeight += weight; // Sum 
  10.  if (sameWeight && weight != firstWeight) { 
  11.  sameWeight = false; 
  12.  } 
  13.  } 
  14.  if (totalWeight > 0 && !sameWeight) { 
  15.  // If (not every invoker has the same weight & at least one invoker's weight>0), select randomly based on totalWeight. 
  16.  int offset = ThreadLocalRandom.current().nextInt(totalWeight); 
  17.  // Return a invoker based on the random value. 
  18.  for (int i = 0; i < length; i++) { 
  19.  offset -= getWeight(invokers.get(i), invocation); 
  20.  if (offset < 0) { 
  21.  return invokers.get(i); 
  22.  } 
  23.  } 
  24.  } 
  25.  // If all invokers have the same weight value or totalWeight=0, return evenly. 
  26.  return invokers.get(ThreadLocalRandom.current().nextInt(length)); 
  27.  } 

RoundRobinLoadBalance 轮询

存在慢的提供者累积请求的问题,比如:第二台机器很慢,但没挂,当请求调到第二台时就卡在那,久而久之,所有请求都卡在调到第二台上。

在老的版本上,dubbo会求出最大权重和最小权重,如果权重相等,那么就直接按取模的方式,每次取完后值加一;如果权重不相等,顺序根据权重分配。

在新的版本上,对这个类进行了重构。

  1. 从methodWeightMap这个实例中根据ServiceKey+MethodName的方式获取里面的一个map实例,如果没有则说明第一次进到该方法,则实例化一个放入到methodWeightMap中,并把获取到的实例命名为map
  2. 遍历所有的invokers
  3. 拿到当前的invoker的identifyString作为key,去map里获取weightedRoundRobin实例,如果map里没有则添加一个
  4. 如果weightedRoundRobin的权重和当前invoker的权重不同,说明权重变了,需要重新设置
  5. 获取当前invoker所对应的weightedRoundRobin实例中的current,并加上当前invoker的权重
  6. 设置weightedRoundRobin最后的更新时间
  7. maxCurrent一开始是设置的0,如果当前的weightedRoundRobin的current值大于maxCurrent则进行赋值
  8. 遍历完后会得到最大的权重的invoker的selectedInvoker和这个invoker所对应的weightedRoundRobin赋值给了selectedWRR,还有权重之和totalWeight
  9. 然后把selectedWRR里的current属性减去totalWeight,并返回selectedInvoker

这样看显然是不够清晰的,我们来举个例子:

  1. 假定有3台dubbo provider: 
  2. 10.0.0.1:20884, weight=2 
  3. 10.0.0.1:20886, weight=3 
  4. 10.0.0.1:20888, weight=4 
  5. totalWeight=9; 
  6. 那么第一次调用的时候: 
  7. 10.0.0.1:20884, weight=2 selectedWRR -> current = 2 
  8. 10.0.0.1:20886, weight=3 selectedWRR -> current = 3 
  9. 10.0.0.1:20888, weight=4 selectedWRR -> current = 4 
  10.   
  11. selectedInvoker-> 10.0.0.1:20888  
  12. 调用 selectedWRR.sel(totalWeight);  
  13. 10.0.0.1:20888, weight=4 selectedWRR -> current = -5 
  14. 返回10.0.0.1:20888这个实例 
  15. 那么第二次调用的时候: 
  16. 10.0.0.1:20884, weight=2 selectedWRR -> current = 4 
  17. 10.0.0.1:20886, weight=3 selectedWRR -> current = 6 
  18. 10.0.0.1:20888, weight=4 selectedWRR -> current = -1 
  19. selectedInvoker-> 10.0.0.1:20886  
  20. 调用 selectedWRR.sel(totalWeight);  
  21. 10.0.0.1:20886 , weight=4 selectedWRR -> current = -3 
  22. 返回10.0.0.1:20886这个实例 
  23. 那么第三次调用的时候: 
  24. 10.0.0.1:20884, weight=2 selectedWRR -> current = 6 
  25. 10.0.0.1:20886, weight=3 selectedWRR -> current = 0 
  26. 10.0.0.1:20888, weight=4 selectedWRR -> current = 3 
  27. selectedInvoker-> 10.0.0.1:20884 
  28. 调用 selectedWRR.sel(totalWeight);  
  29. 10.0.0.1:20884, weight=2 selectedWRR -> current = -3 
  30. 返回10.0.0.1:20884这个实例 
  31.  protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) { 
  32.  String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName(); 
  33.  ConcurrentMap<String, WeightedRoundRobin> map = methodWeightMap.get(key); 
  34.  if (map == null) { 
  35.  methodWeightMap.putIfAbsent(key, new ConcurrentHashMap<String, WeightedRoundRobin>()); 
  36.  map = methodWeightMap.get(key); 
  37.  } 
  38.  int totalWeight = 0; 
  39.  long maxCurrent = Long.MIN_VALUE; 
  40.  long now = System.currentTimeMillis(); 
  41.  Invoker<T> selectedInvoker = null; 
  42.  WeightedRoundRobin selectedWRR = null; 
  43.  for (Invoker<T> invoker : invokers) { 
  44.  String identifyString = invoker.getUrl().toIdentityString(); 
  45.  WeightedRoundRobin weightedRoundRobin = map.get(identifyString); 
  46.  int weight = getWeight(invoker, invocation); 
  47.  if (weight < 0) { 
  48.  weight = 0; 
  49.  } 
  50.  if (weightedRoundRobin == null) { 
  51.  weightedRoundRobin = new WeightedRoundRobin(); 
  52.  weightedRoundRobin.setWeight(weight); 
  53.  map.putIfAbsent(identifyString, weightedRoundRobin); 
  54.  weightedRoundRobin = map.get(identifyString); 
  55.  } 
  56.  if (weight != weightedRoundRobin.getWeight()) { 
  57.  //weight changed 
  58.  weightedRoundRobin.setWeight(weight); 
  59.  } 
  60.  long cur = weightedRoundRobin.increaseCurrent(); 
  61.  weightedRoundRobin.setLastUpdate(now); 
  62.  if (cur > maxCurrent) { 
  63.  maxCurrent = cur; 
  64.  selectedInvoker = invoker; 
  65.  selectedWRR = weightedRoundRobin; 
  66.  } 
  67.  totalWeight += weight; 
  68.  } 
  69.  if (!updateLock.get() && invokers.size() != map.size()) { 
  70.  if (updateLock.compareAndSet(false, true)) { 
  71.  try { 
  72.  // copy -> modify -> update reference 
  73.  ConcurrentMap<String, WeightedRoundRobin> newMap = new ConcurrentHashMap<String, WeightedRoundRobin>(); 
  74.  newMap.putAll(map); 
  75.  Iterator<Entry<String, WeightedRoundRobin>> it = newMap.entrySet().iterator(); 
  76.  while (it.hasNext()) { 
  77.  Entry<String, WeightedRoundRobin> item = it.next(); 
  78.  if (now - item.getValue().getLastUpdate() > RECYCLE_PERIOD) { 
  79.  it.remove(); 
  80.  } 
  81.  } 
  82.  methodWeightMap.put(key, newMap); 
  83.  } finally { 
  84.  updateLock.set(false); 
  85.  } 
  86.  } 
  87.  } 
  88.  if (selectedInvoker != null) { 
  89.  selectedWRR.sel(totalWeight); 
  90.  return selectedInvoker; 
  91.  } 
  92.  // should not happen here 
  93.  return invokers.get(0); 
  94.  } 

LeastActiveLoadBalance 最少活跃调用数

(编辑:辽源站长网)

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

推荐文章
    热点阅读