dubbo中的一致性hash(ConsistentHashLoadBalance)详解
2018-02-27 17:21
2606 查看
注意:本文适用于了解dubbo以及一致性hash的读者
众所周知,dubbo中有四种负载均衡策略:
别的负载均衡策略就不细说了,再这里重点说一下dubo中的一致性hash负载均衡:ConsistentHashLoadBalance
前面的流程不多说,我们直接进入ConsistentHashLoadBalance的doSelect()方法中: protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName();
int identityHashCode = System.identityHashCode(invokers);
ConsistentHashSelector<T> selector = (ConsistentHashSelector<T>) selectors.get(key);
if (selector == null || selector.getIdentityHashCode() != identityHashCode) {
selectors.put(key, new ConsistentHashSelector<T>(invokers, invocation.getMethodName(), identityHashCode));
selector = (ConsistentHashSelector<T>) selectors.get(key);
}
return selector.select(invocation);
}刚开始生成一个key,其路径示例如下:com.dubbo.common.service.app.AppManageService:2222.selectLastApp
然后根据生成的hashCode匹配对应的Selector(一般Selector的数量为对应的consumer的数量)
生成一致性hash的核心方法在ConsistentHashSelector的构造方法中: public ConsistentHashSelector(List<Invoker<T>> invokers, String methodName, int identityHashCode) {
this.virtualInvokers = new TreeMap<Long, Invoker<T>>();
this.identityHashCode = System.identityHashCode(invokers);
URL url = invokers.get(0).getUrl();
this.replicaNumber = url.getMethodParameter(methodName, "hash.nodes", 160);
String[] index = Constants.COMMA_SPLIT_PATTERN.split(url.getMethodParameter(methodName, "hash.arguments", "0"));
argumentIndex = new int[index.length];
for (int i = 0; i < index.length; i ++) {
argumentIndex[i] = Integer.parseInt(index[i]);
}
for (Invoker<T> invoker : invokers) {
for (int i = 0; i < replicaNumber / 4; i++) {
byte[] digest = md5(invoker.getUrl().toFullString() + i);
for (int h = 0; h < 4; h++) {
long m = hash(digest, h);
virtualInvokers.put(m, invoker);
}
}
}
}根据对应的Invokers生成对应的hash环虚拟节点,放在一个TreeMap的结构中,key为虚拟节点的hash值。这样就可以在TreeMap中有序排列,这样hash环就生成了,调用选择的方法如下: public Invoker<T> select(Invocation invocation) {
String key = toKey(invocation.getArguments());
byte[] digest = md5(key);
Invoker<T> invoker = sekectForKey(hash(digest, 0));
return invoker;
}以参数值通过md5加密的hash为参数,进入selectForKey方法: private Invoker<T> sekectForKey(long hash) {
Invoker<T> invoker;
Long key = hash;
if (!virtualInvokers.containsKey(key)) {
SortedMap<Long, Invoker<T>> tailMap = virtualInvokers.tailMap(key);
if (tailMap.isEmpty()) {
key = virtualInvokers.firstKey();
} else {
key = tailMap.firstKey();
}
}
invoker = virtualInvokers.get(key);
return invoker;
}以入参hash获取tailMap的第一个比较大的虚拟节点的Invoker,如果入参的hashcode最大则用第一个虚拟节点的Invoker。
众所周知,dubbo中有四种负载均衡策略:
别的负载均衡策略就不细说了,再这里重点说一下dubo中的一致性hash负载均衡:ConsistentHashLoadBalance
前面的流程不多说,我们直接进入ConsistentHashLoadBalance的doSelect()方法中: protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName();
int identityHashCode = System.identityHashCode(invokers);
ConsistentHashSelector<T> selector = (ConsistentHashSelector<T>) selectors.get(key);
if (selector == null || selector.getIdentityHashCode() != identityHashCode) {
selectors.put(key, new ConsistentHashSelector<T>(invokers, invocation.getMethodName(), identityHashCode));
selector = (ConsistentHashSelector<T>) selectors.get(key);
}
return selector.select(invocation);
}刚开始生成一个key,其路径示例如下:com.dubbo.common.service.app.AppManageService:2222.selectLastApp
然后根据生成的hashCode匹配对应的Selector(一般Selector的数量为对应的consumer的数量)
生成一致性hash的核心方法在ConsistentHashSelector的构造方法中: public ConsistentHashSelector(List<Invoker<T>> invokers, String methodName, int identityHashCode) {
this.virtualInvokers = new TreeMap<Long, Invoker<T>>();
this.identityHashCode = System.identityHashCode(invokers);
URL url = invokers.get(0).getUrl();
this.replicaNumber = url.getMethodParameter(methodName, "hash.nodes", 160);
String[] index = Constants.COMMA_SPLIT_PATTERN.split(url.getMethodParameter(methodName, "hash.arguments", "0"));
argumentIndex = new int[index.length];
for (int i = 0; i < index.length; i ++) {
argumentIndex[i] = Integer.parseInt(index[i]);
}
for (Invoker<T> invoker : invokers) {
for (int i = 0; i < replicaNumber / 4; i++) {
byte[] digest = md5(invoker.getUrl().toFullString() + i);
for (int h = 0; h < 4; h++) {
long m = hash(digest, h);
virtualInvokers.put(m, invoker);
}
}
}
}根据对应的Invokers生成对应的hash环虚拟节点,放在一个TreeMap的结构中,key为虚拟节点的hash值。这样就可以在TreeMap中有序排列,这样hash环就生成了,调用选择的方法如下: public Invoker<T> select(Invocation invocation) {
String key = toKey(invocation.getArguments());
byte[] digest = md5(key);
Invoker<T> invoker = sekectForKey(hash(digest, 0));
return invoker;
}以参数值通过md5加密的hash为参数,进入selectForKey方法: private Invoker<T> sekectForKey(long hash) {
Invoker<T> invoker;
Long key = hash;
if (!virtualInvokers.containsKey(key)) {
SortedMap<Long, Invoker<T>> tailMap = virtualInvokers.tailMap(key);
if (tailMap.isEmpty()) {
key = virtualInvokers.firstKey();
} else {
key = tailMap.firstKey();
}
}
invoker = virtualInvokers.get(key);
return invoker;
}以入参hash获取tailMap的第一个比较大的虚拟节点的Invoker,如果入参的hashcode最大则用第一个虚拟节点的Invoker。
相关文章推荐
- consistent hashing(一致性hash)
- Consistent Hash Ring【一致性哈希】
- Dubbo负载均衡:一致性Hash的实现分析
- 一致性hash 原理与实现 (Consistent hashing implemented)
- Dubbo负载均衡:一致性Hash的实现分析
- 探索c#之一致性Hash详解
- 参考dubbo 利用hash一致性做负载均衡[代码记录,供以后参考]
- Mycat 分片规则详解--一致性hash分片
- jump Consistent hash:零内存消耗,均匀,快速,简洁,来自Google的一致性哈希算法
- dubbo 一致性hash
- 一致性hash 之 [翻译]Consistent Hash By Tom White
- 探索c#之一致性Hash详解
- 探索c#之一致性Hash详解
- c# 一致性HASH详解
- [Oracle] Sql优化系列--Hash join详解
- dubbo 配置文件详解
- vue vue-Router默认hash模式修改为history需要做的修改详解
- Hash详解
- 基于一致性hash算法(consistent hashing)的使用详解
- redis数据结构详解之Hash(四)