负载均衡算法实现代码

        /**
 * Created by 赵超 on 2017/1/17
 * 服务器的ip地址
 */
public class IpAddrUtil {

    /**
     * 用于存储各个服务器的ip,key代表ip,value代表该ip的权重
     */
    public static HashMap serverIpAddrMap = new HashMap<>();

    static {
        serverIpAddrMap.put("192.168.3.100", 1);
        serverIpAddrMap.put("192.168.3.101", 1);
        serverIpAddrMap.put("192.168.3.102", 4);
        serverIpAddrMap.put("192.168.3.103", 1);
        serverIpAddrMap.put("192.168.3.104", 2);
        serverIpAddrMap.put("192.168.3.105", 1);
        serverIpAddrMap.put("192.168.3.105", 3);
    }

}

/**
* Created by 赵超 on 2017/1/17
* 负载均衡实现方法
* 轮询法
* 随机法
* 源地址hash法
* 加权轮询法
* 加权随机法
* 最小连接数法(并未实现)
*/
public class LoadBalanceUtil {

private static Integer pos = 0;

/**
* 轮询法
* @return
*/
public static String roundRobin() {
//重建一个Map,避免服务器的上下线导致的并发问题
Map serverMap = new HashMap<>();
serverMap.putAll(IpAddrUtil.serverIpAddrMap);
//获取地址的list
Set keySet = serverMap.keySet();
ArrayList keyList = new ArrayList<>();
keyList.addAll(keySet);

String server;

synchronized (pos) {
if (pos > keyList.size()) {
pos = 0;
}
server = keyList.get(pos);
pos ++;
}

return server;
}

/**
* 随机法
* @return
*/
public static String random() {
//重建一个Map,避免服务器的上下线导致的并发问题
Map serverMap = new HashMap<>();
serverMap.putAll(IpAddrUtil.serverIpAddrMap);
//获取地址的list
Set keySet = serverMap.keySet();
ArrayList keyList = new ArrayList<>();
keyList.addAll(keySet);
Random random = new Random();
int serverPos = random.nextInt(keyList.size());
return keyList.get(serverPos);
}

/**
* 源地址hash法
* @return
*/
public static String hash() {
//重建一个Map,避免服务器的上下线导致的并发问题
Map serverMap = new HashMap<>();
serverMap.putAll(IpAddrUtil.serverIpAddrMap);
Set keySet = serverMap.keySet();
ArrayList keyList = new ArrayList<>();
keyList.addAll(keySet);
//在web应用中通过HttpServlet的getRemoteIp方法获取
String remoteIp = "127.0.0.1"; //这里不是定值
int hashCode = remoteIp.hashCode();
int size = keyList.size();
int serverPos = hashCode % size;
return keyList.get(serverPos);
}

/**
* 加权轮询法
* @return
*/
public static String weightRoundRobin() {
Map serverMap = new HashMap<>();
serverMap.putAll(IpAddrUtil.serverIpAddrMap);
Set keySet = serverMap.keySet();
Iterator iterator = keySet.iterator();
List serverList = new ArrayList<>();
while (iterator.hasNext()) {
String server = iterator.next();
int weight = serverMap.get(server);
//权重越高 添加次数越多 使用的频率会越高(次数越多)
for (int i = 0; i < weight; i++) {
serverList.add(server);
}
}
String server;
synchronized (pos) {
if (pos > serverList.size()) {
// if (pos > keySet.size()) {
pos = 0;
}
server = serverList.get(pos);
pos ++;
}
return server;
}

/**
* 加权随机法
* @return
*/
public static String weightRandom() {
Map serverMap = new HashMap<>();
serverMap.putAll(IpAddrUtil.serverIpAddrMap);
Set keySet = serverMap.keySet();
Iterator iterator = keySet.iterator();
List serverList = new ArrayList<>();
while (iterator.hasNext()) {
String server = iterator.next();
int weight = serverMap.get(server);
for (int i = 0; i < weight; i ++) {
serverList.add(server);
}
}
Random random = new Random();
int serverPos = random.nextInt(serverList.size());
return serverList.get(serverPos);
}

/**
* 最小连接数法(并未实现)
*/
public static String leastConnections() {
return null;
}

}

你可能感兴趣的:(负载均衡算法实现代码)