??????????????
???????????????????Load Balance??????????????????????????????????????????????????е????λ???????????????????????????????????????????????????????????????????????????????????????е???????????????????????????????????????????????????????????????????????????????У?????????????????????????????????????????????????????????????????y??????????????????
???????????????????????????????????????????????????????з???LVS??????????????????????F5??????????????£????????
???????????????”???????????????????????????е????????????“?????????????Java?????????????????????OK???????????????????????????д??????????Ip?б??
????public class IpMap
????{
????// ??·???Ip?б??Key????Ip??Value?????Ip?????
????public static HashMap<String?? Integer> serverWeightMap =
????new HashMap<String?? Integer>();
????static
????{
????serverWeightMap.put("192.168.1.100"?? 1);
????serverWeightMap.put("192.168.1.101"?? 1);
????// ????4
????serverWeightMap.put("192.168.1.102"?? 4);
????serverWeightMap.put("192.168.1.103"?? 1);
????serverWeightMap.put("192.168.1.104"?? 1);
????// ????3
????serverWeightMap.put("192.168.1.105"?? 3);
????serverWeightMap.put("192.168.1.106"?? 1);
????// ????2
????serverWeightMap.put("192.168.1.107"?? 2);
????serverWeightMap.put("192.168.1.108"?? 1);
????serverWeightMap.put("192.168.1.109"?? 1);
????serverWeightMap.put("192.168.1.110"?? 1);
????}
????}
?????????Round Robin????
???????????Round Robin??????????????????£?
????public class RoundRobin
????{
????private static Integer pos = 0;
????public static String getServer()
????{
????// ??????Map?????????????????????μ????????
????Map<String?? Integer> serverMap =
????new HashMap<String?? Integer>();
????serverMap.putAll(IpMap.serverWeightMap);
????// ???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;
????}
????}
????????serverWeightMap?е????б????????????????л???????????????崻????????????????????????????????????????????serverMap?????serverMap?е?????????????????????????????????????????????μ????????????serverWeightMap?????????????serverMap?????????????????????????У?????????????????????????????????????????????????????ν??????з????????????崻??????????????????????????????????????????????????????????????·??????server??????á?
???????????????λ?????pos??????????????????????????????????????????????????????????????????pos?????????pos???????????????????????????????????????????п??????keyList??????硣
?????????????????????????????????????
????????????????????????????????????????????????????????????pos??????????????????????????????????synchronized?????????????????????????????????????????
?????????Random????
??????????????????????????????б???С??????????????????з???????????????????????????????????????????Ч?????????????????????????????????????????????Ч????
???????????????????????£?
????public class Random
????{
????public static String getServer()
????{
????// ??????Map?????????????????????μ????????
????Map<String?? Integer> serverMap =
????new HashMap<String?? Integer>();
????serverMap.putAll(IpMap.serverWeightMap);
????// ???Ip???List
????Set<String> keySet = serverMap.keySet();
????ArrayList<String> keyList = new ArrayList<String>();
????keyList.addAll(keySet);
????java.util.Random random = new java.util.Random();
????int randomPos = random.nextInt(keyList.size());
????return keyList.get(randomPos);
????}
????}
????????????·?????????£??????serverMap????????server?б??????server????????Random??nextInt?????0~keyList.size()?????????????????????????б?????????????????????????з????????????????????????????????????Ч???????????????Ч????
?????????????Hash????
?????????????????????????????IP????????????????????????????????????????????б???С?????????????????????????????????????????????????????????????£?
????public class Hash
????{
????public static String getServer()
????{
????// ??????Map?????????????????????μ????????
????Map<String?? Integer> serverMap =
????new HashMap<String?? Integer>();
????serverMap.putAll(IpMap.serverWeightMap);
????// ???Ip???List
????Set<String> keySet = serverMap.keySet();
????ArrayList<String> keyList = new ArrayList<String>();
????keyList.addAll(keySet);
????// ??Web????п????HttpServlet??getRemoteIp???????
????String remoteIp = "127.0.0.1";
????int hashCode = remoteIp.hashCode();
????int serverListSize = keyList.size();
????int serverPos = hashCode % serverListSize;
????return keyList.get(serverPos);
????}
????}
????????????????????????????????????????·?????????????????ip???remoteIp?????????Hash???????????б???С????????????????????????????б??е????????