IP地址类:
package com.way.LB;
import java.util.HashMap;
public class IPMap {
public static HashMap<String,Integer> serverWeightMap=new HashMap<String,Integer>();
static{
serverWeightMap.put("http://www.baidu.com",1);
serverWeightMap.put("http://www.qq.com", 2);
serverWeightMap.put("http://www.taobao.com", 3);
}
}
哈希法
package com.way.LB.LBAlgorithm;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import com.way.LB.IPMap;
public class Hash {
public static String getServer(){
Map<String,Integer> ipServerMap=new HashMap<String,Integer>();
ipServerMap.putAll(IPMap.serverWeightMap);
Set<String> ipSet=ipServerMap.keySet();
ArrayList<String> ipList=new ArrayList<String>();
ipList.addAll(ipSet);
String clientIP="127.0.0.1";
int hashCode=clientIP.hashCode();
int ipPos=hashCode%ipList.size();
return ipList.get(ipPos);
}
}
随机数法
package com.way.LB.LBAlgorithm;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import com.way.LB.IPMap;
public class Random {
public static String getServer(){
Map<String,Integer> serverIPMap=new HashMap<String,Integer>();
serverIPMap.putAll(IPMap.serverWeightMap);
Set<String> ipSet=serverIPMap.keySet();
ArrayList<String> ipList=new ArrayList<String>();
ipList.addAll(ipSet);
java.util.Random random=new java.util.Random();
int randompos=random.nextInt(ipList.size());
return ipList.get(randompos);
}
}
轮询法:
package com.way.LB.LBAlgorithm;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.way.LB.IPMap;
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<Entry<String, Integer>> entrySet = serverMap.entrySet();
ArrayList<Entry<String, Integer>> keyList = new ArrayList<Entry<String, Integer>>();
keyList.addAll(entrySet);
String server = null;
synchronized (pos)
{
if (pos > entrySet.size())
pos = 0;
server = keyList.get(pos).getKey();
pos ++;
}
return server;
}
}
加权随机法:
package com.way.LB.LBAlgorithm;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.way.LB.IPMap;
public class WeightRandom {
public static String getString(){
Map<String,Integer> ipServerMap=new HashMap<String,Integer>();
ipServerMap.putAll(IPMap.serverWeightMap);
Set<Entry<String,Integer>> ipSet=ipServerMap.entrySet();
ArrayList<String> ipList=new ArrayList<String>();
for(Entry<String,Integer> entry:ipSet){
for(int i=0;i<entry.getValue();i++){
ipList.add(entry.getKey());
}
}
java.util.Random random=new java.util.Random();
int randomPos=random.nextInt(ipList.size());
return ipList.get(randomPos);
}
}
加权轮询法
package com.way.LB.LBAlgorithm;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.way.LB.IPMap;
public class WeightRoundRobin {
private static Integer pos;
public static String getServer(){
Map<String,Integer> ipServerMap=new HashMap<String,Integer>();
ipServerMap.putAll(IPMap.serverWeightMap);
Set<Entry<String,Integer>> ipSet=ipServerMap.entrySet();
ArrayList<String> ipList=new ArrayList<String>();
for(Entry<String,Integer> entry:ipSet){
for(int i=0;i<entry.getValue();i++){
ipList.add(entry.getKey());
}
}
String ipStr=null;
synchronized (pos) {
if(pos>ipList.size()){
pos=0;
}
pos++;
ipStr=ipList.get(pos);
}
return ipStr;
}
}