栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Nignx平滑加权轮询算法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Nignx平滑加权轮询算法

Nignx平滑加权轮询算法

public class Weight {
    private Integer index;
    private Integer weight;
    private Integer currentWeight;

    public Weight(Integer index, Integer weight, Integer currentWeight) {
        this.index = index;
        this.weight = weight;
        this.currentWeight = currentWeight;
    }

    public Integer getIndex() {
        return index;
    }

    public void setIndex(Integer index) {
        this.index = index;
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public Integer getCurrentWeight() {
        return currentWeight;
    }

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

public class Test {
    public static void main(String[] args) {
        int countA = 0;
        int countB = 0;
        for (int i = 0; i < 9985; i++) {
            if(weightRoundRobin()==0) {
                countA++;
            }else{
                countB++;
            }

        }
        System.out.println(countA);
        System.out.println(countB);
    }
    
    private static Map weightMap = new linkedHashMap<>();

    static {
        if (weightMap.isEmpty()) {
            weightMap.put(0, new Weight(0, 15, 0));
            weightMap.put(1, new Weight(1, 85, 0));
//            weightMap.put(2, new Weight(2, 1, 0));
        }
    }

    public static int weightRoundRobin() {
        int totalWeight = 0;
        for (Weight weight : weightMap.values()) {
            totalWeight += weight.getWeight();
        }
        //1 currentWeight+=weight
        for (Weight weight : weightMap.values()) {
            weight.setCurrentWeight(weight.getCurrentWeight() + weight.getWeight());
        }
        //2 max(currentWeight)
        Weight maxCurrentWeight = null;
        for (Weight weight : weightMap.values()) {
            if (maxCurrentWeight == null || weight.getCurrentWeight() > maxCurrentWeight.getCurrentWeight()) {
                maxCurrentWeight = weight;
            }
        }
        //3. max(currentWeight) -= sum(weight)
        maxCurrentWeight.setCurrentWeight(maxCurrentWeight.getCurrentWeight() - totalWeight);
        return maxCurrentWeight.getIndex();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/710096.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号