服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 详解Java实现负载均衡的几种算法代码

详解Java实现负载均衡的几种算法代码

2020-08-06 15:06魔流剑 Java教程

本篇文章主要介绍了详解Java实现负载均衡的几种算法代码 ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

本篇文章主要介绍Java实现负载均衡的几种算法,具体如下:

轮询:

?
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
56
57
58
package class2.zookeeper.loadbalance;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
 
/**
 * 負載均衡算法,輪詢法
 * @author guoy
 *
 */
public class TestRoundRobin {
 
  
  static Map<String,Integer> serverWeigthMap = new HashMap<String,Integer>();
 
   static{
    serverWeigthMap.put("192.168.1.12", 1);
    serverWeigthMap.put("192.168.1.13", 1);
    serverWeigthMap.put("192.168.1.14", 2);
    serverWeigthMap.put("192.168.1.15", 2);
    serverWeigthMap.put("192.168.1.16", 3);
    serverWeigthMap.put("192.168.1.17", 3);
    serverWeigthMap.put("192.168.1.18", 1);
    serverWeigthMap.put("192.168.1.19", 2);
  }
   Integer pos = 0;
   public String roundRobin()
    {
      //重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
      Map<String,Integer> serverMap = new HashMap<String,Integer>();
      serverMap.putAll(serverWeigthMap);
      //獲取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;
    }
    
    public static void main(String[] args) {
      TestRoundRobin robin = new TestRoundRobin();
      for (int i = 0; i < 20; i++) {
        String serverIp = robin.roundRobin();
        System.out.println(serverIp);
      }
    }
}

加权轮询:

?
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
56
57
58
59
60
package class2.zookeeper.loadbalance;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
 
/**
 * 加權隨機载均衡算法
 * @author guoy
 *
 */
public class TestWeightRandom {
  
  static Map<String,Integer> serverWeigthMap = new HashMap<String,Integer>();
 
   static{
    serverWeigthMap.put("192.168.1.12", 1);
    serverWeigthMap.put("192.168.1.13", 1);
    serverWeigthMap.put("192.168.1.14", 2);
    serverWeigthMap.put("192.168.1.15", 2);
    serverWeigthMap.put("192.168.1.16", 3);
    serverWeigthMap.put("192.168.1.17", 3);
    serverWeigthMap.put("192.168.1.18", 1);
    serverWeigthMap.put("192.168.1.19", 2);
  }
 
  public static String weightRandom()
  {
    //重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
    Map<String,Integer> serverMap = new HashMap<String,Integer>();
    serverMap.putAll(serverWeigthMap);
    //獲取ip列表list
    Set<String> keySet = serverMap.keySet();
    Iterator<String> it = keySet.iterator();
 
    List<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);
      }
    }   
    Random random = new Random();
    int randomPos = random.nextInt(serverList.size());
    
    String server = serverList.get(randomPos);
    return server;
  }
  
  public static void main(String[] args) {
    String serverIp = weightRandom();
    System.out.println(serverIp);
  }
}

随机:

?
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
package class2.zookeeper.loadbalance;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;
 
/**
 * 隨機负载均衡算法
 * @author guoy
 *
 */
public class TestRandom {
  
  static Map<String,Integer> serverWeigthMap = new HashMap<String,Integer>();
 
   static{
    serverWeigthMap.put("192.168.1.12", 1);
    serverWeigthMap.put("192.168.1.13", 1);
    serverWeigthMap.put("192.168.1.14", 2);
    serverWeigthMap.put("192.168.1.15", 2);
    serverWeigthMap.put("192.168.1.16", 3);
    serverWeigthMap.put("192.168.1.17", 3);
    serverWeigthMap.put("192.168.1.18", 1);
    serverWeigthMap.put("192.168.1.19", 2);
  }
 
  public static String random()
  {
    //重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
    Map<String,Integer> serverMap = new HashMap<String,Integer>();
    serverMap.putAll(serverWeigthMap);
    //獲取ip列表list
    Set<String> keySet = serverMap.keySet();
    ArrayList<String> keyList = new ArrayList<String>();
    keyList.addAll(keySet);
    
    Random random = new Random();
    int randomPos = random.nextInt(keyList.size());
    
    String server = keyList.get(randomPos);
    return server;
  }
  
  public static void main(String[] args) {
    String serverIp = random();
    System.out.println(serverIp);
  }
}

加权随机:

?
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
56
57
58
59
60
package class2.zookeeper.loadbalance;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
 
/**
 * 加權隨機载均衡算法
 * @author guoy
 *
 */
public class TestWeightRandom {
  
  static Map<String,Integer> serverWeigthMap = new HashMap<String,Integer>();
 
   static{
    serverWeigthMap.put("192.168.1.12", 1);
    serverWeigthMap.put("192.168.1.13", 1);
    serverWeigthMap.put("192.168.1.14", 2);
    serverWeigthMap.put("192.168.1.15", 2);
    serverWeigthMap.put("192.168.1.16", 3);
    serverWeigthMap.put("192.168.1.17", 3);
    serverWeigthMap.put("192.168.1.18", 1);
    serverWeigthMap.put("192.168.1.19", 2);
  }
 
  public static String weightRandom()
  {
    //重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
    Map<String,Integer> serverMap = new HashMap<String,Integer>();
    serverMap.putAll(serverWeigthMap);
    //獲取ip列表list
    Set<String> keySet = serverMap.keySet();
    Iterator<String> it = keySet.iterator();
 
    List<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);
      }
    }   
    Random random = new Random();
    int randomPos = random.nextInt(serverList.size());
    
    String server = serverList.get(randomPos);
    return server;
  }
  
  public static void main(String[] args) {
    String serverIp = weightRandom();
    System.out.println(serverIp);
  }
}

ip hash:

?
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
56
package class2.zookeeper.loadbalance;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
 
/**
 * 负载均衡 ip_hash算法
 * @author guoy
 *
 */
public class TestIpHash {
 
  
  static Map<String,Integer> serverWeigthMap = new HashMap<String,Integer>();
 
   static{
    serverWeigthMap.put("192.168.1.12", 1);
    serverWeigthMap.put("192.168.1.13", 1);
    serverWeigthMap.put("192.168.1.14", 2);
    serverWeigthMap.put("192.168.1.15", 2);
    serverWeigthMap.put("192.168.1.16", 3);
    serverWeigthMap.put("192.168.1.17", 3);
    serverWeigthMap.put("192.168.1.18", 1);
    serverWeigthMap.put("192.168.1.19", 2);
  }
 
   /**
   * 获取请求服务器地址
   * @param remoteIp 负载均衡服务器ip
   * @return
   */
  public static String ipHash(String remoteIp)
  {
    //重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
    Map<String,Integer> serverMap = new HashMap<String,Integer>();
    serverMap.putAll(serverWeigthMap);
    //獲取ip列表list
    Set<String> keySet = serverMap.keySet();
    ArrayList<String> keyList = new ArrayList<String>();
    keyList.addAll(keySet);
    
    int hashCode =remoteIp.hashCode();
    int serverListSize = keyList.size();
    int serverPos = hashCode % serverListSize;
    
    return keyList.get(serverPos);
  }
  
  public static void main(String[] args) {
    String serverIp = ipHash("192.168.1.12");
    System.out.println(serverIp);
  }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/lr393993507/p/5702854.html

延伸 · 阅读

精彩推荐