dubbo中实现了四种负载均衡算法,继承结构图如下:
依次来介绍这四种负载均衡算法。
RandomLoadBalance(随机算法)
随机算法的代码是:
protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
int length = invokers.size(); // 总个数
int totalWeight = 0; // 总权重
boolean sameWeight = true; // 权重是否都一样
for (int i = 0; i < length; i++) {
int weight = getWeight(invokers.get(i), invocation);
totalWeight += weight; // 累计总权重
if (sameWeight && i > 0
&& weight != getWeight(invokers.get(i - 1), invocation)) {
sameWeight = false; // 计算所有权重是否一样
}
}
if (totalWeight > 0 && ! sameWeight) {
// 如果权重不相同且权重大于0则按总权重数随机
int offset = random.nextInt(totalWeight);
// 并确定随机值落在哪个片断上
for (int i = 0; i < length; i++) {
offset -= getWeight(invokers.get(i), invocation);
if (offset < 0) {
return invokers.get(i);
}
}
}
// 如果权重相同或权重为0则均等随机
return invokers.get(random.nextInt(length));
}
代码中已经有详细的注释了。
算法思想大致是根据权重随机选择结点。例如有A、B、C三个结点,权重分别是3、5、8,总权重就是16。该算法就生成[1, 16]
的一个随机数r
,如果r
属于[1, 3]
就选择A,r
属于[4, 8]
就选择B,r
属于[9, 16]
就选择C。
可以看出,该算法中,每个结点被选中的概率依赖于权重的比例。
RoundRobinLoadBalance(权重轮询算法)
权重轮询算法的代码:
protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
String key = invokers.get(0).getUrl().getServiceKey() + "." + invocation.getMethodName();
int length = invokers.size(); // 总个数
int maxWeight = 0; // 最大权重
int minWeight = Integer.MAX_VALUE; // 最小权重
for (int i = 0; i < length; i++) {
int weight = getWeight(invokers.get(i), invocation);
maxWeight = Math.max(maxWeight, weight); // 累计最大权重
minWeight = Math.min(minWeight, weight); // 累计最小权重
}
if (maxWeight > 0 && minWeight < maxWeight) { // 权重不一样
AtomicPositiveInteger weightSequence = weightSequences.get(key);
if (weightSequence == null) {
weightSequences.putIfAbsent(key, new AtomicPositiveInteger());
weightSequence = weightSequences.get(key);
}
int currentWeight = weightSequence.getAndIncrement() % maxWeight;
List<Invoker<T>> weightInvokers = new ArrayList<Invoker<T>>();
for (Invoker<T> invoker : invokers) { // 筛选权重大于当前权重基数的Invoker
if (getWeight(invoker, invocation) > currentWeight) {
weightInvokers.add(invoker);
}
}
int weightLength = weightInvokers.size();
if (weightLength == 1) {
return weightInvokers.get(0);
} else if (weightLength > 1) {
invokers = weightInvokers;
length = invokers.size();
}
}
AtomicPositiveInteger sequence = sequences.get(key);
if (sequence == null) {
sequences.putIfAbsent(key, new AtomicPositiveInteger());
sequence = sequences.get(key);
}
// 取模轮循
return invokers.get(sequence.getAndIncrement() % length);
}
这段算法和常用的权重轮询算法有点差异,不过思想是一样的。
假设有一组服务器S = {S0, S1, …, Sn-1}
,W(Si)
表示服务器Si
的权值,一个指示变量i
表示上一次选择的服务器,指示变量cw
表示当前调度的权值,max(S)
表示集合S
中所有服务器的最大权值,gcd(S)
表示集合S
中所有服务器权值的最大公约数。变量i
初始化为-1
,cw
初始化为零。算法如下:
while (true) {
i = (i + 1) mod n;
if (i == 0) {
//每一轮权重减少一个最大公约数
cw = cw - gcd(S);
if (cw <= 0) {
//初始化cw等于最大权重
cw = max(S);
if (cw == 0)
return NULL;
}
}
//轮询所有权重大于cw的结点
if (W(Si) >= cw)
return Si;
}
大致思想就是:每一轮循环都只会轮询权重大于cw
的节点,而在每一轮循环结束后,cw
会减少一个最大公约数,也就有更多节点会参与到轮询之中。当cw
小于等于零以后,会被重置成最大的权重值,开始新一轮的循环。
ConsistentHashLoadBalance(一致性hash算法)
一致性hash算法在缓存中应用比较多,网络上有很多材料。
例如:
一致性哈希-维基百科词条
一致性哈希算法的理解与实践
LeastActiveLoadBalance(最少活跃数算法)
代码实现是:
protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
int length = invokers.size(); // 总个数
int leastActive = -1; // 最小的活跃数
int leastCount = 0; // 相同最小活跃数的个数
int[] leastIndexs = new int[length]; // 相同最小活跃数的下标
int totalWeight = 0; // 总权重
int firstWeight = 0; // 第一个权重,用于于计算是否相同
boolean sameWeight = true; // 是否所有权重相同
for (int i = 0; i < length; i++) {
Invoker<T> invoker = invokers.get(i);
int active = RpcStatus.getStatus(invoker.getUrl(), invocation.getMethodName()).getActive(); // 活跃数
int weight = invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT); // 权重
if (leastActive == -1 || active < leastActive) { // 发现更小的活跃数,重新开始
leastActive = active; // 记录最小活跃数
leastCount = 1; // 重新统计相同最小活跃数的个数
leastIndexs[0] = i; // 重新记录最小活跃数下标
totalWeight = weight; // 重新累计总权重
firstWeight = weight; // 记录第一个权重
sameWeight = true; // 还原权重相同标识
} else if (active == leastActive) { // 累计相同最小的活跃数
leastIndexs[leastCount ++] = i; // 累计相同最小活跃数下标
totalWeight += weight; // 累计总权重
// 判断所有权重是否一样
if (sameWeight && i > 0
&& weight != firstWeight) {
sameWeight = false;
}
}
}
// assert(leastCount > 0)
if (leastCount == 1) {
// 如果只有一个最小则直接返回
return invokers.get(leastIndexs[0]);
}
if (! sameWeight && totalWeight > 0) {
// 如果权重不相同且权重大于0则按总权重数随机
int offsetWeight = random.nextInt(totalWeight);
// 并确定随机值落在哪个片断上
for (int i = 0; i < leastCount; i++) {
int leastIndex = leastIndexs[i];
offsetWeight -= getWeight(invokers.get(leastIndex), invocation);
if (offsetWeight <= 0)
return invokers.get(leastIndex);
}
}
// 如果权重相同或权重为0则均等随机
return invokers.get(leastIndexs[random.nextInt(leastCount)]);
}
代码比较简单,结合注释就能理解了。主要思想是记录一个活跃数,这个值保存在RpcStatus
里。找到最少活跃数的结点集合后,在这些集合中根据权重随机选择结合(这里的随机选择算法和RandomLoadBalance一样)。