1、轮询法
将请求按顺序轮流地分配到服务器上,他均衡的对待后端每一台服务器,而不关心服务器实际的连接数和当前的系统负载
public class TeskToundRobin {
private static Map<String, Integer> serverWeightMap = null;
private static Integer pos = 0;
public void initServerWeightMap(){
serverWeightMap = new HashMap<String,Integer>();
serverWeightMap.put("192.168.1.100", 1);
serverWeightMap.put("192.168.1.101", 1);
serverWeightMap.put("192.168.1.102", 4);
serverWeightMap.put("192.168.1.103", 1);
serverWeightMap.put("192.168.1.104", 1);
serverWeightMap.put("192.168.1.105", 3);
serverWeightMap.put("192.168.1.106", 1);
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);
}
public static String testRoundRobin(){
Map<String,Integer> serverMap = new HashMap<String,Integer>();
serverMap.putAll(serverWeightMap);
Set<String> keySet = serverMap.keySet();
ArrayList<String> keyList = new ArrayList<String>();
keyList.addAll(keySet);
String server = null;
synchronized(pos){
System.out.println("pos = "+pos);
if(pos >= keySet.size()){
pos = 0;
}
server = keyList.get(pos);
System.out.println("server = "+server);
pos++;
}
return server;
}
public static void main(String[] args) {
for(int i= 0;i<15; i++){
TeskToundRobin test = new TeskToundRobin();
test.initServerWeightMap();
test.testRoundRobin();
}
}
}
2、随机算法
public static String randomAlgorithm(){
Map<String,Integer> serverMap = new HashMap<String,Integer>();
serverMap.putAll(serverWeightMap);
Set<String> keySet = serverMap.keySet();
ArrayList<String> keyList = new ArrayList<String>();
keyList.addAll(keySet);
Random random = new Random();
int randomPos = random.nextInt(keyList.size());
String server = keyList.get(randomPos);
System.out.println(server);
return server;
}
3、哈希算法
public static String testConsumerHash(String remoteip){
Map<String,Integer> serverMap = new HashMap<String,Integer>();
serverMap.putAll(serverWeightMap);
Set<String> keySet = serverMap.keySet();
ArrayList<String> keyList = new ArrayList<String>();
keyList.addAll(keySet);
int hashCode = remoteip.hashCode();
int serverListSize = keyList.size();
int serverPos = hashCode % serverListSize;
String server = keyList.get(serverPos);
System.out.println(serverPos+"==="+server);
return server;
}
4、加权轮询法
public static String testWeightRoundRobin(){
Map<String,Integer> serverMap = new HashMap<String,Integer>();
serverMap.putAll(serverWeightMap);
Set<String> keySet = serverMap.keySet();
Iterator<String> it = keySet.iterator();
List<String> serverList = new ArrayList<String>();
while(it.hasNext()){
String server = it.next();
Integer weight = serverMap.get(server);
for(int i=0; i<weight; i++){
serverList.add(server);
}
}
String server = null;
synchronized(pos){
if(pos >= serverList.size()){
pos = 0;
}
server = serverList.get(pos);
pos ++;
}
return server;
}
5、加权随机法
public static String testWeightRandom(){
Map<String,Integer> serverMap = new HashMap<String,Integer>();
serverMap.putAll(serverWeightMap);
Set<String> keySet = serverMap.keySet();
Iterator<String> it = keySet.iterator();
List<String> serverList = new ArrayList<String>();
while(it.hasNext()){
String server = it.next();
Integer weight = serverMap.get(server);
for(int i=0; i<weight; i++){
serverList.add(server);
}
}
Random random = new Random();
int randomPos = random.nextInt(serverList.size());
String server = serverList.get(randomPos);
return server;
}
6、最小连接数法
根据后端服务器当前连接数情况,取连接数最小的服务器地址,尽可能得提高后端服务器的利用效率,奖负载合理地分流到每一台机器。由于最小连接数涉及服务器连接数的汇总和感知。所以设计和实现比较繁琐。