欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實現(xiàn)平滑加權(quán)輪詢算法之降權(quán)和提權(quán)詳解

 更新時間:2022年04月13日 08:02:31   作者:持行非就  
所有負載均衡的場景幾乎都會用到這個平滑加權(quán)輪詢算法,下面這篇文章主要給大家介紹了關(guān)于Java實現(xiàn)平滑加權(quán)輪詢算法之降權(quán)和提權(quán)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下

前言

上一篇講了普通輪詢、加權(quán)輪詢的兩種實現(xiàn)方式,重點講了平滑加權(quán)輪詢算法,并在文末留下了懸念:節(jié)點出現(xiàn)分配失敗時降低有效權(quán)重值;成功時提高有效權(quán)重值(但不能大于weight值)。

本文在平滑加權(quán)輪詢算法的基礎(chǔ)上講,還沒弄懂的可以看上一篇文章。

現(xiàn)在來模擬實現(xiàn):平滑加權(quán)輪詢算法的降權(quán)和提權(quán)

1.兩個關(guān)鍵點

節(jié)點宕機時,降低有效權(quán)重值;

節(jié)點正常時,提高有效權(quán)重值(但不能大于weight值);

注意:降低或提高權(quán)重都是針對有效權(quán)重。

2.代碼實現(xiàn)

2.1.服務(wù)節(jié)點類

package com.yty.loadbalancingalgorithm.wrr;

/**
 * String ip:負載IP
 * final Integer weight:權(quán)重,保存配置的權(quán)重
 * Integer effectiveWeight:有效權(quán)重,輪詢的過程權(quán)重可能變化
 * Integer currentWeight:當前權(quán)重,比對該值大小獲取節(jié)點
 *   第一次加權(quán)輪詢時:currentWeight = weight = effectiveWeight
 *   后面每次加權(quán)輪詢時:currentWeight 的值都會不斷變化,其他權(quán)重不變
 * Boolean isAvailable:是否存活
 */
public class ServerNode implements Comparable<ServerNode>{
    private String ip;
    private final Integer weight;
    private Integer effectiveWeight;
    private Integer currentWeight;
    private Boolean isAvailable;

    public ServerNode(String ip, Integer weight){
        this(ip,weight,true);
    }
    public ServerNode(String ip, Integer weight,Boolean isAvailable){
        this.ip = ip;
        this.weight = weight;
        this.effectiveWeight = weight;
        this.currentWeight = weight;
        this.isAvailable = isAvailable;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public Integer getWeight() {
        return weight;
    }

    public Integer getEffectiveWeight() {
        return effectiveWeight;
    }

    public void setEffectiveWeight(Integer effectiveWeight) {
        this.effectiveWeight = effectiveWeight;
    }

    public Integer getCurrentWeight() {
        return currentWeight;
    }

    public void setCurrentWeight(Integer currentWeight) {
        this.currentWeight = currentWeight;
    }

    public Boolean isAvailable() {
        return isAvailable;
    }
    public void setIsAvailable(Boolean isAvailable){
        this.isAvailable = isAvailable;
    }

    // 每成功一次,恢復有效權(quán)重1,不超過配置的起始權(quán)重
    public void onInvokeSuccess(){
        if(effectiveWeight < weight) effectiveWeight++;
    }
    // 每失敗一次,有效權(quán)重減少1,無底線的減少
    public void onInvokeFault(){
        effectiveWeight--;
    }

    @Override
    public int compareTo(ServerNode node) {
        return currentWeight > node.currentWeight ? 1 : (currentWeight.equals(node.currentWeight) ? 0 : -1);
    }

    @Override
    public String toString() {
        return "{ip='" + ip + "', weight=" + weight + ", effectiveWeight=" + effectiveWeight
                + ", currentWeight=" + currentWeight + ", isAvailable=" + isAvailable + "}";
    }
}

2.2.平滑輪詢算法降權(quán)和提權(quán)

package com.yty.loadbalancingalgorithm.wrr;

import java.util.ArrayList;
import java.util.List;

/**
 * 加權(quán)輪詢算法:加入存活狀態(tài),降權(quán)使宕機權(quán)重降低,從而不會被選中
 */
public class WeightedRoundRobinAvailable {

    private static List<ServerNode> serverNodes = new ArrayList<>();
    // 準備模擬數(shù)據(jù)
    static {
        serverNodes.add(new ServerNode("192.168.1.101",1));// 默認為true
        serverNodes.add(new ServerNode("192.168.1.102",3,false));
        serverNodes.add(new ServerNode("192.168.1.103",2));
    }

