自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

談?wù)凞ubbo負(fù)載均衡是如何實現(xiàn)的?

開源
在一個截面上碰撞的概率高,但調(diào)用量越大分布越均勻,而且按概率使用權(quán)重后也比較均勻,有利于動態(tài)調(diào)整提供者權(quán)重。

 [[276413]]

 

談?wù)刣ubbo負(fù)載均衡是如何實現(xiàn)的?

 

 

dubbo的負(fù)載均衡全部由AbstractLoadBalance的子類來實現(xiàn)

RandomLoadBalance 隨機

在一個截面上碰撞的概率高,但調(diào)用量越大分布越均勻,而且按概率使用權(quán)重后也比較均勻,有利于動態(tài)調(diào)整提供者權(quán)重。

  • 獲取invoker的數(shù)量
  • 獲取第一個invoker的權(quán)重,并復(fù)制給firstWeight
  • 循環(huán)invoker集合,把它們的權(quán)重全部相加,并復(fù)制給totalWeight,如果權(quán)重不相等,那么sameWeight為false
  • 如果invoker集合的權(quán)重并不是全部相等的,那么獲取一個隨機數(shù)在1到totalWeight之間,賦值給offset屬性
  • 循環(huán)遍歷invoker集合,獲取權(quán)重并與offset相減,當(dāng)offset減到小于零,那么就返回這個inovker
  • 如果權(quán)重相等,那么直接在invoker集合里面取一個隨機數(shù)返回
  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 輪詢

存在慢的提供者累積請求的問題,比如:第二臺機器很慢,但沒掛,當(dāng)請求調(diào)到第二臺時就卡在那,久而久之,所有請求都卡在調(diào)到第二臺上。

在老的版本上,dubbo會求出最大權(quán)重和最小權(quán)重,如果權(quán)重相等,那么就直接按取模的方式,每次取完后值加一;如果權(quán)重不相等,順序根據(jù)權(quán)重分配。

