负载均衡算法实现

负载均衡算法实现

负载均衡介绍

负责均衡主要有以下五种方法实现:
1、轮询法

将请求按顺序轮流地分配到后端服务器上,它均衡地对待后端的每一台服务器,而不关心服务器实际的连接数和当前的系统负载;

2、随机法

通过系统的随机算法,根据后端服务器的列表大小值来随机选取其中的一台服务器进行访问。由概率统计理论可以得知,随着客户端调用服务端的次数增多, 其实际效果越来越接近于平均分配调用量到后端的每一台服务器,也就是轮询的结果;

3、源地址哈希法

源地址哈希的思想是根据获取客户端的IP地址,通过哈希函数计算得到的一个数值,用该数值对服务器列表的大小进行取模运算,得到的结果便是客服端要访问服务器的序号。采用源地址哈希法进行负载均衡,同一IP地址的客户端,当后端服务器列表不变时,它每次都会映射到同一台后端服务器进行访问;

4、加权轮询法

不同的后端服务器可能机器的配置和当前系统的负载并不相同,因此它们的抗压能力也不相同。给配置高、负载低的机器配置更高的权重,让其处理更多的请;而配置低、负载高的机器,给其分配较低的权重,降低其系统负载,加权轮询能很好地处理这一问题,并将请求顺序且按照权重分配到后端;

5、加权随机法

与加权轮询法一样,加权随机法也根据后端机器的配置,系统的负载分配不同的权重。不同的是,它是按照权重随机请求后端服务器,而非顺序;

代码实现

1.轮询方式

/**
 * 1.负载均衡算法的轮询方式
 */
public class poll {
    static HashMap<String, Integer> ipMap = new HashMap<>();
    static {
        ipMap.put("192.168.110.10",1);
        ipMap.put("192.168.110.11",1);
        ipMap.put("192.168.110.12",1);
        ipMap.put("192.168.110.13",1);
    }
    Integer pos  = 0;
    public String RoundRobin(){
        //1.将map中数据取出放到conCurrentmap中,conCurrentmap能够避免多线程时,数据出错
        ConcurrentHashMap<String, Integer> cMap = new ConcurrentHashMap<>();
        cMap.putAll(ipMap);
        //2.取出Key,放到set中,可以去重
        Set<String> ipset = cMap.keySet();
        //3.set放到list中
        List<String> iplist = new ArrayList<>();
        iplist.addAll(ipset);
        String serverName = null;
        synchronized (pos){
            if (pos >= iplist.size()){
                pos = 0;
            }
            serverName = iplist.get(pos);
            pos++;
        }
        return serverName;
    }

    public static void main(String[] args) {
        poll poll = new poll();
        for (int i = 0; i < 10; i++) {
            String name = poll.RoundRobin();
            System.out.println(name);
        }
    }
}

2.加权轮询法

public class WeightPoll {
    static HashMap<String, Integer> ipMap = new java.util.HashMap<>();
    static {
        ipMap.put("192.168.110.10",1);
        ipMap.put("192.168.110.11",2);
        ipMap.put("192.168.110.12",3);
        ipMap.put("192.168.110.13",4);
    }
    Integer pos = 0;
    public String WeightRobin(){
        ConcurrentHashMap<String, Integer> ipServerMap = new ConcurrentHashMap<>();
        ipServerMap.putAll(ipMap);
        Set<String> ipSet = ipServerMap.keySet();
        Iterator<String> iterator = ipSet.iterator();
        //定义一个list放所有server
        List<String> iplist = new ArrayList<>();
        //循环放入iplist中
        while (iterator.hasNext()){
            String name = iterator.next();
            Integer num = ipServerMap.get(name);
            for (Integer i = 0; i < num; i++) {
                iplist.add(name);
            }
        }
        String serverName = null;
        if (pos >= iplist.size()){
            pos = 0;
        }
        serverName = iplist.get(pos);
        pos++;
        return serverName;
    }
    
    public static void main(String[] args) {
        WeightPoll weightPoll = new WeightPoll();
        for (int i = 0; i < 11; i++) {
            System.out.println(weightPoll.WeightRobin());
        }
    }
    
}

3.随机法

public class RandomAlgrithm {
    static HashMap<String, Integer> ipMap = new java.util.HashMap<>();
    static {
        ipMap.put("192.168.110.10",1);
        ipMap.put("192.168.110.11",2);
        ipMap.put("192.168.110.12",3);
        ipMap.put("192.168.110.13",4);
    }
    public String Random(){
        ConcurrentHashMap<String, Integer> ipConMap = new ConcurrentHashMap<>();
        ipConMap.putAll(ipMap);
        Set<String> ipSet = ipConMap.keySet();
        List<String> ipList = new ArrayList<>();
        ipList.addAll(ipSet);
        int num = new Random().nextInt(ipList.size());
        String serverName = ipList.get(num);
        return serverName;
    }
    
    public static void main(String[] args) {
        RandomAlgrithm randomAlgrithm = new RandomAlgrithm();
        for (int i = 0; i < 10; i++) {
            System.out.println(randomAlgrithm.Random());
        }
    }
}

4.加权随机

public class WeightRandom {
    static HashMap<String, Integer> ipMap = new java.util.HashMap<>();
    static {
        ipMap.put("192.168.110.10",1);
        ipMap.put("192.168.110.11",2);
        ipMap.put("192.168.110.12",3);
        ipMap.put("192.168.110.13",4);
    }

    public String WeightRandom(){
        ConcurrentHashMap<String, Integer> ipConMap = new ConcurrentHashMap<>();
        ipConMap.putAll(ipMap);
        Set<String> ipSet = ipConMap.keySet();
        Iterator<String> iterator = ipSet.iterator();
        List<String> iplist = new ArrayList<>();
        while (iterator.hasNext()){
            String name = iterator.next();
            Integer num = ipConMap.get(name);
            for (Integer i = 0; i < num; i++) {
                iplist.add(name);
            }
        }
        int pos = new Random().nextInt(iplist.size());
        return iplist.get(pos);
    }
    public static void main(String[] args) {
        WeightRandom weightRandom = new WeightRandom();
        for (int i = 0; i < 10; i++) {
            System.out.println(weightRandom.WeightRandom());
        }
    }
}

5.源地址哈希法

public class AddressHash {
    static HashMap<String, Integer> ipMap = new java.util.HashMap<>();
    static {
        ipMap.put("192.168.110.10",1);
        ipMap.put("192.168.110.11",2);
        ipMap.put("192.168.110.12",3);
        ipMap.put("192.168.110.13",4);
    }
    public String ipHash(String clientIp){
        ConcurrentHashMap<String, Integer> ipConMap = new ConcurrentHashMap<>();
        ipConMap.putAll(ipMap);
        Set<String> ipSet = ipConMap.keySet();
        List<String> iplist = new ArrayList<>();
        iplist.addAll(ipSet);
        int hashCode = clientIp.hashCode();
        int num = hashCode % iplist.size();
        return iplist.get(num);
    }

    public static void main(String[] args) {
        AddressHash addressHash = new AddressHash();
        for (int i = 0; i < 1; i++) {
            System.out.println(addressHash.ipHash("192.168.110.57"));
            System.out.println(addressHash.ipHash("192.168.110.47"));
            System.out.println(addressHash.ipHash("192.168.110.23"));
            System.out.println(addressHash.ipHash("192.168.110.59"));
        }
    }
}

你可能感兴趣的:(负载均衡,算法,运维)