阿里巴巴的分布式应用框架-dubbo负载均衡策略--- 一致哈希算法

2023-06-15,,

dubbo是阿里巴巴公司开发的一个开源分布式应用框架,基于服务的发布者和订阅者,服务者启动服务向注册中心发布自己的服务;消费者(订阅者)启动服务器向注册中心订阅所需要的服务。注册中心将订阅的服务注册列表返回给订阅者。注册中心会感应服务的提供者的变化,如果服务的提供者发生变化,注册中心会立即通知消费者及时变更服务信息数据;dubbo并且带有审计功能--监控中心,服务的发布者和服务的消费者每分钟间隔都会向监控中心发送自己的统计情况如:调用次数 或者调用时间等(这些数据是保存在内存中以每分钟为单位向监控中心进行发送数据)。监控中心宕机不影响使用,只是减少部分采样的数据,对等的集群任意一台服务宕机,会自动切换到其他对等的机器,注册中心宕机,对服务没有影响,订阅者自己本地会缓存服务提供者的列表信息,由此可见dubbo的健壮性还是可以的。

dubbo有很多非常好的特性:负载均衡、集群容错等

集群容错策略:

failover :失败重连;

failfast:只发起一次请求;

failsafe:失败直接忽略;

failback:失败返回定时发送;、

forking:并发执行 只要有一台成功立即返回;

broadcast:调用所有提供者任意一台报错就报错)。

负载均衡:

随机按照权重概率选择

轮循,按公约后的权重设置轮循比率

最少活跃调用数,相同活跃数的随机,活跃数指调用前后计数差。
一致哈希:相同的请求参数会请求同一个服务提供者。

支持多协议:

dubbo、thrift、rmi、jms、redis 等。

下面我们看下下载地址 dubbo的一直哈希策略的实现源代码:默认有160个虚拟节点 这样让服务列表更加均匀分布,命中更均匀。

./*
2. * Copyright 1999-2012 Alibaba Group.
3. *
4. * Licensed under the Apache License, Version 2.0 (the "License");
5. * you may not use this file except in compliance with the License.
6. * You may obtain a copy of the License at
7. *
8. * http://www.apache.org/licenses/LICENSE-2.0
9. *
10. * Unless required by applicable law or agreed to in writing, software
11. * distributed under the License is distributed on an "AS IS" BASIS,
12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13. * See the License for the specific language governing permissions and
14. * limitations under the License.
15. */
.package com.alibaba.dubbo.rpc.cluster.loadbalance;
.
.import java.io.UnsupportedEncodingException;
.import java.security.MessageDigest;
.import java.security.NoSuchAlgorithmException;
.import java.util.List;
.import java.util.SortedMap;
.import java.util.TreeMap;
.import java.util.concurrent.ConcurrentHashMap;
.import java.util.concurrent.ConcurrentMap;
.
.import com.alibaba.dubbo.common.Constants;
.import com.alibaba.dubbo.common.URL;
.import com.alibaba.dubbo.rpc.Invocation;
.import com.alibaba.dubbo.rpc.Invoker;
.
./**
33. * ConsistentHashLoadBalance
34. *
35. * @author william.liangf
36. */
.public class ConsistentHashLoadBalance extends AbstractLoadBalance {
.
. private final ConcurrentMap<String, ConsistentHashSelector<?>> selectors = new ConcurrentHashMap<String, ConsistentHashSelector<?>>();
.
. @SuppressWarnings("unchecked")
. @Override
. protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
. String key = invokers.get().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);
. }
.
. private static final class ConsistentHashSelector<T> {
.
. private final TreeMap<Long, Invoker<T>> virtualInvokers;
.
. private final int replicaNumber;
.
. private final int identityHashCode;
.
. private final int[] argumentIndex;
.
. 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().getUrl();
. this.replicaNumber = url.getMethodParameter(methodName, "hash.nodes", );
. String[] index = Constants.COMMA_SPLIT_PATTERN.split(url.getMethodParameter(methodName, "hash.arguments", ""));
. argumentIndex = new int[index.length];
. for (int i = ; i < index.length; i ++) {
. argumentIndex[i] = Integer.parseInt(index[i]);
. }
. for (Invoker<T> invoker : invokers) {
. for (int i = ; i < replicaNumber / ; i++) {
. byte[] digest = md5(invoker.getUrl().toFullString() + i);
. for (int h = ; h < ; h++) {
. long m = hash(digest, h);
. virtualInvokers.put(m, invoker);
. }
. }
. }
. }
.
. public int getIdentityHashCode() {
. return identityHashCode;
. }
.
. public Invoker<T> select(Invocation invocation) {
. String key = toKey(invocation.getArguments());
. byte[] digest = md5(key);
. Invoker<T> invoker = sekectForKey(hash(digest, ));
. return invoker;
. }
.
. private String toKey(Object[] args) {
. StringBuilder buf = new StringBuilder();
. for (int i : argumentIndex) {
. if (i >= && i < args.length) {
. buf.append(args[i]);
. }
. }
. return buf.toString();
. }
.
. 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;
. }
.
. private long hash(byte[] digest, int number) {
. return (((long) (digest[ + number * ] & 0xFF) << )
. | ((long) (digest[ + number * ] & 0xFF) << )
. | ((long) (digest[ + number * ] & 0xFF) << )
. | (digest[ + number * ] & 0xFF))
. & 0xFFFFFFFFL;
. }
.
. private byte[] md5(String value) {
. MessageDigest md5;
. try {
. md5 = MessageDigest.getInstance("MD5");
. } catch (NoSuchAlgorithmException e) {
. throw new IllegalStateException(e.getMessage(), e);
. }
. md5.reset();
. byte[] bytes = null;
. try {
. bytes = value.getBytes("UTF-8");
. } catch (UnsupportedEncodingException e) {
. throw new IllegalStateException(e.getMessage(), e);
. }
. md5.update(bytes);
. return md5.digest();
. }
.
. }
.
.}

阿里巴巴的分布式应用框架-dubbo负载均衡策略--- 一致哈希算法的相关教程结束。

《阿里巴巴的分布式应用框架-dubbo负载均衡策略--- 一致哈希算法.doc》

下载本文的Word格式文档,以方便收藏与打印。