在新的版本上,對這個類進(jìn)行了重構(gòu)。

  1. 從methodWeightMap這個實例中根據(jù)ServiceKey+MethodName的方式獲取里面的一個map實例,如果沒有則說明第一次進(jìn)到該方法,則實例化一個放入到methodWeightMap中,并把獲取到的實例命名為map
  2. 遍歷所有的invokers
  3. 拿到當(dāng)前的invoker的identifyString作為key,去map里獲取weightedRoundRobin實例,如果map里沒有則添加一個
  4. 如果weightedRoundRobin的權(quán)重和當(dāng)前invoker的權(quán)重不同,說明權(quán)重變了,需要重新設(shè)置
  5. 獲取當(dāng)前invoker所對應(yīng)的weightedRoundRobin實例中的current,并加上當(dāng)前invoker的權(quán)重
  6. 設(shè)置weightedRoundRobin最后的更新時間
  7. maxCurrent一開始是設(shè)置的0,如果當(dāng)前的weightedRoundRobin的current值大于maxCurrent則進(jìn)行賦值
  8. 遍歷完后會得到最大的權(quán)重的invoker的selectedInvoker和這個invoker所對應(yīng)的weightedRoundRobin賦值給了selectedWRR,還有權(quán)重之和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. 那么第一次調(diào)用的時候: 
  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. 調(diào)用 selectedWRR.sel(totalWeight);  
  13. 10.0.0.1:20888, weight=4 selectedWRR -> current = -5 
  14. 返回10.0.0.1:20888這個實例 
  15. 那么第二次調(diào)用的時候: 
  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. 調(diào)用 selectedWRR.sel(totalWeight);  
  21. 10.0.0.1:20886 , weight=4 selectedWRR -> current = -3 
  22. 返回10.0.0.1:20886這個實例 
  23. 那么第三次調(diào)用的時候: 
  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. 調(diào)用 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(falsetrue)) { 
  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 最少活躍調(diào)用數(shù)

使慢的提供者收到更少請求,因為越慢的提供者的調(diào)用前后計數(shù)差會越大。

  1. 遍歷所有的invoker
  2. 獲取當(dāng)前invoker的活躍數(shù),調(diào)用的是RpcStatus的getStatus方法,過濾器里面會記錄每個方法的活躍數(shù)
  3. 獲取當(dāng)前invoker的權(quán)重
  4. 如果是第一次進(jìn)來或者是當(dāng)前invoker的活躍數(shù)比最小的活躍數(shù)還小
  5. 那么把leastActive設(shè)置為當(dāng)前invoker的活躍數(shù),設(shè)置leastCount為1,leastIndexes數(shù)組的第一個位置設(shè)置為1,記錄一下totalWeight和firstWeight
  6. 如果不滿足第4點的條件,那么判斷當(dāng)前invoker的活躍數(shù)和最小的活躍數(shù)是否相等
  7. 如果滿足第6點,那么把當(dāng)前的權(quán)重加入到totalWeight中,并把leastIndexes數(shù)組中記錄一下最小活躍數(shù)相同的下標(biāo);再看一下是否所有的權(quán)重相同
  8. 如果invoker集合中只有一個invoker活躍數(shù)是最小的,那么直接返回
  9. 如果權(quán)重不相等,隨機權(quán)重后,判斷在哪個 Invoker 的權(quán)重區(qū)間中
  10. 權(quán)重相等,直接隨機選擇 Invoker 即可
  1. 最小活躍數(shù)算法實現(xiàn): 
  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. 假設(shè)offset=1(即random.nextInt(5)=1) 
  14. 1-2=-1<0?是,所以選中 10.0.0.1:20884, weight=2 
  15. 假設(shè)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(); // 總個數(shù) 
  26. 10: int leastActive = -1; // 最小的活躍數(shù) 
  27. 11: int leastCount = 0; // 相同最小活躍數(shù)的個數(shù) 
  28. 12: int[] leastIndexes = new int[length]; // 相同最小活躍數(shù)的下標(biāo) 
  29. 13: int totalWeight = 0; // 總權(quán)重 
  30. 14: int firstWeight = 0; // 第一個權(quán)重,用于于計算是否相同 
  31. 15: boolean sameWeight = true; // 是否所有權(quán)重相同 
  32. 16: // 計算獲得相同最小活躍數(shù)的數(shù)組和個數(shù) 
  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(); // 活躍數(shù) 
  36. 20: int weight = invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT); // 權(quán)重 
  37. 21: if (leastActive == -1 || active < leastActive) { // 發(fā)現(xiàn)更小的活躍數(shù),重新開始 
  38. 22: leastActive = active; // 記錄最小活躍數(shù) 
  39. 23: leastCount = 1; // 重新統(tǒng)計相同最小活躍數(shù)的個數(shù) 
  40. 24: leastIndexes[0] = i; // 重新記錄最小活躍數(shù)下標(biāo) 
  41. 25: totalWeight = weight; // 重新累計總權(quán)重 
  42. 26: firstWeight = weight; // 記錄第一個權(quán)重 
  43. 27: sameWeight = true; // 還原權(quán)重相同標(biāo)識 
  44. 28: } else if (active == leastActive) { // 累計相同最小的活躍數(shù) 
  45. 29: leastIndexes[leastCount++] = i; // 累計相同最小活躍數(shù)下標(biāo) 
  46. 30: totalWeight += weight; // 累計總權(quán)重 
  47. 31: // 判斷所有權(quán)重是否一樣 
  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: // 如果權(quán)重不相同且權(quán)重大于0則按總權(quán)重數(shù)隨機 
  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: // 如果權(quán)重相同或權(quán)重為0則均等隨機 
  71. 55: return invokers.get(leastIndexes[random.nextInt(leastCount)]); 
  72. 56: } 
  73. 57:  
  74. 58: } 

ConsistentHashLoadBalance 一致性 Hash