    /**
     * 按照當前權(quán)重(currentWeight)最大值獲取IP
     * @return ServerNode
     */
    public ServerNode selectNode(){
        if (serverNodes.size() <= 0) return null;
        if (serverNodes.size() == 1)
            return (serverNodes.get(0).isAvailable()) ? serverNodes.get(0) : null;
        
        // 權(quán)重之和
        Integer totalWeight = 0;
        ServerNode nodeOfMaxWeight = null; // 保存輪詢選中的節(jié)點信息
        synchronized (serverNodes){
            StringBuffer sb1 = new StringBuffer();
            StringBuffer sb2 = new StringBuffer();
            sb1.append(Thread.currentThread().getName()+"==加權(quán)輪詢--[當前權(quán)重]值的變化:"+printCurrentWeight(serverNodes));
            // 有限權(quán)重總和可能發(fā)生變化
            for(ServerNode serverNode : serverNodes){
                totalWeight += serverNode.getEffectiveWeight();
            }

            // 選出當前權(quán)重最大的節(jié)點
            ServerNode tempNodeOfMaxWeight = serverNodes.get(0);
            for (ServerNode serverNode : serverNodes) {
                if (serverNode.isAvailable()) {
                    serverNode.onInvokeSuccess();//提權(quán)
                    sb2.append(Thread.currentThread().getName()+"==[正常節(jié)點]:"+serverNode+"\n");
                } else {
                    serverNode.onInvokeFault();//降權(quán)
                    sb2.append(Thread.currentThread().getName()+"==[宕機節(jié)點]:"+serverNode+"\n");
                }

                tempNodeOfMaxWeight = tempNodeOfMaxWeight.compareTo(serverNode) > 0 ? tempNodeOfMaxWeight : serverNode;
            }
            // 必須new個新的節(jié)點實例來保存信息,否則引用指向同一個堆實例,后面的set操作將會修改節(jié)點信息
            nodeOfMaxWeight = new ServerNode(tempNodeOfMaxWeight.getIp(),tempNodeOfMaxWeight.getWeight(),tempNodeOfMaxWeight.isAvailable());
            nodeOfMaxWeight.setEffectiveWeight(tempNodeOfMaxWeight.getEffectiveWeight());
            nodeOfMaxWeight.setCurrentWeight(tempNodeOfMaxWeight.getCurrentWeight());

            // 調(diào)整當前權(quán)重比:按權(quán)重(effectiveWeight)的比例進行調(diào)整,確保請求分發(fā)合理。
            tempNodeOfMaxWeight.setCurrentWeight(tempNodeOfMaxWeight.getCurrentWeight() - totalWeight);
            sb1.append(" -> "+printCurrentWeight(serverNodes));

            serverNodes.forEach(serverNode -> serverNode.setCurrentWeight(serverNode.getCurrentWeight()+serverNode.getEffectiveWeight()));

            sb1.append(" -> "+printCurrentWeight(serverNodes));
            System.out.print(sb2);  //所有節(jié)點的當前信息
            System.out.println(sb1); //打印當前權(quán)重變化過程
        }
        return nodeOfMaxWeight;
    }

    // 格式化打印信息
    private String printCurrentWeight(List<ServerNode> serverNodes){
        StringBuffer stringBuffer = new StringBuffer("[");
        serverNodes.forEach(node -> stringBuffer.append(node.getCurrentWeight()+",") );
        return stringBuffer.substring(0, stringBuffer.length() - 1) + "]";
    }

