负载均衡的几种算法Java实现代码
轮询:
1 package class2.zookeeper.loadbalance;2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Map;
6 import java.util.Set;
7
8 /**
9 * 負載均衡算法,輪詢法
10 * @author guoy
11 *
12 */
13 public class TestRoundRobin {
14
15
16 static Map<String,Integer> serverWeigthMap = new HashMap<String,Integer>();
17
18 static{
19 serverWeigthMap.put("192.168.1.12", 1);
20 serverWeigthMap.put("192.168.1.13", 1);
21 serverWeigthMap.put("192.168.1.14", 2);
22 serverWeigthMap.put("192.168.1.15", 2);
23 serverWeigthMap.put("192.168.1.16", 3);
24 serverWeigthMap.put("192.168.1.17", 3);
25 serverWeigthMap.put("192.168.1.18", 1);
26 serverWeigthMap.put("192.168.1.19", 2);
27 }
28 Integer pos = 0;
29 public String roundRobin()
30 {
31 //重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
32 Map<String,Integer> serverMap = new HashMap<String,Integer>();
33 serverMap.putAll(serverWeigthMap);
34 //獲取ip列表list
35 Set<String> keySet = serverMap.keySet();
36 ArrayList<String> keyList = new ArrayList<String>();
37 keyList.addAll(keySet);
38
39 String server = null;
40
41 synchronized (pos) {
42 if(pos >=keySet.size()){
43 pos = 0;
44 }
45 server = keyList.get(pos);
46 pos ++;
47 }
48 return server;
49 }
50
51 public static void main(String[] args) {
52 TestRoundRobin robin = new TestRoundRobin();
53 for (int i = 0; i < 20; i++) {
54 String serverIp = robin.roundRobin();
55 System.out.println(serverIp);
56 }
57 }
58 }
加权轮询:
1 package class2.zookeeper.loadbalance;2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Random;
9 import java.util.Set;
10
11 /**
12 * 加權隨機载均衡算法
13 * @author guoy
14 *
15 */
16 public class TestWeightRandom {
17
18 static Map<String,Integer> serverWeigthMap = new HashMap<String,Integer>();
19
20 static{
21 serverWeigthMap.put("192.168.1.12", 1);
22 serverWeigthMap.put("192.168.1.13", 1);
23 serverWeigthMap.put("192.168.1.14", 2);
24 serverWeigthMap.put("192.168.1.15", 2);
25 serverWeigthMap.put("192.168.1.16", 3);
26 serverWeigthMap.put("192.168.1.17", 3);
27 serverWeigthMap.put("192.168.1.18", 1);
28 serverWeigthMap.put("192.168.1.19", 2);
29 }
30
31 public static String weightRandom()
32 {
33 //重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
34 Map<String,Integer> serverMap = new HashMap<String,Integer>();
35 serverMap.putAll(serverWeigthMap);
36 //獲取ip列表list
37 Set<String> keySet = serverMap.keySet();
38 Iterator<String> it = keySet.iterator();
39
40 List<String> serverList = new ArrayList<String>();
41
42 while (it.hasNext()) {
43 String server = it.next();
44 Integer weight = serverMap.get(server);
45 for (int i = 0; i < weight; i++) {
46 serverList.add(server);
47 }
48 }
49 Random random = new Random();
50 int randomPos = random.nextInt(serverList.size());
51
52 String server = serverList.get(randomPos);
53 return server;
54 }
55
56 public static void main(String[] args) {
57 String serverIp = weightRandom();
58 System.out.println(serverIp);
59 }
60 }
随机:
1 package class2.zookeeper.loadbalance;2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Map;
6 import java.util.Random;
7 import java.util.Set;
8
9 /**
10 * 隨機负载均衡算法
11 * @author guoy
12 *
13 */
14 public class TestRandom {
15
16 static Map<String,Integer> serverWeigthMap = new HashMap<String,Integer>();
17
18 static{
19 serverWeigthMap.put("192.168.1.12", 1);
20 serverWeigthMap.put("192.168.1.13", 1);
21 serverWeigthMap.put("192.168.1.14", 2);
22 serverWeigthMap.put("192.168.1.15", 2);
23 serverWeigthMap.put("192.168.1.16", 3);
24 serverWeigthMap.put("192.168.1.17", 3);
25 serverWeigthMap.put("192.168.1.18", 1);
26 serverWeigthMap.put("192.168.1.19", 2);
27 }
28
29 public static String random()
30 {
31 //重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
32 Map<String,Integer> serverMap = new HashMap<String,Integer>();
33 serverMap.putAll(serverWeigthMap);
34 //獲取ip列表list
35 Set<String> keySet = serverMap.keySet();
36 ArrayList<String> keyList = new ArrayList<String>();
37 keyList.addAll(keySet);
38
39 Random random = new Random();
40 int randomPos = random.nextInt(keyList.size());
41
42 String server = keyList.get(randomPos);
43 return server;
44 }
45
46 public static void main(String[] args) {
47 String serverIp = random();
48 System.out.println(serverIp);
49 }
50 }
加权随机:
1 package class2.zookeeper.loadbalance;2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Random;
9 import java.util.Set;
10
11 /**
12 * 加權隨機载均衡算法
13 * @author guoy
14 *
15 */
16 public class TestWeightRandom {
17
18 static Map<String,Integer> serverWeigthMap = new HashMap<String,Integer>();
19
20 static{
21 serverWeigthMap.put("192.168.1.12", 1);
22 serverWeigthMap.put("192.168.1.13", 1);
23 serverWeigthMap.put("192.168.1.14", 2);
24 serverWeigthMap.put("192.168.1.15", 2);
25 serverWeigthMap.put("192.168.1.16", 3);
26 serverWeigthMap.put("192.168.1.17", 3);
27 serverWeigthMap.put("192.168.1.18", 1);
28 serverWeigthMap.put("192.168.1.19", 2);
29 }
30
31 public static String weightRandom()
32 {
33 //重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
34 Map<String,Integer> serverMap = new HashMap<String,Integer>();
35 serverMap.putAll(serverWeigthMap);
36 //獲取ip列表list
37 Set<String> keySet = serverMap.keySet();
38 Iterator<String> it = keySet.iterator();
39
40 List<String> serverList = new ArrayList<String>();
41
42 while (it.hasNext()) {
43 String server = it.next();
44 Integer weight = serverMap.get(server);
45 for (int i = 0; i < weight; i++) {
46 serverList.add(server);
47 }
48 }
49 Random random = new Random();
50 int randomPos = random.nextInt(serverList.size());
51
52 String server = serverList.get(randomPos);
53 return server;
54 }
55
56 public static void main(String[] args) {
57 String serverIp = weightRandom();
58 System.out.println(serverIp);
59 }
60 }
ip hash:
1 package class2.zookeeper.loadbalance;2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Map;
6 import java.util.Set;
7
8 /**
9 * 负载均衡 ip_hash算法
10 * @author guoy
11 *
12 */
13 public class TestIpHash {
14
15
16 static Map<String,Integer> serverWeigthMap = new HashMap<String,Integer>();
17
18 static{
19 serverWeigthMap.put("192.168.1.12", 1);
20 serverWeigthMap.put("192.168.1.13", 1);
21 serverWeigthMap.put("192.168.1.14", 2);
22 serverWeigthMap.put("192.168.1.15", 2);
23 serverWeigthMap.put("192.168.1.16", 3);
24 serverWeigthMap.put("192.168.1.17", 3);
25 serverWeigthMap.put("192.168.1.18", 1);
26 serverWeigthMap.put("192.168.1.19", 2);
27 }
28
29 /**
30 * 获取请求服务器地址
31 * @param remoteIp 负载均衡服务器ip
32 * @return
33 */
34 public static String ipHash(String remoteIp)
35 {
36 //重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
37 Map<String,Integer> serverMap = new HashMap<String,Integer>();
38 serverMap.putAll(serverWeigthMap);
39 //獲取ip列表list
40 Set<String> keySet = serverMap.keySet();
41 ArrayList<String> keyList = new ArrayList<String>();
42 keyList.addAll(keySet);
43
44 int hashCode =remoteIp.hashCode();
45 int serverListSize = keyList.size();
46 int serverPos = hashCode % serverListSize;
47
48 return keyList.get(serverPos);
49 }
50
51 public static void main(String[] args) {
52 String serverIp = ipHash("192.168.1.12");
53 System.out.println(serverIp);
54 }
55
56 }
以上是 负载均衡的几种算法Java实现代码 的全部内容, 来源链接: utcz.com/z/395158.html