负载均衡算法

常见的负载均衡算法主要有:
1、随机算法
(1)简单随机算法
(2)加权随机算法
2、轮询算法
(1)简单轮循算法
(2)加权轮询算法
(3)平滑加权轮询算法
3、一致性哈希算法
4、最小活跃数算法


package loadbalance;

import java.util.*;

public class SuchLoadBalance {

    public static void main(String[] args) {

        LoadBalance lb = new WeightRoundLoadBalance();

        for (int i = 0; i < 100; i++) {
            System.out.println(lb.getNext());
        }

    }

    private static class ServerIps {

        public static final List<String> SERVER_IP_LIST
                 = Arrays.asList("1.1.1.1", "2.2.2.2", "3.3.3.3", "4.4.4.4", "5.5.5.5");

        public static final Map<String, Integer> SERVER_IP_MAP
                = new HashMap<>();

        static {
            SERVER_IP_MAP.put("1.1.1.1", 10);  //0-9
            SERVER_IP_MAP.put("2.2.2.2", 20);  //10-29
            SERVER_IP_MAP.put("3.3.3.3", 20);  //30-49
            SERVER_IP_MAP.put("4.4.4.4", 45);  //50-94
            SERVER_IP_MAP.put("5.5.5.5", 5);   //95-99
        }
    }

    interface LoadBalance {
        String getNext();
    }

    /**
     * 随机负载均衡
     */
    private static class RandomLoadBalance implements LoadBalance {

        @Override
        public String getNext() {
            int size = ServerIps.SERVER_IP_LIST.size();
            int randomValue = new Random().nextInt(size);
            return ServerIps.SERVER_IP_LIST.get(randomValue);
        }

    }

    /**
     * 轮循负载均衡
     */
    private static class RoundLoadBalance implements LoadBalance {

        int currentIndex = 0;

        int size = ServerIps.SERVER_IP_LIST.size();

        @Override
        public String getNext() {
            //防止溢出
            if (currentIndex < 0) {
                currentIndex = 0;
            }
            return ServerIps.SERVER_IP_LIST.get((currentIndex++) % size);
        }

    }

    /**
     * 加权随机负载均衡
     */
    private static class WeightRandomLoadBalance implements LoadBalance {

        int dataArea = 0;

        public WeightRandomLoadBalance() {
            for (Map.Entry<String, Integer> entry : ServerIps.SERVER_IP_MAP.entrySet()) {
                dataArea += entry.getValue();
            }
        }

        @Override
        public String getNext() {
            int randomValue = new Random().nextInt(dataArea);
            System.out.println("random value : " + randomValue);
            for (Map.Entry<String, Integer> entry : ServerIps.SERVER_IP_MAP.entrySet()) {
                if (randomValue < entry.getValue()) {
                    return entry.getKey();
                }
                randomValue -= entry.getValue();
            }
            return null;
        }

    }

    /**
     * 加权轮循负载均衡
     */
    private static class WeightRoundLoadBalance implements LoadBalance {

        int dataArea = 0;

        int currentIndex = 0;

        public WeightRoundLoadBalance() {
            for (Map.Entry<String, Integer> entry : ServerIps.SERVER_IP_MAP.entrySet()) {
                dataArea += entry.getValue();
            }
        }

        @Override
        public String getNext() {
            //防止溢出
            if (currentIndex < 0) {
                currentIndex = 0;
            }
            System.out.println("current value : " + currentIndex);
            int temp = currentIndex++;
            for (Map.Entry<String, Integer> entry : ServerIps.SERVER_IP_MAP.entrySet()) {
                if (temp < entry.getValue()) {
                    return entry.getKey();
                }
                temp -= entry.getValue();
            }
            return null;
        }

    }

    /**
     * 平滑加权轮询负载均衡
     */
    private static class SmoothWeightRoundLoadBalance implements LoadBalance {

        @Override
        public String getNext() {
            return null;
        }

    }

    /**
     * 一致性哈希负载均衡
     */
    private static class ConsistencyHashLoadBalance implements LoadBalance {

        @Override
        public String getNext() {
            return null;
        }

    }

    /**
     * 最小活跃数负载均衡
     */
    public static class MinActiveLoadBalanace implements LoadBalance {

        @Override
        public String getNext() {
            return null;
        }

    }




}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。