负载均衡算法

1、随机

java 复制代码
public class Test0119 {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("C");

        for (int i = 0; i < 10; i++) {
            System.out.println(random(list));
        }
    }

    public static String random(List<String> list){
        int index = new Random().nextInt(list.size());
        return list.get(index);
    }
}

2、轮询

java 复制代码
public class Test0119 {
    static int index = 0;

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("C");

        for (int i = 0; i < 10; i++) {
            System.out.println(roundRobin(list));
        }
    }

    public static String roundRobin(List<String> list){
        if(index < list.size()){
            return list.get(index++);
        } else{
            index = 1;
            return list.get(0);
        }
    }
}

3、加权轮询

  • 实现1
java 复制代码
public class Test0119 {
    static int index = 0;

    public static void main(String[] args) {
        Map<String, Integer> servers = new HashMap<>();
        servers.put("A", 2);
        servers.put("B", 3);
        servers.put("C", 5);
        for (int i = 0; i < 10; i++) {
            System.out.println(weightRoundRobin(servers));
        }
    }

    public static String weightRoundRobin(Map<String, Integer> servers){
        List<String> list = new ArrayList<>();
        for(String key : servers.keySet()){
            Integer value = servers.get(key);
            while(value-- != 0){
                list.add(key);
            }
        }

        if(index < list.size()){
            return list.get(index++);
        } else{
            index = 1;
            return list.get(0);
        }
    }
}
  • 实现2
java 复制代码
public class Test01191 {
    static int index = 0;

    public static void main(String[] args) {
        Map<String, Integer> servers = new HashMap<>();
        servers.put("A", 2);
        servers.put("B", 3);
        servers.put("C", 5);
        for (int i = 0; i < 10; i++) {
            System.out.println(weightRoundRobin(i, servers));
        }
    }

    public static String weightRoundRobin(int i, Map<String, Integer> servers){
        for(String key : servers.keySet()){
            Integer value = servers.get(key);
            if(i < value){
                return key;
            } else{
                i -= value;
            }
        }

        return "";
    }
}

4、加权随机

  • 实现1
java 复制代码
public class Test01191 {

    public static void main(String[] args) {
        Map<String, Integer> servers = new HashMap<>();
        servers.put("A", 2);
        servers.put("B", 3);
        servers.put("C", 5);
        for (int i = 0; i < 10; i++) {
            System.out.println(weightRandom(servers));
        }
    }

    public static String weightRandom(Map<String, Integer> servers){
        List<String> list = new ArrayList<>();
        for(String key : servers.keySet()){
            Integer value = servers.get(key);
            while(value-- != 0){
                list.add(key);
            }
        }

        int index = new Random().nextInt(list.size());
        return list.get(index);
    }
}

5、平滑加权轮询

  • 固定权限:weight(2,3,5)
  • 动态变化权限:currWeight(0,0,0)
currWeight += weight max(currWeight) result max(currWeight) -= sum(weight)
2、3、5 5 C 2、3、-5
4、6、0 6 B 4、-4、0
6、-1、5 6 A -4、-1、5
-2、2、10 10 C -2、2、0
0、5、5 5 B 0、-5、5
2、-2、10 10 C 2、-2、0
4、1、5 5 C 4、1、-5
6、4、0 6 A -4、4、0
-2、7、5 7 B -2、-3、5
0、0、10 10 C 0、0、0

解释:

先使用currWeight加上weight,比如第一行结果是2、3、5,然后取得currWeight最大值是5,数字5正好位于2、3、5的第三位,属于C服务器,接着将max(currWeight)减去weight之和,变成2、3、-5。执行结果变成新的currWeight,开始第二行。

第二行,currWeight加上weight变成4、6、0,重复上面的过程,直至最后的currWeight归零,10次会归零,因为2+3+5=10。然后循环往复。

java 复制代码
public class Test01191 {

    public static void main(String[] args) {
        int sum = 10;

        Map<String, Integer> servers = new HashMap<>();
        servers.put("A", 2);
        servers.put("B", 3);
        servers.put("C", 5);

        List<String> serverList = Arrays.asList("A", "B", "C");

        List<Integer> weight = new ArrayList<>();
        for(String key: servers.keySet()){
            weight.add(servers.get(key));
        }
        List<Integer> currWeight = Arrays.asList(0, 0, 0);

        for (int i = 0; i < 10; i++) {
            add(weight, currWeight);
            Map<Integer, String> map = find(currWeight, serverList, sum);
            for(Integer key : map.keySet()){
                System.out.println(map.get(key));
            }
        }

    }

    public static void add(List<Integer> weight, List<Integer> currWeight){
        for(int i = 0; i<weight.size(); i++){
            currWeight.set(i, currWeight.get(i) + weight.get(i));
        }
    }

    public static Map<Integer, String> find(List<Integer> currWeight, List<String> serverList, int sum){
        Map<Integer, String> map = new HashMap<>();
        int max = -1;
        int index = -1;

        for(int i=0; i<currWeight.size(); i++){
            if(currWeight.get(i) > max){
                max = currWeight.get(i);
                index = i;
            }
        }

        currWeight.set(index, currWeight.get(index) - sum);
        map.put(max, serverList.get(index));
        return map;
    }
}
相关推荐
小O的算法实验室1 天前
2026年ASOC,基于深度强化学习的无人机三维复杂环境分层自适应导航规划方法,深度解析+性能实测
算法·无人机·论文复现·智能算法·智能算法改进
郭涤生1 天前
STL vector 扩容机制与自定义内存分配器设计分析
c++·算法
༾冬瓜大侠༿1 天前
vector
c语言·开发语言·数据结构·c++·算法
Ricky111zzz1 天前
leetcode学python记录1
python·算法·leetcode·职场和发展
汀、人工智能1 天前
[特殊字符] 第58课:两个正序数组的中位数
数据结构·算法·数据库架构··数据流·两个正序数组的中位数
liu****1 天前
第16届省赛蓝桥杯大赛C/C++大学B组(京津冀)
开发语言·数据结构·c++·算法·蓝桥杯
汀、人工智能1 天前
[特殊字符] 第79课:分割等和子集
数据结构·算法·数据库架构·位运算·哈希表·分割等和子集
汀、人工智能1 天前
[特殊字符] 第74课:完全平方数
数据结构·算法·数据库架构·图论·bfs·完全平方数
CoderCodingNo1 天前
【GESP】C++四、五级练习题 luogu-P1177 【模板】排序
数据结构·c++·算法
Proxy_ZZ01 天前
从零实现LDPC比特翻转译码器:C语言实战与底层逻辑解析
c语言·算法