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

java实现一个平滑加权轮询算法

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

java实现一个平滑加权轮询算法

package com.lsm.work.weightedroundrobin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


public class SmoothWeightedRobinTest {

    
    private List serverList = new ArrayList<>();
    
    private Integer weightSum;


    public SmoothWeightedRobinTest(List serverList) {
        this.serverList = serverList;
        this.weightSum = serverList.stream().map(ServerConfig::getWeight).reduce((x, y) -> x += y).get();
    }

    public static void main(String[] args) {
        List list = new ArrayList<>();

        list.add(new ServerConfig("A", 5, 5));
        list.add(new ServerConfig("B", 1, 1));
        list.add(new ServerConfig("C", 1, 1));
        // 初始每个实例的当前有效权重为配置权重(初始化权重),并求得配置权重和weightSum;
        //int weightSum = list.stream().map(ServerConfig::getWeight).reduce((x, y) -> x += y).get();
        SmoothWeightedRobinTest test = new SmoothWeightedRobinTest(list);
        test.test();
    }

    // 临时存放每次选中后的当前权重
    List tempList = new ArrayList<>();
    // 所有选中的列表
    List 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;
        }
    }

}


每七次一个循环
[A, A, B, A, C, A, A]

本算法主要是根据 https://www.jianshu.com/p/836193df61db原理实现

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

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

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