• 平滑加权轮询算法


    代码

    package cn.com.codingce.随做.平滑加权轮询算法;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    /**
     * 平滑加权轮询算法
     *
     * @author inke219223m
     */
    public class SmoothWeightedRobinTest {
    
        /**
         * 实例列表
         */
        private List<ServerConfig> serverList = new ArrayList<>();
        /**
         * 最大权重
         */
        private Integer weightSum;
    
    
        public SmoothWeightedRobinTest(List<ServerConfig> serverList) {
            this.serverList = serverList;
            this.weightSum = serverList.stream().map(ServerConfig::getWeight).reduce((x, y) -> x += y).get();
        }
    
        public static void main(String[] args) {
            List<ServerConfig> list = new ArrayList<>();
    
            list.add(new ServerConfig("192.168.1.1", 5, 5));
            list.add(new ServerConfig("192.168.1.2", 1, 1));
            list.add(new ServerConfig("192.168.1.3", 1, 1));
            // 初始每个实例的当前有效权重为配置权重(初始化权重),并求得配置权重和weightSum;
            //int weightSum = list.stream().map(ServerConfig::getWeight).reduce((x, y) -> x += y).get();
            SmoothWeightedRobinTest test = new SmoothWeightedRobinTest(list);
            test.test();
        }
    
        // 临时存放每次选中后的当前权重
        List<ServerConfig> tempList = new ArrayList<>();
        // 所有选中的列表
        List<String> chooseList = new ArrayList<>();
    
        public void test() {
            System.out.println("初始化当前权重:" + serverList.toString());
            System.out.println();
            for (int i = 1; i <= 20; i++) {
                this.nextServerIndex(i);
            }
            System.out.println(chooseList);
        }
    
        public ServerConfig nextServerIndex(int index) {
            // 选出当前有效权重最大的实例,将当前有效权重currentWeight减去所有实例的"权重和"(weightSum),且变量tmpSv指向此位置;
            ServerConfig max = Collections.max(serverList, Comparator.comparingInt(ServerConfig::getCurrentWeight));
            // 选中的实例
            ServerConfig tmpSv = null;
    
            for (ServerConfig serverConfig : serverList) {
                if (max.equals(serverConfig)) {
                    serverConfig.setCurrentWeight(serverConfig.getCurrentWeight() - weightSum);
                    if (tmpSv == null || serverConfig.getCurrentWeight() > tmpSv.getCurrentWeight()) {
                        tmpSv = serverConfig;
                    }
                }
                //为了打印
                try {
                    tempList.add(serverConfig.clone());
                } catch (CloneNotSupportedException e) {
                    e.printStackTrace();
                }
                //将每个实例的当前有效权重currentWeight都加上配置权重weight;
                serverConfig.setCurrentWeight(serverConfig.getCurrentWeight() + serverConfig.getWeight());
            }
            //选中后的当前权重
            System.out.println("第" + index + "次选中后的当前权重:" + tempList.toString());
            tempList.clear();
            // 选中的实例
            System.out.println("第" + index + "次选中的实例:" + tmpSv.getName());
            // 全部选中的实例
            chooseList.add(tmpSv.getName());
            // 选中前的当前权重
            System.out.println("第" + (index + 1) + "次选中前的当前权重:" + serverList.toString());
            System.out.println();
    
            return tmpSv;
        }
    
    
        public static class ServerConfig {
    
            //服务名称
            public String name;
    
            //初始权重
            public int weight;
    
            //当前权重
            public int currentWeight;
    
            public ServerConfig() {
            }
    
            public ServerConfig(String name, int weight) {
                this.name = name;
                this.weight = weight;
            }
    
            public ServerConfig(String name, int weight, int currentWeight) {
                this.name = name;
                this.weight = weight;
                this.currentWeight = currentWeight;
            }
    
            public int getWeight() {
                return weight;
            }
    
            public void setWeight(int weight) {
                this.weight = weight;
            }
    
            public int getCurrentWeight() {
                return currentWeight;
            }
    
            public void setCurrentWeight(int currentWeight) {
                this.currentWeight = currentWeight;
            }
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            @Override
            public String toString() {
                return "ServerConfig{" + "name='" + name + '\'' + ", curr=" + currentWeight + '}';
            }
    
            @Override
            public ServerConfig clone() throws CloneNotSupportedException {
    //            // 使用Jackson序列化进行深拷贝
    //            ObjectMapper objectMapper = new ObjectMapper();
    //            User copyUser = objectMapper.readValue(objectMapper.writeValueAsString(user), User.class);
    
                ServerConfig serverConfig = new ServerConfig();
                serverConfig.setCurrentWeight(this.currentWeight);
                serverConfig.setName(this.name);
                serverConfig.setWeight(this.weight);
                return serverConfig;
            }
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161

    日志

    /Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/bin/java -Dvisualvm.id=125005792986662 -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=53787:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home/jre/lib/rt.jar:/Users/mxz/mxz-code/gitee/codingce-leetcode/new_code/out/production/new_code cn.com.codingce.随做.平滑加权轮询算法.SmoothWeightedRobinTest
    初始化当前权重:[ServerConfig{name='192.168.1.1', curr=5}, ServerConfig{name='192.168.1.2', curr=1}, ServerConfig{name='192.168.1.3', curr=1}]
    
    第1次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=-2}, ServerConfig{name='192.168.1.2', curr=1}, ServerConfig{name='192.168.1.3', curr=1}]
    第1次选中的实例:192.168.1.1
    第2次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=3}, ServerConfig{name='192.168.1.2', curr=2}, ServerConfig{name='192.168.1.3', curr=2}]
    
    第2次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=-4}, ServerConfig{name='192.168.1.2', curr=2}, ServerConfig{name='192.168.1.3', curr=2}]
    第2次选中的实例:192.168.1.1
    第3次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=1}, ServerConfig{name='192.168.1.2', curr=3}, ServerConfig{name='192.168.1.3', curr=3}]
    
    第3次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=1}, ServerConfig{name='192.168.1.2', curr=-4}, ServerConfig{name='192.168.1.3', curr=3}]
    第3次选中的实例:192.168.1.2
    第4次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=6}, ServerConfig{name='192.168.1.2', curr=-3}, ServerConfig{name='192.168.1.3', curr=4}]
    
    第4次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=-1}, ServerConfig{name='192.168.1.2', curr=-3}, ServerConfig{name='192.168.1.3', curr=4}]
    第4次选中的实例:192.168.1.1
    第5次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=4}, ServerConfig{name='192.168.1.2', curr=-2}, ServerConfig{name='192.168.1.3', curr=5}]
    
    第5次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=4}, ServerConfig{name='192.168.1.2', curr=-2}, ServerConfig{name='192.168.1.3', curr=-2}]
    第5次选中的实例:192.168.1.3
    第6次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=9}, ServerConfig{name='192.168.1.2', curr=-1}, ServerConfig{name='192.168.1.3', curr=-1}]
    
    第6次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=2}, ServerConfig{name='192.168.1.2', curr=-1}, ServerConfig{name='192.168.1.3', curr=-1}]
    第6次选中的实例:192.168.1.1
    第7次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=7}, ServerConfig{name='192.168.1.2', curr=0}, ServerConfig{name='192.168.1.3', curr=0}]
    
    第7次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=0}, ServerConfig{name='192.168.1.2', curr=0}, ServerConfig{name='192.168.1.3', curr=0}]
    第7次选中的实例:192.168.1.1
    第8次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=5}, ServerConfig{name='192.168.1.2', curr=1}, ServerConfig{name='192.168.1.3', curr=1}]
    
    第8次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=-2}, ServerConfig{name='192.168.1.2', curr=1}, ServerConfig{name='192.168.1.3', curr=1}]
    第8次选中的实例:192.168.1.1
    第9次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=3}, ServerConfig{name='192.168.1.2', curr=2}, ServerConfig{name='192.168.1.3', curr=2}]
    
    第9次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=-4}, ServerConfig{name='192.168.1.2', curr=2}, ServerConfig{name='192.168.1.3', curr=2}]
    第9次选中的实例:192.168.1.1
    第10次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=1}, ServerConfig{name='192.168.1.2', curr=3}, ServerConfig{name='192.168.1.3', curr=3}]
    
    第10次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=1}, ServerConfig{name='192.168.1.2', curr=-4}, ServerConfig{name='192.168.1.3', curr=3}]
    第10次选中的实例:192.168.1.2
    第11次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=6}, ServerConfig{name='192.168.1.2', curr=-3}, ServerConfig{name='192.168.1.3', curr=4}]
    
    第11次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=-1}, ServerConfig{name='192.168.1.2', curr=-3}, ServerConfig{name='192.168.1.3', curr=4}]
    第11次选中的实例:192.168.1.1
    第12次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=4}, ServerConfig{name='192.168.1.2', curr=-2}, ServerConfig{name='192.168.1.3', curr=5}]
    
    第12次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=4}, ServerConfig{name='192.168.1.2', curr=-2}, ServerConfig{name='192.168.1.3', curr=-2}]
    第12次选中的实例:192.168.1.3
    第13次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=9}, ServerConfig{name='192.168.1.2', curr=-1}, ServerConfig{name='192.168.1.3', curr=-1}]
    
    第13次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=2}, ServerConfig{name='192.168.1.2', curr=-1}, ServerConfig{name='192.168.1.3', curr=-1}]
    第13次选中的实例:192.168.1.1
    第14次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=7}, ServerConfig{name='192.168.1.2', curr=0}, ServerConfig{name='192.168.1.3', curr=0}]
    
    第14次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=0}, ServerConfig{name='192.168.1.2', curr=0}, ServerConfig{name='192.168.1.3', curr=0}]
    第14次选中的实例:192.168.1.1
    第15次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=5}, ServerConfig{name='192.168.1.2', curr=1}, ServerConfig{name='192.168.1.3', curr=1}]
    
    第15次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=-2}, ServerConfig{name='192.168.1.2', curr=1}, ServerConfig{name='192.168.1.3', curr=1}]
    第15次选中的实例:192.168.1.1
    第16次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=3}, ServerConfig{name='192.168.1.2', curr=2}, ServerConfig{name='192.168.1.3', curr=2}]
    
    第16次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=-4}, ServerConfig{name='192.168.1.2', curr=2}, ServerConfig{name='192.168.1.3', curr=2}]
    第16次选中的实例:192.168.1.1
    第17次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=1}, ServerConfig{name='192.168.1.2', curr=3}, ServerConfig{name='192.168.1.3', curr=3}]
    
    第17次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=1}, ServerConfig{name='192.168.1.2', curr=-4}, ServerConfig{name='192.168.1.3', curr=3}]
    第17次选中的实例:192.168.1.2
    第18次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=6}, ServerConfig{name='192.168.1.2', curr=-3}, ServerConfig{name='192.168.1.3', curr=4}]
    
    第18次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=-1}, ServerConfig{name='192.168.1.2', curr=-3}, ServerConfig{name='192.168.1.3', curr=4}]
    第18次选中的实例:192.168.1.1
    第19次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=4}, ServerConfig{name='192.168.1.2', curr=-2}, ServerConfig{name='192.168.1.3', curr=5}]
    
    第19次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=4}, ServerConfig{name='192.168.1.2', curr=-2}, ServerConfig{name='192.168.1.3', curr=-2}]
    第19次选中的实例:192.168.1.3
    第20次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=9}, ServerConfig{name='192.168.1.2', curr=-1}, ServerConfig{name='192.168.1.3', curr=-1}]
    
    第20次选中后的当前权重:[ServerConfig{name='192.168.1.1', curr=2}, ServerConfig{name='192.168.1.2', curr=-1}, ServerConfig{name='192.168.1.3', curr=-1}]
    第20次选中的实例:192.168.1.1
    第21次选中前的当前权重:[ServerConfig{name='192.168.1.1', curr=7}, ServerConfig{name='192.168.1.2', curr=0}, ServerConfig{name='192.168.1.3', curr=0}]
    
    [192.168.1.1, 192.168.1.1, 192.168.1.2, 192.168.1.1, 192.168.1.3, 192.168.1.1, 192.168.1.1, 192.168.1.1, 192.168.1.1, 192.168.1.2, 192.168.1.1, 192.168.1.3, 192.168.1.1, 192.168.1.1, 192.168.1.1, 192.168.1.1, 192.168.1.2, 192.168.1.1, 192.168.1.3, 192.168.1.1]
    
    Process finished with exit code 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
  • 相关阅读:
    计算机毕业设计springboot+vue+elementUI高考填报志愿综合参考系统
    ESB+MDM预置样例测试总结
    C++ —— 二叉搜索树
    混合灰狼和布谷鸟搜索优化算法(Matlab完整代码实现)
    分享几个简单实用的 JavaScript 优雅小技巧!
    优化group By查询很慢的问题建议使用DISTINCT字段做分组的查询优化
    FPGA基础篇目录
    类和对象!
    【Vue】Element开发笔记
    在windows上配置git支持多账号
  • 原文地址:https://blog.csdn.net/weixin_43874301/article/details/125533019