A colleague asked if dubbo's load balancing is done by zookeeper, I don't know, it's embarrassing, but I guess it is done internally by dubbo. The real-time load balancing is handled by dubbo-admin, then modify it on the interface of dubbo-admin The weight parameter is fine
So where is the specific embodiment of this parameter?
com.xxxx.xxx.common.service.basic.IAuditJoursService=override\://192.168.247.9/com.xxxx.xxx.common.service.basic.IAuditJoursService?category\=configurators&dynamic\=false&enabled\=true&weight\=400
It can be seen that a piece of content is inserted in dubbo.registry, and the weight is changed to 400, then the weight of this service is 400. When calling, the client will decide which service provider to call according to the weight and policy.
protected int getWeight(Invoker<?> invoker, Invocation invocation) {
// 根据url获取weight
int weight = invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT);
if (weight > 0) {
long timestamp = invoker.getUrl().getParameter(Constants.TIMESTAMP_KEY, 0L);
if (timestamp > 0L) {
// 计算服务的启动时间
int uptime = (int) (System.currentTimeMillis() - timestamp);
int warmup = invoker.getUrl().getParameter(Constants.WARMUP_KEY, Constants.DEFAULT_WARMUP);
// 如果启动时间小于预热时间的话,默认60s
if (uptime > 0 && uptime < warmup) {
weight = calculateWarmupWeight(uptime, warmup, weight);
}
}
}
return weight;
}
/**
* 启动时间 / 预热时间 * 权重 说明还没有预热完的服务被选取的几率比较小,
* 预热可能加载一些数据字典等基础内容
*/
static int calculateWarmupWeight(int uptime, int warmup, int weight) {
int ww = (int) ( (float) uptime / ( (float) warmup / (float) weight ) );
return ww < 1 ? 1 : (ww > weight ? weight : ww);
}
AbstractLoadBalance.java
protected abstract <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation);
All load balancing strategies integrate this class and implement the selected algorithm by itself. Dubbo selects the random algorithm by default.
- Random LoadBalance
is random, and the random probability is set according to the weight.
The probability of collision on a section is high, but the larger the number of calls, the more uniform the distribution, and the more uniform the weights are used according to the probability, which is conducive to dynamically adjusting the provider weights. - RoundRobin LoadBalance
round robin, set the round robin ratio according to the weight after the convention.
There is a problem of slow providers accumulating requests. For example, the second machine is very slow, but it does not hang. When the request is transferred to the second machine, it is stuck there. Over time, all requests are stuck on the second machine. - LeastActive LoadBalance
Minimum number of active calls, the same active number is random, the active number refers to the difference between the counts before and after the call.
Make the slow provider receive fewer requests, because the slower the provider's count difference before and after the call will be larger.
ConsistentHash LoadBalance - Consistent Hash, requests with the same parameters are always sent to the same provider.
When a certain provider hangs, the request originally sent to the provider will be spread to other providers based on virtual nodes, and will not cause drastic changes.
See the algorithm: http://en.wikipedia.org/wiki/Consistent_hashing
by default only hashes the first parameter. If you want to modify it, please configure it. By
default 160 virtual nodes are used. If you want to modify it, please configure