dubbo 负载均衡

在系统中可以启动多个 provider 实例,consumer 发起远程调用时,根据指定的负载均衡算法选择一个 provider。

在本机配置多个 provider,使用不同的端口:

<dubbo:protocol name="dubbo" port="20880"/>

<dubbo:protocol name="dubbo" port="20881"/>

<dubbo:protocol name="dubbo" port="20882"/>

consumer 配置 loadbalance:

<dubbo:reference id="hello" loadbalance="roundrobin" interface="com.zhang.HelloService" />

dubbo 2.1.2 提供了4种不同的负载均衡算法,在 /META-INF/dubbo/com.alibaba.dubbo.rpc.cluster.LoadBalance 文件中:

adptive=com.alibaba.dubbo.rpc.cluster.loadbalance.LoadBalanceAdptive
random=com.alibaba.dubbo.rpc.cluster.loadbalance.RandomLoadBalance
roundrobin=com.alibaba.dubbo.rpc.cluster.loadbalance.RoundRobinLoadBalance
leastactive=com.alibaba.dubbo.rpc.cluster.loadbalance.LeastActiveLoadBalance
consistenthash=com.alibaba.dubbo.rpc.cluster.loadbalance.ConsistentHashLoadBalance

分别对应随机、轮询、最少活跃、一致性哈希。
随机、轮训都是先考虑权重,如果没有设置权重或者每个 provider 的权重相同,则退化成完全的随机和轮训,最少活跃没看明白。

负载均衡的粒度是单个方法,例: com.zhang.HelloService.sayHello() 有一个负载均衡的 selector。

轮询思想就是:维持一个 map,“接口名 + 方法名”作为建,一个计数器作为值,每次调用接口时,增加计数器然后取模。

public class RoundRobinLoadBalance extends AbstractLoadBalance {
    public static final String NAME = "roundrobin"; 
    private final ConcurrentMap<String, AtomicPositiveInteger> sequences = new ConcurrentHashMap<String, AtomicPositiveInteger>();
    private final ConcurrentMap<String, AtomicPositiveInteger> weightSequences = new ConcurrentHashMap<String, AtomicPositiveInteger>();

    protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, Invocation invocation) {
        String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName();
        // 省略权重部分代码
        AtomicPositiveInteger sequence = sequences.get(key);
        if (sequence == null) {
            sequences.putIfAbsent(key, new AtomicPositiveInteger());
            sequence = sequences.get(key);
        }
        // 取模轮循
        return invokers.get(sequence.getAndIncrement() % length);
    }

}

重点分析下一致性哈希吧,它的思想和jedis如出一辙。假定现在有invoker1,invoker2,invoker3,从invoker1衍生出160个符号,根据这些符号计算哈希值,然后把哈希值和 invoker 作为键值对放到 TreeMap 上。同样操作invoker2和invoker3。在选择invoker时,根据调用参数获取哈希值,然后从TreeMap上搜索对应的键值。

// com.alibaba.dubbo.rpc.cluster.loadbalance.ConsistentHashLoadBalance.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();
    
    // hash.nodes 默认为160,表示1个invoker对应160个符号,或者说160个符号指向这个invoker
    this.replicaNumber = url.getMethodParameter(methodName, "hash.nodes", 160);
    // hash.arguments 默认为0,默认取调用方法的第1个参数值计算哈希值
    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);
                // 把键值对挂到TreeMap上
                virtualInvokers.put(m, invoker);
            }
        }
    }
}

public Invoker<T> select(Invocation invocation) {
    //获取参数值
    String key = toKey(invocation.getArguments());
    //md5计算
    byte[] digest = md5(key);
    //计算哈希值,从TreeMap上取invoker
    Invoker<T> invoker = sekectForKey(hash(digest, 0));
    return invoker;
}

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

假定存在方法:

void com.zhang.HelloService.f1(int userid);
void com.zhang.HelloService.f2(int userid);
void com.zhang.HelloService.f3(int userid, Object param);

如果采用一致性哈希负载均衡,可以肯定的是,f1(10086) 的调用都会被转发到同一个的 provider,那 f1(10086) 和 f2(10086) 是否会转发到同一个 provider 呢?如果希望 f3(10086, param1) 和 f3(10086, param2) 都转发到相同的 provider,应该怎么做?

当然,我们也可以实现 AbstractLoadBalance 接口,使用自定义的负载均衡算法。

 如果考虑到 provider 会下线,或者有新的 porvider 上线,则一致性哈希的 virtualInvokers 会重新计算。为什么要使用这种一致性哈希算法?

原文地址:https://www.cnblogs.com/allenwas3/p/8846316.html