当前位置:   article > 正文

dubbo中的一致性hash(ConsistentHashLoadBalance)详解_dubbo consistenthash

dubbo consistenthash

注意:本文适用于了解dubbo以及一致性hash的读者

众所周知,dubbo中有四种负载均衡策略:

别的负载均衡策略就不细说了,再这里重点说一下dubo中的一致性hash负载均衡:ConsistentHashLoadBalance


前面的流程不多说,我们直接进入ConsistentHashLoadBalance的doSelect()方法中:

  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. }

刚开始生成一个key,其路径示例如下:com.dubbo.common.service.app.AppManageService:2222.selectLastApp

然后根据生成的hashCode匹配对应的Selector(一般Selector的数量为对应的consumer的数量)

生成一致性hash的核心方法在ConsistentHashSelector的构造方法中:

  1. public ConsistentHashSelector(List<Invoker<T>> invokers, String methodName, int identityHashCode) {
  2. this.virtualInvokers = new TreeMap<Long, Invoker<T>>();
  3. this.identityHashCode = System.identityHashCode(invokers);
  4. URL url = invokers.get(0).getUrl();
  5. this.replicaNumber = url.getMethodParameter(methodName, "hash.nodes", 160);
  6. String[] index = Constants.COMMA_SPLIT_PATTERN.split(url.getMethodParameter(methodName, "hash.arguments", "0"));
  7. argumentIndex = new int[index.length];
  8. for (int i = 0; i < index.length; i ++) {
  9. argumentIndex[i] = Integer.parseInt(index[i]);
  10. }
  11. for (Invoker<T> invoker : invokers) {
  12. for (int i = 0; i < replicaNumber / 4; i++) {
  13. byte[] digest = md5(invoker.getUrl().toFullString() + i);
  14. for (int h = 0; h < 4; h++) {
  15. long m = hash(digest, h);
  16. virtualInvokers.put(m, invoker);
  17. }
  18. }
  19. }
  20. }

根据对应的Invokers生成对应的hash环虚拟节点,放在一个TreeMap的结构中,key为虚拟节点的hash值。这样就可以在TreeMap中有序排列,这样hash环就生成了,调用选择的方法如下:

  1. public Invoker<T> select(Invocation invocation) {
  2. String key = toKey(invocation.getArguments());
  3. byte[] digest = md5(key);
  4. Invoker<T> invoker = sekectForKey(hash(digest, 0));
  5. return invoker;
  6. }

以参数值通过md5加密的hash为参数,进入selectForKey方法:

  1. private Invoker<T> sekectForKey(long hash) {
  2. Invoker<T> invoker;
  3. Long key = hash;
  4. if (!virtualInvokers.containsKey(key)) {
  5. SortedMap<Long, Invoker<T>> tailMap = virtualInvokers.tailMap(key);
  6. if (tailMap.isEmpty()) {
  7. key = virtualInvokers.firstKey();
  8. } else {
  9. key = tailMap.firstKey();
  10. }
  11. }
  12. invoker = virtualInvokers.get(key);
  13. return invoker;
  14. }
以入参hash获取tailMap的第一个比较大的虚拟节点的Invoker,如果入参的hashcode最大则用第一个虚拟节点的Invoker。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/41331
推荐阅读
相关标签
  

闽ICP备14008679号