    // 并發(fā)測試:兩個線程循環(huán)獲取節(jié)點
    public static void main(String[] args) throws InterruptedException {
        // 循環(huán)次數(shù)
        int loop = 18;

        new Thread(() -> {
            WeightedRoundRobinAvailable weightedRoundRobin1 = new WeightedRoundRobinAvailable();
            for(int i=1;i<=loop;i++){
                ServerNode serverNode = weightedRoundRobin1.selectNode();
                System.out.println(Thread.currentThread().getName()+"==第"+i+"次輪詢選中[當前權(quán)重最大]的節(jié)點:" + serverNode + "\n");
            }
        }).start();
        //
        new Thread(() -> {
            WeightedRoundRobinAvailable weightedRoundRobin2 = new WeightedRoundRobinAvailable();
            for(int i=1;i<=loop;i++){
                ServerNode serverNode = weightedRoundRobin2.selectNode();
                System.out.println(Thread.currentThread().getName()+"==第"+i+"次輪詢選中[當前權(quán)重最大]的節(jié)點:" + serverNode + "\n");
            }
        }).start();

        //main 線程睡了一下,再偷偷把 所有宕機 拉起來:模擬服務(wù)器恢復正常
        Thread.sleep(5);
        for (ServerNode serverNode:serverNodes){
            if(!serverNode.isAvailable())
                serverNode.setIsAvailable(true);
        }
    }
}

3.分析結(jié)果

執(zhí)行結(jié)果:將執(zhí)行結(jié)果的前中后四次抽出來分析

Thread-0==[正常節(jié)點]:{ip='192.168.1.101', weight=1, effectiveWeight=1, currentWeight=1, isAvailable=true}

Thread-0==[宕機節(jié)點]:{ip='192.168.1.102', weight=3, effectiveWeight=2, currentWeight=3, isAvailable=false}

Thread-0==[正常節(jié)點]:{ip='192.168.1.103', weight=2, effectiveWeight=2, currentWeight=2, isAvailable=true}

Thread-0==加權(quán)輪詢--[當前權(quán)重]值的變化:[1,3,2] -> [1,-3,2] -> [2,-1,4]

Thread-0==第1次輪詢選中[當前權(quán)重最大]的節(jié)點:{ip='192.168.1.102', weight=3, effectiveWeight=2, currentWeight=3, isAvailable=false}

……

Thread-1==[正常節(jié)點]:{ip='192.168.1.101', weight=1, effectiveWeight=1, currentWeight=6, isAvailable=true}

Thread-1==[宕機節(jié)點]:{ip='192.168.1.102', weight=3, effectiveWeight=-7, currentWeight=-21, isAvailable=false}

Thread-1==[正常節(jié)點]:{ip='192.168.1.103', weight=2, effectiveWeight=2, currentWeight=12, isAvailable=true}

Thread-1==加權(quán)輪詢--[當前權(quán)重]值的變化:[6,-21,12] -> [6,-21,15] -> [7,-28,17]

Thread-1==第5次輪詢選中[當前權(quán)重最大]的節(jié)點:{ip='192.168.1.103', weight=2, effectiveWeight=2, currentWeight=12, isAvailable=true}

……

Thread-0==[正常節(jié)點]:{ip='192.168.1.101', weight=1, effectiveWeight=1, currentWeight=13, isAvailable=true}

Thread-0==[正常節(jié)點]:{ip='192.168.1.102', weight=3, effectiveWeight=3, currentWeight=-19, isAvailable=true}

Thread-0==[正常節(jié)點]:{ip='192.168.1.103', weight=2, effectiveWeight=2, currentWeight=12, isAvailable=true}

Thread-0==加權(quán)輪詢--[當前權(quán)重]值的變化:[13,-19,12] -> [7,-19,12] -> [8,-16,14]

Thread-0==第15次輪詢選中[當前權(quán)重最大]的節(jié)點:{ip='192.168.1.101', weight=1, effectiveWeight=1, currentWeight=13, isAvailable=true}

……

Thread-1==[正常節(jié)點]:{ip='192.168.1.101', weight=1, effectiveWeight=1, currentWeight=2, isAvailable=true}

Thread-1==[正常節(jié)點]:{ip='192.168.1.102', weight=3, effectiveWeight=3, currentWeight=2, isAvailable=true}

Thread-1==[正常節(jié)點]:{ip='192.168.1.103', weight=2, effectiveWeight=2, currentWeight=2, isAvailable=true}

Thread-1==加權(quán)輪詢--[當前權(quán)重]值的變化:[2,2,2] -> [2,2,-4] -> [3,5,-2]

Thread-1==第18次輪詢選中[當前權(quán)重最大]的節(jié)點:{ip='192.168.1.103', weight=2, effectiveWeight=2, currentWeight=2, isAvailable=true}

分析

一開始權(quán)重最高的節(jié)點雖然是宕機了,但是還是會被選中并返回;

“有效權(quán)重總和” 和 “當前權(quán)重總和”都減少了1,因為設(shè)置輪詢到失敗節(jié)點,都會自減1;

到第5次輪詢時,當前權(quán)重已經(jīng)變成了[7,-28,17],可以看出宕機節(jié)點越往后當前權(quán)重越小,所以后面根本不會再選中宕機節(jié)點,雖然沒剔除故障節(jié)點,但卻起到不分配宕機節(jié)點;

到第15次輪詢時,有效權(quán)重已經(jīng)恢復起始值,當前權(quán)重變?yōu)閇8,-16,14],當前權(quán)重只能慢慢恢復,并不是節(jié)點一正常就立即恢復宕機過的節(jié)點,起到對故障節(jié)點的緩沖恢復(故障過的節(jié)點可能還存在問題);