相同參數(shù)的請求總是發(fā)到同一提供者。當(dāng)某一臺提供者掛時,原本發(fā)往該提供者的請求,基于虛擬節(jié)點,平攤到其它提供者,不會引起劇烈變動。

  1. 基于 invokers 集合,根據(jù)對象內(nèi)存地址來計算定義哈希值
  2. 獲得 ConsistentHashSelector 對象。若為空,或者定義哈希值變更(說明 invokers 集合發(fā)生變化),進(jìn)行創(chuàng)建新的 ConsistentHashSelector 對象
  3. 調(diào)用ConsistentHashSelector對象的select方法
  1. 1: public class ConsistentHashLoadBalance extends AbstractLoadBalance { 
  2.  2:  
  3.  3: /** 
  4.  4: * 服務(wù)方法與一致性哈希選擇器的映射 
  5.  5: * 
  6.  6: * KEY:serviceKey + "." + methodName 
  7.  7: */ 
  8.  8: private final ConcurrentMap<String, ConsistentHashSelector<?>> selectors = new ConcurrentHashMap<String, ConsistentHashSelector<?>>(); 
  9.  9:  
  10. 10: @SuppressWarnings("unchecked"
  11. 11: @Override 
  12. 12: protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) { 
  13. 13: String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName(); 
  14. 14: // 基于 invokers 集合,根據(jù)對象內(nèi)存地址來計算定義哈希值 
  15. 15: int identityHashCode = System.identityHashCode(invokers); 
  16. 16: // 獲得 ConsistentHashSelector 對象。若為空,或者定義哈希值變更(說明 invokers 集合發(fā)生變化),進(jìn)行創(chuàng)建新的 ConsistentHashSelector 對象 
  17. 17: ConsistentHashSelector<T> selector = (ConsistentHashSelector<T>) selectors.get(key); 
  18. 18: if (selector == null || selector.identityHashCode != identityHashCode) { 
  19. 19: selectors.put(key, new ConsistentHashSelector<T>(invokers, invocation.getMethodName(), identityHashCode)); 
  20. 20: selector = (ConsistentHashSelector<T>) selectors.get(key); 
  21. 21: } 
  22. 22: return selector.select(invocation); 
  23. 23: } 
  24. 24: } 

ConsistentHashSelector 一致性哈希選擇器

