dubbo-- Load Balancing

dubbo provides four load balancing strategy: random, robin, least active, consistent hash

A, RandomLoadBalance-- random

    protected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL url, Invocation invocation) {
        // Number of invokers
        int length = invokers.size();
        // Every invoker has the same weight?
        boolean sameWeight = true;
        // the weight of every invokers
        int[] weights = new int[length];
        // the first invoker's weight
        int firstWeight = getWeight(invokers.get(0), invocation);
        weights[0] = firstWeight;
        // The sum of weights
        int totalWeight = firstWeight;
        for (int i = 1; i < length; i++) {
            int weight = getWeight(invokers.get(i), invocation);
            // save for later use
            weights[i] = weight;
            // Sum
            totalWeight += weight;
            if (sameWeight && weight != firstWeight) {
                sameWeight = false ;
            }
        }
        // the right weight, by weight random 
        IF (totalWeight> 0 &&! SameWeight) {
             // 0 - totalweight (not included) a random number 
            int offset = ThreadLocalRandom.current () the nextInt (totalWeight);.
             // return random corresponding to the number of the array An invoker 
            for ( int I = 0; I <length; I ++ ) {
                offset -= weights[i];
                if (offset < 0) {
                    return invokers.get(i);
                }
            }
        }
        // all the node weight is equal to 0 or random array returned from a invoker 
        return invokers.get (ThreadLocalRandom.current () the nextInt (length).);
    }

Summary: random load balancing: according to each node weights, random (using ThreadLocalRandom thread-safe), is divided into two specific cases:

1, each node of the same weight, returns a random invoker.

2, is not the same weight, the total weight generated according to a random number, the random number and interval returns the corresponding invoker.

Features: a small amount of requests may occur is inclined, when the request is increased, the more balanced.

Two, RoundRobinLoadBalance-- poll

 

Three, LeastActiveLoadBalance-- minimal activity

 

Four, ConsistentHashLoadBalance-- consistency Hash

 

Guess you like

Origin www.cnblogs.com/wqff-biubiu/p/12501555.html
Recommended