美文网首页
java笔记-代码简单实现负载均衡

java笔记-代码简单实现负载均衡

作者: 一个喜欢烧砖的人 | 来源:发表于2018-07-31 10:23 被阅读42次
    本文使用java代码实现简单的负载均衡

    服务器负载均衡选择

    • 随机法
    • 加权随机法
    • 轮训法
    • 加权轮训法
    废话不多说,直接撸代码:

    模拟的服务器地址池:

        static class IpMap {
            public static HashMap<String, Integer> serverWeightMap = new HashMap<String, Integer>();
            static {
                serverWeightMap.put("192.168.30.201", 1);
                serverWeightMap.put("192.168.30.202", 2);
                serverWeightMap.put("192.168.30.203", 3);
                serverWeightMap.put("192.168.30.204", 4);
                serverWeightMap.put("192.168.30.205", 2);
                serverWeightMap.put("192.168.30.206", 1);
                serverWeightMap.put("192.168.30.207", 5);
            }
        }
    

    1、轮训法

    static class RoundRobin {
            private static Integer pos = 0;
    
            public static String getServer() {
                Map<String, Integer> serverMap = new HashMap<String, Integer>();
                serverMap.putAll(IpMap.serverWeightMap);
    
                Set<String> keySet = serverMap.keySet();
    
                List<String> keyList = new ArrayList<String>();
    
                keyList.addAll(keySet);
    
                String server;
                synchronized (pos) {
                    if (pos > keySet.size()) {
                        pos = 0;
                    }
                    server = keyList.get(pos);
                    pos++;
                }
                return server;
            }
        }
    

    2、随机法

    static class Random {
            public static String getServer() {
                Map<String, Integer> serverMap = new HashMap<String, Integer>();
                serverMap.putAll(IpMap.serverWeightMap);
                Set<String> keySet = serverMap.keySet();
                List<String> keyList = new ArrayList<String>();
                keyList.addAll(keySet);
                java.util.Random random = new java.util.Random();
                int pos = random.nextInt(keyList.size());
                return keyList.get(pos);
            }
    
    
        }
    

    3、加权轮训法

    static class WeightRounRabin {
            private static Integer pos = 0;
    
            public static String getServer() {
                Map<String, Integer> serverMap = new HashMap<String, Integer>();
                serverMap.putAll(IpMap.serverWeightMap);
    
                Set<String> keySet = serverMap.keySet();
                List<String> keyList = new ArrayList<String>();
    
                Iterator<String> iterator = keySet.iterator();
                while (iterator.hasNext()) {
                    String server = iterator.next();
                    int weight = serverMap.get(server);
                    for (int i = 0; i < weight; i++) {
                        keyList.add(server);
                    }
                }
                String server;
                synchronized (pos) {
                    if (pos > keySet.size()) {
                        pos = 0;
                    }
                    server = keyList.get(pos);
                    pos++;
                }
                return server;
            }
        }
    

    4、加权随机法

    static class WeightRandom {
            public static String getServer() {
                Map<String, Integer> serverMap = new HashMap<String, Integer>();
                serverMap.putAll(IpMap.serverWeightMap);
    
                Set<String> keySet = serverMap.keySet();
                List<String> keyList = new ArrayList<String>();
    
                Iterator<String> iterator = keySet.iterator();
                while (iterator.hasNext()) {
                    String server = iterator.next();
                    int weight = serverMap.get(server);
                    for (int i = 0; i < weight; i++) {
                        keyList.add(server);
                    }
                }
    
                java.util.Random random = new java.util.Random();
                int pos = random.nextInt(keyList.size());
                return keyList.get(pos);
            }
    
    
        }
    

    相信聪明的你早已看懂....

    相关文章

      网友评论

          本文标题:java笔记-代码简单实现负载均衡

          本文链接:https://www.haomeiwen.com/subject/gijcvftx.html