Nginx--负载均衡--加权轮询

转载自:https://www.jianshu.com/p/159fb7805147

一. 加权轮询在nginx中的部分配置

http {
    upstream cluster {
        server a weight=5;
        server b weight=1;
        server c weight=1;
    }
    server {
        listen 80;
        location / {
            proxy_pass http: //cluster 
        }
    }
}

二. 加权轮询算法介绍

加权轮询算法的原理:根据服务器的不同处理能力,给每个服务器分配不同的权值,使其能够接受相应权值数的服务请求。

Nginx加权轮询算法中,每个节点有三个权重变量:

  1. weight : 配置的权重,即在配置文件或初始化时约定好的每个节点的权重
  2. currentWeight : 节点当前权重,会一直变化
  3. effectiveWeight :有效权重,初始值为weight, 通讯过程中发现节点异常,则-1 ,之后再次选取本节点,调用成功一次则+1,直达恢复到weight 。 用于健康检查,处理异常节点,降低其权重

算法逻辑:

  1. 轮询所有节点,计算当前状态下所有节点的effectiveWeight之和totalWeight
  2. currentWeight = currentWeight + effectiveWeight; 选出所有节点中currentWeight中最大的一个节点作为选中节点
  3. 选中节点的currentWeight = currentWeight - totalWeight;
    注:为了简单清晰,后面的实现不考虑健康检查effectiveWeight这个功能实现,假设所有节点都是100%可用,所以上面的逻辑要把使用effectiveWeight的地方换成weight

例子:

三. java代码实现

1. DemoApplication 类
package com.example.demo;

public class DemoApplication {
    public static void main(String[] args) {
        /**
         * 假设有三个服务器权重配置如下:
         * server A  weight = 4 ;
         * server B  weight = 3 ;
         * server C  weight = 2 ;
         */
        Node serverA = new Node("serverA", 4);
        Node serverB = new Node("serverB", 3);
        Node serverC = new Node("serverC", 2);
        
        SmoothWeightedRoundRobin smoothWeightedRoundRobin = 
                new SmoothWeightedRoundRobin(serverA,serverB ,serverC);
        
        for (int i = 0; i < 7; i++) {
            Node i1 = smoothWeightedRoundRobin.select();
            System.out.println(i1.getServerName());
        }
    }
}
2. SmoothWeightedRoundRobin 类
package com.example.demo;

import java.util.*;
import java.util.concurrent.locks.ReentrantLock;

public class SmoothWeightedRoundRobin {
    private volatile List<Node> nodeList = new ArrayList<>() ; // 保存权重
    private ReentrantLock lock = new ReentrantLock() ;

    public SmoothWeightedRoundRobin(Node ...nodes) {
        for (Node node : nodes) {
            nodeList.add(node) ;
        }
    }

    public Node select(){
        try {
            lock.lock();
            return this.selectInner() ;
        }finally {
            lock.unlock();
        }
    }

    private Node selectInner(){
        int totalWeight = 0 ;
        Node maxNode = null ;
        int maxWeight = 0 ;

        for (int i = 0; i < nodeList.size(); i++) {
            Node n = nodeList.get(i);
            totalWeight += n.getWeight() ;

            // 每个节点的当前权重要加上原始的权重
            n.setCurrentWeight(n.getCurrentWeight() + n.getWeight());

            // 保存当前权重最大的节点
            if (maxNode == null || maxWeight < n.getCurrentWeight() ) {
                maxNode = n ;
                maxWeight = n.getCurrentWeight() ;
            }
        }
        // 被选中的节点权重减掉总权重
        maxNode.setCurrentWeight(maxNode.getCurrentWeight() - totalWeight);

        return maxNode ;
    }
}
3. Node 类
package com.example.demo;

public class Node {
    private final int weight ;  // 初始权重 (保持不变)
    private final String serverName ; // 服务名
    private int currentWeight ; // 当前权重

    public Node( String serverName, int weight) {
        this.weight = weight;
        this.serverName = serverName ;
        this.currentWeight = weight ;
    }

    public int getWeight() {
        return weight;
    }
    public String getServerName() {
        return serverName;
    }
    public int getCurrentWeight() {
        return currentWeight;
    }
    public void setCurrentWeight(int currentWeight) {
        this.currentWeight = currentWeight;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。