負荷等化アルゴリズム実装コード

4607 ワード

        /**
 * 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;
}

}