Loading... ## 权重变量 **weight** > 配置的权重,即在配置文件或初始化时约定好的每个节点的权重 **effective_weight** > 后端的有效权重,初始值为weight 在释放后端时,如果发现与后端的通信过程中发生了错误,则减小effective_weight 此后有新的请求过来时,在选取后端的过程中,再逐步增加effective_weight,最终又恢复到weight 这个配置作用主要是为了当后端发生错误时,降低其权重 **current_weight** > 后端的当前权重,初始值为0 每次选取后端时,会遍历集群中所有后端,对于每个后端,让它的current_weight增加它的effective_weight,同时累加所有后端的effective_weight,保存为total 如果该后端的current_weight是最大的,就选定这个后端,然后把它的current_weight减去total 如果该后端没有被选定,那么current_weight不用减小 ##算法逻辑 > 轮询所有节点,计算当前状态下所有节点的effectiveWeight之和totalWeight currentWeight = currentWeight + effectiveWeight,选出所有节点中currentWeight中最大的一个节点为命中节点 命中节点的currentWeight = currentWeight - totalWeight; ##示例 ``` upstream test { server localhost:8081 weight=1 server localhost:8082 weight=2 server localhost:8083 weight=3 } ``` | current_weight | current_weight += effective_weight | total | 命中 | current_weight | | ------------ | ------------ | ------------ | ------------ | ------------ | | [0,0,0] | [1,2,3] | 6 | 8003 | [1,2,-3] | | [1,2,-3] |[2,4,0] | 6 | 8002 | [2,-2,0] | | [2,-2,0] | [3,0,3] | 6 | 8003 | [3,0,-3] | | [3,0,-3] |[4,2,0] | 6 | 8001 | [-2,2,0] | | [-2,2,0] | [-1,4,3] | 6 | 8002 | [-1,-2,3] | | [-1,-2,3] |[0,0,6] | 6 | 8003 | [0,0,0] | | [0,0,0] |[1,2,3] | 6 | 8003 | [1,2,-3] | 最后修改:2020 年 09 月 16 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 0 如果觉得我的文章对你有用,请随意赞赏
7 条评论
自从有了UDP的需求,开始对NGINX有了新的依赖
太专业啦
负载均衡怎么实现用户登陆状态同步呢
可以在后端用redis做session同步,或者可以试试nginx consistent_hash(一致性哈希)算法
就像用户ID 正常情况下是存在session里的,用的时候从session里取;如果用redis做session同步,是直接把ID存在redis里吗,不管哪个服务器校验登陆状态都要在redis去取?
是的 session存redis里面,后端从redis取
::(怒) 来看看你。么么哒