# 一篇有趣的负载均衡算法实现

## 1. 随机访问

``````/** 服务器列表 */
private static List<String> serverList = new ArrayList<>();
static {
}

/**
* 随机路由算法
*/
public static String random() {
// 复制遍历用的集合，防止操作中集合有变更
List<String> tempList = new ArrayList<>(serverList.size());
// 随机数随机访问
int randomInt = new Random().nextInt(tempList.size());
return tempList.get(randomInt);
}``````

``````public static void main(String[] args) {
HashMap<String, Integer> serverMap = new HashMap<>();
for (int i = 0; i < 20000; i++) {
String server = random();
Integer count = serverMap.get(server);
if (count == null) {
count = 1;
} else {
count++;
}
// 记录
serverMap.put(server, count);
}
// 路由总体结果
for (Map.Entry<String, Integer> entry : serverMap.entrySet()) {
System.out.println("IP:" + entry.getKey() + "，次数：" + entry.getValue());
}
}``````

``````IP:192.168.1.3，次数：24979
IP:192.168.1.2，次数：24896
IP:192.168.1.5，次数：25043
IP:192.168.1.4，次数：25082``````

## 2. 轮询访问

``````/** 服务器列表 */
private static List<String> serverList = new ArrayList<>();
static {
}
private static Integer index = 0;

/**
* 随机路由算法
*/
public static String randomOneByOne() {
// 复制遍历用的集合，防止操作中集合有变更
List<String> tempList = new ArrayList<>(serverList.size());
String server = "";
synchronized (index) {
index++;
if (index == tempList.size()) {
index = 0;
}
server = tempList.get(index);;
}
return server;
}``````

``````IP:192.168.1.3，次数：25000
IP:192.168.1.2，次数：25000
IP:192.168.1.5，次数：25000
IP:192.168.1.4，次数：25000``````

## 3. 轮询加权

``````/** 服务器列表 */
private static HashMap<String, Integer> serverMap = new HashMap<>();
static {
serverMap.put("192.168.1.2", 2);
serverMap.put("192.168.1.3", 2);
serverMap.put("192.168.1.4", 2);
serverMap.put("192.168.1.5", 4);
}
private static Integer index = 0;

/**
* 加权路由算法
*/
public static String oneByOneWithWeight() {
List<String> tempList = new ArrayList();
HashMap<String, Integer> tempMap = new HashMap<>();
tempMap.putAll(serverMap);
for (String key : serverMap.keySet()) {
for (int i = 0; i < serverMap.get(key); i++) {
}
}
synchronized (index) {
index++;
if (index == tempList.size()) {
index = 0;
}
return tempList.get(index);
}
}``````

``````IP:192.168.1.3，次数：20000
IP:192.168.1.2，次数：20000
IP:192.168.1.5，次数：40000
IP:192.168.1.4，次数：20000``````

`192.168.1.5` 承担了 2 倍的请求。

## 4. 随机加权

``````/** 服务器列表 */
private static HashMap<String, Integer> serverMap = new HashMap<>();
static {
serverMap.put("192.168.1.2", 2);
serverMap.put("192.168.1.3", 2);
serverMap.put("192.168.1.4", 2);
serverMap.put("192.168.1.5", 4);
}
/**
* 加权路由算法
*/
public static String randomWithWeight() {
List<String> tempList = new ArrayList();
HashMap<String, Integer> tempMap = new HashMap<>();
tempMap.putAll(serverMap);
for (String key : serverMap.keySet()) {
for (int i = 0; i < serverMap.get(key); i++) {
}
}
int randomInt = new Random().nextInt(tempList.size());
return tempList.get(randomInt);
}``````

``````IP:192.168.1.3，次数：19934
IP:192.168.1.2，次数：20033
IP:192.168.1.5，次数：39900
IP:192.168.1.4，次数：20133``````

## 5. IP-Hash

``````private static List<String> serverList = new ArrayList<>();
static {
}

/**
* ip hash 路由算法
*/
public static String ipHash(String ip) {
// 复制遍历用的集合，防止操作中集合有变更
List<String> tempList = new ArrayList<>(serverList.size());
// 哈希计算请求的服务器
int index = ip.hashCode() % serverList.size();
return tempList.get(Math.abs(index));
}``````

## 6. 总结

<完>

Hello world : ) 我是阿朗，一线技术工具人，认认真真写文章。

Hello world :)

352 声望
1.8k 粉丝
0 条评论