LoadBalance load Balancing, responsible for selecting a specific invoker from multiple invokers for this invocation, the call process contains the load balancing algorithm, the call will need to re-select after the failure
LoadBalance Interface Definition
@SPI (Randomloadbalance. NAME)
Public interface loadbalance{
@Adaptive ("LoadBalance")
<T> invoker<t> Select (list<invoker<t>> invokers,url URL, invocation invocation)throws rpcexception;
}
Class Annotations @spi Description can be customized based on the Dubbo extension mechanism for the implementation of the balanced algorithm, the default is the random algorithm
Method Note @adaptive Description can be generated to set the formulation method
The Select method sets a specific algorithm for selecting a URL parameter, and selects a invoker from the Invokers set based on the specific algorithm.
1. Randomloadbalance: Random access Policy, set random probability by weight, is the default policy
1) Get the number of all invokers
2) Traverse all invokers, get the weights of each invokers, and add the weights together
Each of the two adjacent Invoker compare their weights are the same, there is a different weight is not equal
3) If the total weight is greater than 0 and the weight is not equal
Get random number by total weight offset = random.netx (totalweight);
Traverse Invokers to determine which fragment the random number offset falls on (invoker)
4) The same weight or the total weight of 0, according to the number of invokers equal choice
Invokers.get (random.nextint (length))
2. Roundrobinloadbalance: Poll, set poll rate by weight after convention
1) Get polling key service Name + method name
Gets the number of invokers available for invocation length
Sets the default value for the maximum weight maxweight=0
Sets the default value for the minimum weight minweight=integer.max_value
2) Traverse all inokers, compare maxweight and Minweight
3) If the weights are not the same
Get the self-increment sequence based on key
The self-increment sequence plus one and the maximum weight modulus are currentweigth by default
Traverse all invokers to filter out invokers greater than currentweight
Sets the number of invokers available for invocation length
4) self-increment sequence plus and length modulus, get invoker from Invokers
3. Leastactiveloadbalance: The minimum number of active calls, the same active random selection,
The active number refers to the count difference before and after the call, which causes the slow provider to receive fewer requests, because the slower the provider, the greater the count difference before and after.
Active count of the functional consumer is set in the Activelimitfilter
4. The least active selection process is as follows:
1) Get the total number of callable Invoker
Initialize the minimum active number, the same minimum number of active
Subscript array with the same minimum active number
Wait a minute
2) Traverse all invokers to get the number of fetches per invoker active and weighted
Find the invoker of the minimum weight
If there is a inovkers with the same minimum weight, the subscript is recorded in the array leastindexs[] Array
Accumulate all weights to the totalweight variable
3) If the weights of the invokers are not equal and Totalweight is greater than 0
Random offsetweight by total weight = Random.nextint (totalweight)
Calculates the random value on which fragment and returns Invoker
4) If the weights of the invokers are equal or totalweight equals 0, equal random
5. Consistenthashloadbalance: Consistent hash, the same parameter requests are always sent to the same provider, when a provider hangs, the original request to the provider, based on the virtual node, divided into other providers, will not cause drastic changes. For a consistent hash algorithm introduced on the Internet a lot, this gives a http://blog.csdn.net/sparkliang/article/details/5279393 for reference, Readers please read the consistentashloadbalance in the implementation of the consistent hashing algorithm, or relatively easy to understand here is no longer verbose.
Dubbo Principle Analysis-cluster & fault Tolerant load balancing