详解Java实现负载均衡的几种算法代码
本篇文章主要介绍Java实现负载均衡的几种算法,具体如下:
轮询:
packageclass2.zookeeper.loadbalance;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.Map;
importjava.util.Set;
/**
*負載均衡算法,輪詢法
*@authorguoy
*
*/
publicclassTestRoundRobin{
staticMap<String,Integer>serverWeigthMap=newHashMap<String,Integer>();
static{
serverWeigthMap.put("192.168.1.12",1);
serverWeigthMap.put("192.168.1.13",1);
serverWeigthMap.put("192.168.1.14",2);
serverWeigthMap.put("192.168.1.15",2);
serverWeigthMap.put("192.168.1.16",3);
serverWeigthMap.put("192.168.1.17",3);
serverWeigthMap.put("192.168.1.18",1);
serverWeigthMap.put("192.168.1.19",2);
}
Integerpos=0;
publicStringroundRobin()
{
//重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
Map<String,Integer>serverMap=newHashMap<String,Integer>();
serverMap.putAll(serverWeigthMap);
//獲取ip列表list
Set<String>keySet=serverMap.keySet();
ArrayList<String>keyList=newArrayList<String>();
keyList.addAll(keySet);
Stringserver=null;
synchronized(pos){
if(pos>=keySet.size()){
pos=0;
}
server=keyList.get(pos);
pos++;
}
returnserver;
}
publicstaticvoidmain(String[]args){
TestRoundRobinrobin=newTestRoundRobin();
for(inti=0;i<20;i++){
StringserverIp=robin.roundRobin();
System.out.println(serverIp);
}
}
}
加权轮询:
packageclass2.zookeeper.loadbalance;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.List;
importjava.util.Map;
importjava.util.Random;
importjava.util.Set;
/**
*加權隨機载均衡算法
*@authorguoy
*
*/
publicclassTestWeightRandom{
staticMap<String,Integer>serverWeigthMap=newHashMap<String,Integer>();
static{
serverWeigthMap.put("192.168.1.12",1);
serverWeigthMap.put("192.168.1.13",1);
serverWeigthMap.put("192.168.1.14",2);
serverWeigthMap.put("192.168.1.15",2);
serverWeigthMap.put("192.168.1.16",3);
serverWeigthMap.put("192.168.1.17",3);
serverWeigthMap.put("192.168.1.18",1);
serverWeigthMap.put("192.168.1.19",2);
}
publicstaticStringweightRandom()
{
//重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
Map<String,Integer>serverMap=newHashMap<String,Integer>();
serverMap.putAll(serverWeigthMap);
//獲取ip列表list
Set<String>keySet=serverMap.keySet();
Iterator<String>it=keySet.iterator();
List<String>serverList=newArrayList<String>();
while(it.hasNext()){
Stringserver=it.next();
Integerweight=serverMap.get(server);
for(inti=0;i<weight;i++){
serverList.add(server);
}
}
Randomrandom=newRandom();
intrandomPos=random.nextInt(serverList.size());
Stringserver=serverList.get(randomPos);
returnserver;
}
publicstaticvoidmain(String[]args){
StringserverIp=weightRandom();
System.out.println(serverIp);
}
}
随机:
packageclass2.zookeeper.loadbalance;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.Map;
importjava.util.Random;
importjava.util.Set;
/**
*隨機负载均衡算法
*@authorguoy
*
*/
publicclassTestRandom{
staticMap<String,Integer>serverWeigthMap=newHashMap<String,Integer>();
static{
serverWeigthMap.put("192.168.1.12",1);
serverWeigthMap.put("192.168.1.13",1);
serverWeigthMap.put("192.168.1.14",2);
serverWeigthMap.put("192.168.1.15",2);
serverWeigthMap.put("192.168.1.16",3);
serverWeigthMap.put("192.168.1.17",3);
serverWeigthMap.put("192.168.1.18",1);
serverWeigthMap.put("192.168.1.19",2);
}
publicstaticStringrandom()
{
//重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
Map<String,Integer>serverMap=newHashMap<String,Integer>();
serverMap.putAll(serverWeigthMap);
//獲取ip列表list
Set<String>keySet=serverMap.keySet();
ArrayList<String>keyList=newArrayList<String>();
keyList.addAll(keySet);
Randomrandom=newRandom();
intrandomPos=random.nextInt(keyList.size());
Stringserver=keyList.get(randomPos);
returnserver;
}
publicstaticvoidmain(String[]args){
StringserverIp=random();
System.out.println(serverIp);
}
}
加权随机:
packageclass2.zookeeper.loadbalance;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.List;
importjava.util.Map;
importjava.util.Random;
importjava.util.Set;
/**
*加權隨機载均衡算法
*@authorguoy
*
*/
publicclassTestWeightRandom{
staticMap<String,Integer>serverWeigthMap=newHashMap<String,Integer>();
static{
serverWeigthMap.put("192.168.1.12",1);
serverWeigthMap.put("192.168.1.13",1);
serverWeigthMap.put("192.168.1.14",2);
serverWeigthMap.put("192.168.1.15",2);
serverWeigthMap.put("192.168.1.16",3);
serverWeigthMap.put("192.168.1.17",3);
serverWeigthMap.put("192.168.1.18",1);
serverWeigthMap.put("192.168.1.19",2);
}
publicstaticStringweightRandom()
{
//重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
Map<String,Integer>serverMap=newHashMap<String,Integer>();
serverMap.putAll(serverWeigthMap);
//獲取ip列表list
Set<String>keySet=serverMap.keySet();
Iterator<String>it=keySet.iterator();
List<String>serverList=newArrayList<String>();
while(it.hasNext()){
Stringserver=it.next();
Integerweight=serverMap.get(server);
for(inti=0;i<weight;i++){
serverList.add(server);
}
}
Randomrandom=newRandom();
intrandomPos=random.nextInt(serverList.size());
Stringserver=serverList.get(randomPos);
returnserver;
}
publicstaticvoidmain(String[]args){
StringserverIp=weightRandom();
System.out.println(serverIp);
}
}
iphash:
packageclass2.zookeeper.loadbalance;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.Map;
importjava.util.Set;
/**
*负载均衡ip_hash算法
*@authorguoy
*
*/
publicclassTestIpHash{
staticMap<String,Integer>serverWeigthMap=newHashMap<String,Integer>();
static{
serverWeigthMap.put("192.168.1.12",1);
serverWeigthMap.put("192.168.1.13",1);
serverWeigthMap.put("192.168.1.14",2);
serverWeigthMap.put("192.168.1.15",2);
serverWeigthMap.put("192.168.1.16",3);
serverWeigthMap.put("192.168.1.17",3);
serverWeigthMap.put("192.168.1.18",1);
serverWeigthMap.put("192.168.1.19",2);
}
/**
*获取请求服务器地址
*@paramremoteIp负载均衡服务器ip
*@return
*/
publicstaticStringipHash(StringremoteIp)
{
//重新建立一個map,避免出現由於服務器上線和下線導致的並發問題
Map<String,Integer>serverMap=newHashMap<String,Integer>();
serverMap.putAll(serverWeigthMap);
//獲取ip列表list
Set<String>keySet=serverMap.keySet();
ArrayList<String>keyList=newArrayList<String>();
keyList.addAll(keySet);
inthashCode=remoteIp.hashCode();
intserverListSize=keyList.size();
intserverPos=hashCode%serverListSize;
returnkeyList.get(serverPos);
}
publicstaticvoidmain(String[]args){
StringserverIp=ipHash("192.168.1.12");
System.out.println(serverIp);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。