Selector
In a scaled system, there are many nodes to provide same service. They are deployed at the datacenter or multiple datacenter.
How do clients select a node to invoke? You can use a selector to do this and it is just like a load balancing with routing algorithm.
rpcx supports multiple selectors and you can set it when you create XClient.
RandomSelect
Example:random
This selector will pick a node randomly.
Roundrobin
Example:roundrobin
select the node in a round-robin fashion.
WeightedRoundRobin
Example:weighted
use the smooth weighted round-robin balancing, implemented the same algorithm in Nginx.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weightof each eligible peer by its weight, select peer with greatest current_weightand reduce its current_weight by total number of weight points distributedamong peers.
WeightedICMP
Example:ping
use the result of ping (ICMP) to set weight of each node. The shorter ping time, the higher weight of the node.
assume t
is cost time of ping:
- weight=191 if t <= 10
- weight=201 -t if 10 < t <=200
- weight=1 if 200 < t < 1000
- weight=0 if t >= 1000
ConsistentHash
Example:hash
Use JumpConsistentHash to router the same node for the same servicePath, serviceMethod and arguments. It is a fast consistent hash algorithm.
If nodes have changed, the algorithm will re-calculate consistent hash.
Geography
Example:geo
We want to clients to select a closest service to invoke.
Services must set the geo location in their metadata.
If they are multiple closest services, select one randomly.
You should use the below method set geo location of clients and it will change selector to Geo selector.
func (c *xClient) ConfigGeoSelector(latitude, longitude float64)
SelectByUser Selector
Example:customized
If the above selectors are not suitable for you, you can develop a customized selector.
The selector is an interface:
type Selector interface {
Select(ctx context.Context, servicePath, serviceMethod string, args interface{}) string
UpdateServer(servers map[string]string)
}
-Select
: defines the select algorithm.-UpdateServer
: clients init the nodes and update if nodes change.
By smallnest updated 2018-12-04 11:47:26