最后1次輪詢時,因為沒有宕機節(jié)點,所以有效權(quán)重不變,當前權(quán)重已經(jīng)恢復[3,5,-2],如果再輪詢一次,那就會訪問到一開始故障的節(jié)點了。

4.結(jié)論

降權(quán)起到緩慢“剔除”宕機節(jié)點的效果;提權(quán)起到緩沖恢復宕機節(jié)點的效果。

對比上一篇文章可以看到:

當前權(quán)重(currentWeight):針對的是節(jié)點的選擇,受有效權(quán)重影響,起到緩慢“剔除”宕機節(jié)點和緩沖恢復宕機節(jié)點的效果,當前權(quán)重最高就會被選擇;

有效權(quán)重(effectiveWeight):針對的是權(quán)重的變化,也即是降權(quán)和提權(quán),降權(quán)/提權(quán)只會直接操作有效權(quán)重;

權(quán)重(weight):針對的是存儲起始配置,限定有效權(quán)重的提權(quán)。

到此這篇關(guān)于Java實現(xiàn)平滑加權(quán)輪詢算法之降權(quán)和提權(quán)的文章就介紹到這了,更多相關(guān)Java平滑加權(quán)輪詢降權(quán)和提權(quán)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用spring@value加載時機

    使用spring@value加載時機

    這篇文章主要介紹了使用spring@value加載時機方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • spring框架下@value注解屬性static無法獲取值問題

    spring框架下@value注解屬性static無法獲取值問題

    這篇文章主要介紹了spring框架下@value注解屬性static無法獲取值問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 詳解MyBatis-Puls中saveBatch批量添加慢的問題

    詳解MyBatis-Puls中saveBatch批量添加慢的問題

    本文主要介紹了詳解MyBatis-Puls中saveBatch批量添加慢的問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-01-01
  • springboot整合websocket實現(xiàn)群聊思路代碼詳解

    springboot整合websocket實現(xiàn)群聊思路代碼詳解

    通過springboot引入websocket,實現(xiàn)群聊,通過在線websocket測試進行展示。本文重點給大家介紹springboot整合websocket實現(xiàn)群聊功能,代碼超級簡單,感興趣的朋友跟隨小編一起學習吧
    2021-05-05
  • 一文教你如何判斷Java代碼中異步操作是否完成

    一文教你如何判斷Java代碼中異步操作是否完成

    在許多應(yīng)用程序中,我們經(jīng)常使用異步操作來提高性能和響應(yīng)度,這篇文章主要介紹了幾種常見的方法來判斷Java代碼中異步操作是否完成,希望對大家有所幫助
    2024-02-02
  • 10分鐘帶你理解Java中的反射

    10分鐘帶你理解Java中的反射

    反射是java中一種強大的工具,能夠使我們很方便的創(chuàng)建靈活的代碼,這篇文章帶大家十分鐘快速理解Java中的反射,有需要的可以參考借鑒。
    2016-08-08
  • SpringBoot中的依賴管理詳解

    SpringBoot中的依賴管理詳解

    這篇文章主要介紹了SpringBoot中的依賴管理詳解,傳統(tǒng)的Spring框架實現(xiàn)一個Web服務(wù),需要導入各種依賴JAR包,然后編寫對應(yīng)的XML配置文件等,相較而言,Spring Boot顯得更加方便、快捷和高效,需要的朋友可以參考下
    2023-08-08
  • java搜索無向圖中兩點之間所有路徑的算法

    java搜索無向圖中兩點之間所有路徑的算法

    這篇文章主要介紹了java搜索無向圖中兩點之間所有路徑的算法
    2019-01-01
  • spring boot 注冊攔截器過程詳解

    spring boot 注冊攔截器過程詳解

    這篇文章主要介紹了spring boot中注冊攔截器過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • java 文件大數(shù)據(jù)Excel下載實例代碼

    java 文件大數(shù)據(jù)Excel下載實例代碼

    這篇文章主要介紹了java 文件大數(shù)據(jù)Excel下載實例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-04-04

最新評論