ConsistentHashSelector ,是 ConsistentHashLoadBalance 的內(nèi)部類,一致性哈希選擇器,基于 Ketama 算法。

  1. /** 
  2.  * 虛擬節(jié)點與 Invoker 的映射關(guān)系 
  3.  */ 
  4. private final TreeMap<Long, Invoker<T>> virtualInvokers; 
  5. /** 
  6.  * 每個Invoker 對應(yīng)的虛擬節(jié)點數(shù) 
  7.  */ 
  8. private final int replicaNumber; 
  9. /** 
  10.  * 定義哈希值 
  11.  */ 
  12. private final int identityHashCode; 
  13. /** 
  14.  * 取值參數(shù)位置數(shù)組 
  15.  */ 
  16. private final int[] argumentIndex; 
  17.  1: ConsistentHashSelector(List<Invoker<T>> invokers, String methodName, int identityHashCode) { 
  18.  2: this.virtualInvokers = new TreeMap<Long, Invoker<T>>(); 
  19.  3: // 設(shè)置 identityHashCode 
  20.  4: this.identityHashCode = identityHashCode; 
  21.  5: URL url = invokers.get(0).getUrl(); 
  22.  6: // 初始化 replicaNumber 
  23.  7: this.replicaNumber = url.getMethodParameter(methodName, "hash.nodes", 160); 
  24.  8: // 初始化 argumentIndex 
  25.  9: String[] index = Constants.COMMA_SPLIT_PATTERN.split(url.getMethodParameter(methodName, "hash.arguments""0")); 
  26.  10: argumentIndex = new int[index.length]; 
  27.  11: for (int i = 0; i < index.length; i++) { 
  28.  12: argumentIndex[i] = Integer.parseInt(index[i]); 
  29.  13: } 
  30.  14: // 初始化 virtualInvokers 
  31.  15: for (Invoker<T> invoker : invokers) { 
  32.  16: String address = invoker.getUrl().getAddress(); 
  33.  17: // 每四個虛擬結(jié)點為一組,為什么這樣?下面會說到 
  34.  18: for (int i = 0; i < replicaNumber / 4; i++) { 
  35.  19: // 這組虛擬結(jié)點得到惟一名稱 
  36.  20: byte[] digest = md5(address + i); 
  37.  21: // Md5是一個16字節(jié)長度的數(shù)組,將16字節(jié)的數(shù)組每四個字節(jié)一組,分別對應(yīng)一個虛擬結(jié)點,這就是為什么上面把虛擬結(jié)點四個劃分一組的原因 
  38.  22: for (int h = 0; h < 4; h++) { 
  39.  23: // 對于每四個字節(jié),組成一個long值數(shù)值,做為這個虛擬節(jié)點的在環(huán)中的惟一key 
  40.  24: long m = hash(digest, h); 
  41.  25: virtualInvokers.put(m, invoker); 
  42.  26: } 
  43.  27: } 
  44.  28: } 
  45.  29: } 
  46. public Invoker<T> select(Invocation invocation) { 
  47.  // 基于方法參數(shù),獲得 KEY 
  48.  String key = toKey(invocation.getArguments()); 
  49.  // 計算 MD5 值 
  50.  byte[] digest = md5(key); 
  51.  // 計算 KEY 值 
  52.  return selectForKey(hash(digest, 0)); 
  53. private String toKey(Object[] args) { 
  54.  StringBuilder buf = new StringBuilder(); 
  55.  for (int i : argumentIndex) { 
  56.  if (i >= 0 && i < args.length) { 
  57.  buf.append(args[i]); 
  58.  } 
  59.  } 
  60.  return buf.toString(); 
  61. private Invoker<T> selectForKey(long hash) { 
  62.  // 得到大于當(dāng)前 key 的那個子 Map ,然后從中取出第一個 key ,就是大于且離它最近的那個 key 
  63.  Map.Entry<Long, Invoker<T>> entry = virtualInvokers.tailMap(hash, true).firstEntry(); 
  64.  // 不存在,則取 virtualInvokers 第一個 
  65.  if (entry == null) { 
  66.  entry = virtualInvokers.firstEntry(); 
  67.  } 
  68.  // 存在,則返回 
  69.  return entry.getValue(); 

 

 

責(zé)任編輯:武曉燕 來源: 今日頭條
相關(guān)推薦

2025-04-15 10:00:00

Feign負(fù)載均衡微服務(wù)

2010-05-10 17:52:30

實現(xiàn)負(fù)載均衡

2019-06-09 09:13:14

Istio負(fù)載均衡架構(gòu)

2023-02-13 16:39:45

Kubernetes容器負(fù)載均衡器

2019-11-07 08:49:26

Apache Dubb架構(gòu)負(fù)載均衡

2023-10-25 22:23:35

Dubbo路由

2021-08-23 06:59:22

Nacos負(fù)載均衡客戶端

2010-05-10 15:22:57

實現(xiàn)負(fù)載均衡

2010-04-20 14:31:29

負(fù)載均衡功能

2010-04-22 10:46:40

Lvs負(fù)載均衡故障負(fù)載均衡器

2010-04-28 12:38:38

負(fù)載均衡的概念

2012-10-19 10:21:07

數(shù)據(jù)庫負(fù)載均衡mssqlserver

2013-12-13 09:55:44

VDI負(fù)載均衡

2010-04-20 10:27:57

什么是負(fù)載均衡

2025-04-14 10:00:00

負(fù)載均衡Java開發(fā)

2017-07-03 08:08:25

負(fù)載均衡分類

2010-03-24 10:35:02

Nginx負(fù)載均衡器

2011-11-22 21:26:59

pfSense配置Web服務(wù)器負(fù)載均衡

2024-06-13 09:59:21

數(shù)據(jù)中心數(shù)字化

2024-07-17 08:36:53

點贊
收藏

51CTO技術(shù)棧公眾號