6种负载均衡算法

来源:互联网 发布:淘宝女装修图教程 编辑:程序博客网 时间:2024/06/07 14:47

1、轮询法

  

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

public class RoundRobin {    private static Map<String,Integer> serverWeightMap=new HashMap<String,Integer>();    private static volatile Integer pos=new Integer(0);    static{        serverWeightMap.put("192.168.1.100",1);        serverWeightMap.put("192.168.1.101",1);        serverWeightMap.put("192.168.1.102",4);        serverWeightMap.put("192.168.1.103",1);        serverWeightMap.put("192.168.1.104",1);        serverWeightMap.put("192.168.1.105",3);        serverWeightMap.put("192.168.1.106",1);        serverWeightMap.put("192.168.1.107",2);        serverWeightMap.put("192.168.1.108",1);        serverWeightMap.put("192.168.1.109",1);        serverWeightMap.put("192.168.1.110",1);    }    public static void main(String[] args) {        for(int i=0;i<100;i++){            new Thread(new Runnable() {                @Override                public void run() {                    System.out.println(testRoundRobin());                }            }).start();        }    }    public static String testRoundRobin(){        //重新创建一个map,避免出现由于服务器上线和下线导致的并发问题        Map<String,Integer> serverMap=              new HashMap<String,Integer>();        serverMap.putAll(serverWeightMap);        //取得Ip地址list        Set<String> keySet=serverMap.keySet();        ArrayList<String> keyList=new ArrayList<String>();        keyList.addAll(keySet);        String server=null;        synchronized(pos){            if(pos>=keySet.size()){                pos=0;            }            server=keyList.get(pos);             pos++;        }        return server;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

2)随机(Random)法 
通过系统随机函数,根据后端服务器列表的大小值来随机选取其中一台进行访问。由概率统计论得知,随着调用量的增大,其实际效果越来越接近平均分配流浪到每一台后端服务器。

    public static String testRandom(){        //重新创建一个map,避免出现由于服务器上线和下线导致的并发问题        Map<String,Integer> serverMap=            new HashMap<String,Integer>();        serverMap.putAll(serverWeightMap);        //取得Ip地址list        Set<String> keySet=serverMap.keySet();        ArrayList<String> keyList=new ArrayList<String>();        keyList.addAll(keySet);        int randomPos = new Random().nextInt(keyList.size());        return keyList.get(randomPos);    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

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

public static String testConsumerHash(String remoteip){            //重新创建一个map,避免出现由于服务器上线和下线导致的并发问题            Map<String,Integer> serverMap=                    new HashMap<String,Integer>();            serverMap.putAll(serverWeightMap);            //取得Ip地址list            Set<String> keySet=serverMap.keySet();            ArrayList<String> keyList=new ArrayList<String>();            keyList.addAll(keySet);            int hashCode=remoteip.hashCode();            int size=keyList.size();            int serverPos=hashCode%size;            //此处服务器key是其Ip哈希值(hashCode%size)            return keyList.get(serverPos);    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

4)加权轮询法 
不同的后端服务器可能机器的配置和当前系统的负载并不相同,因此它们的抗压能力也不尽相同。给配置高的、负载低的机器更高的权重,让其处理更多请求,而低配置、负载高的机器,则给其分配较低的权重,降低其系统负载。

    public static String testWeightRoundRobin(){        //重新创建一个map,避免出现由于服务器上线和下线导致的并发问题        Map<String,Integer> serverMap=            new HashMap<String,Integer>();        serverMap.putAll(serverWeightMap);        //取得Ip地址list        Set<String> keySet=serverMap.keySet();        Iterator<String> it=keySet.iterator();        ArrayList<String> serverList=new ArrayList<String>();        while(it.hasNext()){            String server=it.next();            Integer weight=serverMap.get(server);            for(int i=0;i<weight;i++){                serverList.add(server);            }        }        String server=null;        synchronized(pos){            if(pos>=serverList.size()){                pos=0;            }            server=serverList.get(pos);             pos++;        }        return server;    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

5)加权随机(Weught Random)法 
与加权轮询法类似,加权随机法也根据后端服务器不同的配置和负载情况,配置不同的权重

    public static String testWeightRandom(){        //重新创建一个map,避免出现由于服务器上线和下线导致的并发问题        Map<String,Integer> serverMap=            new HashMap<String,Integer>();        serverMap.putAll(serverWeightMap);        //取得Ip地址list        Set<String> keySet=serverMap.keySet();        Iterator<String> it=keySet.iterator();        ArrayList<String> serverList=new ArrayList<String>();        while(it.hasNext()){            String server=it.next();            Integer weight=serverMap.get(server);            for(int i=0;i<weight;i++){                serverList.add(server);            }        }        String server=null;        int randomPos = new Random().nextInt(serverList.size());        server=serverList.get(randomPos);        return server;    }

6、最小连接数法

     最小连接数算法比较灵活和智能,由于后端服务器的配置不尽相同,对于请求的处理有快有慢,它是根据后端服务器当前的连接情况,动态地选取其中当前

积压连接数最少的一台服务器来处理当前的请求,尽可能地提高后端服务的利用效率,将负责合理地分流到每一台服务器