问题描述
dubbo版本3.0.5,两个服务提供者A、B
当消费者请示OrderService 服务时,A调用三次,然后B调用一次,然后A调用三次,然后B调用一次,不断循环,相同请示没有有只请示一个服务者。
java源代码
@DubboReference(version="b",loadbalance = "consistenthash") public OrderService orderServiceB;
dubbo3.0.5的ConsistentHashLoadBalance.java 源代码
private Invoker selectForKey(long hash) { Map.Entry> entry = virtualInvokers.ceilingEntry(hash); if (entry == null) { entry = virtualInvokers.firstEntry(); } String serverAddress = entry.getValue().getUrl().getAddress(); double overloadThread = ((double) totalRequestCount.get() / (double) serverCount) * OVERLOAD_RATIO_THREAD; while (serverRequestCountMap.containsKey(serverAddress) && serverRequestCountMap.get(serverAddress).get() >= overloadThread) { entry = getNextInvokerNode(virtualInvokers, entry); serverAddress = entry.getValue().getUrl().getAddress(); } if (!serverRequestCountMap.containsKey(serverAddress)) { serverRequestCountMap.put(serverAddress, new AtomicLong(1)); } else { serverRequestCountMap.get(serverAddress).incrementAndGet(); } totalRequestCount.incrementAndGet(); return entry.getValue(); }
分析源代码发现,当请示超过阀值,会取其它服务
dubbo2.7.10的ConsistentHashLoadBalance.java 源代码
public class ConsistentHashLoadBalance extends AbstractLoadBalance { public static final String NAME = "consistenthash"; public static final String HASH_NODES = "hash.nodes"; public static final String HASH_ARGUMENTS = "hash.arguments"; private final ConcurrentMap> selectors = new ConcurrentHashMap>(); @SuppressWarnings("unchecked") @Override protected Invoker doSelect(List> invokers, URL url, Invocation invocation) { String methodName = RpcUtils.getMethodName(invocation); String key = invokers.get(0).getUrl().getServiceKey() + "." + methodName; // using the hashcode of list to compute the hash only pay attention to the elements in the list int invokersHashCode = invokers.hashCode(); ConsistentHashSelector selector = (ConsistentHashSelector) selectors.get(key); if (selector == null || selector.identityHashCode != invokersHashCode) { selectors.put(key, new ConsistentHashSelector(invokers, methodName, invokersHashCode)); selector = (ConsistentHashSelector) selectors.get(key); } return selector.select(invocation); } private static final class ConsistentHashSelector { private final TreeMap> virtualInvokers; private final int replicaNumber; private final int identityHashCode; private final int[] argumentIndex; ConsistentHashSelector(List> invokers, String methodName, int identityHashCode) { this.virtualInvokers = new TreeMap>(); this.identityHashCode = identityHashCode; URL url = invokers.get(0).getUrl(); this.replicaNumber = url.getMethodParameter(methodName, HASH_NODES, 160); String[] index = 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 invoker : invokers) { String address = invoker.getUrl().getAddress(); for (int i = 0; i < replicaNumber / 4; i++) { byte[] digest = Bytes.getMD5(address + i); for (int h = 0; h < 4; h++) { long m = hash(digest, h); virtualInvokers.put(m, invoker); } } } } public Invoker select(Invocation invocation) { String key = toKey(invocation.getArguments()); byte[] digest = Bytes.getMD5(key); return selectForKey(hash(digest, 0)); } private String toKey(Object[] args) { StringBuilder buf = new StringBuilder(); for (int i : argumentIndex) { if (i >= 0 && i < args.length) { buf.append(args[i]); } } return buf.toString(); } private Invoker selectForKey(long hash) { Map.Entry> entry = virtualInvokers.ceilingEntry(hash); if (entry == null) { entry = virtualInvokers.firstEntry(); } return entry.getValue(); } private long hash(byte[] digest, int number) { return (((long) (digest[3 + number * 4] & 0xFF) << 24) | ((long) (digest[2 + number * 4] & 0xFF) << 16) | ((long) (digest[1 + number * 4] & 0xFF) << 8) | (digest[number * 4] & 0xFF)) & 0xFFFFFFFFL; } }}
分析dubbo2.7.10版本源代码,相同消费者的请求参数相同请示的服务提供者不会变化。
dubbo3的变化让人很懵逼,违背了致性请示原则,开始还以为配置错误,负载均衡没有生效。