负载:就是后端系统的承载能力。比如同等条件下,一个1核cpu-1G内存的机器的承载能力一般会比8核cpu-8G内存的机器要差;相同配置下,一个cpu利用率为80%的机器比30%的承载能力一般要差等等。
均衡:保证后端请求的平衡。比如:在同等情况下,分配到多台机器的请求要相当;有些情况下,同一用户尽可能分配到同一台机器等等。
负载均衡的算法实际上就是解决跨系统调用的时候,在考虑后端机器承载情况的前提下,保证请求分配的平衡和合理。下面是基于java的负载均衡算法解析及源码,以供参考。
1、轮询:
package class2.zookeeper.loadbalance;
import java.uTIl.ArrayList;
import java.uTIl.HashMap;
import java.uTIl.Map;
import java.uTIl.Set;
/**
* 负载均衡算法,轮询法
* @author guoy
*
*/
public class TestRoundRobin {
static Map《String,Integer》 serverWeigthMap = new HashMap《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);
}
Integer pos = 0;
public String roundRobin()
{
//重新建立一个map,避免出现由于服务器上线和下线导致的并发问题
Map《String,Integer》 serverMap = new HashMap《String,Integer》();
serverMap.putAll(serverWeigthMap);
//获取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;
}
public static void main(String[] args) {
TestRoundRobin robin = new TestRoundRobin();
for (int i = 0; i 《 20; i++) {
String serverIp = robin.roundRobin();
System.out.println(serverIp);
}
}
}
2、加权轮询:
package class2.zookeeper.loadbalance;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
/**
* 加权随机载均衡算法
* @author guoy
*
*/
public class TestWeightRandom {
static Map《String,Integer》 serverWeigthMap = new HashMap《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);
}
public static String weightRandom()
{
//重新建立一个map,避免出现由于服务器上线和下线导致的并发问题
Map《String,Integer》 serverMap = new HashMap《String,Integer》();
serverMap.putAll(serverWeigthMap);
//获取ip列表list
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;
}
public static void main(String[] args) {
String serverIp = weightRandom();
System.out.println(serverIp);
}
}
3、随机:
package class2.zookeeper.loadbalance;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;
/**
* 随机负载均衡算法
* @author guoy
*
*/
public class TestRandom {
static Map《String,Integer》 serverWeigthMap = new HashMap《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);
}
public static String random()
{
//重新建立一个map,避免出现由于服务器上线和下线导致的并发问题
Map《String,Integer》 serverMap = new HashMap《String,Integer》();
serverMap.putAll(serverWeigthMap);
//获取ip列表list
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);
return server;
}
public static void main(String[] args) {
String serverIp = random();
System.out.println(serverIp);
}
}
4、加权随机:
package class2.zookeeper.loadbalance;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
/**
* 加权随机载均衡算法
* @author guoy
*
*/
public class TestWeightRandom {
static Map《String,Integer》 serverWeigthMap = new HashMap《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);
}
public static String weightRandom()
{
//重新建立一个map,避免出现由于服务器上线和下线导致的并发问题
Map《String,Integer》 serverMap = new HashMap《String,Integer》();
serverMap.putAll(serverWeigthMap);
//获取ip列表list
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;
}
public static void main(String[] args) {
String serverIp = weightRandom();
System.out.println(serverIp);
}
}
5、ip hash:
package class2.zookeeper.loadbalance;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* 负载均衡 ip_hash算法
* @author guoy
*
*/
public class TestIpHash {
static Map《String,Integer》 serverWeigthMap = new HashMap《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);
}
/**
* 获取请求服务器地址
* @param remoteIp 负载均衡服务器ip
* @return
*/
public static String ipHash(String remoteIp)
{
//重新建立一个map,避免出现由于服务器上线和下线导致的并发问题
Map《String,Integer》 serverMap = new HashMap《String,Integer》();
serverMap.putAll(serverWeigthMap);
//获取ip列表list
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;
return keyList.get(serverPos);
}
public static void main(String[] args) {
String serverIp = ipHash(“192.168.1.12”);
System.out.println(serverIp);
}
}
技术专区
- Alexa语音服务软件扩展STM32Cube
- 结合DNN API驱动未来神经网络应用的解决方案
- 针对于高端移动通信和汽车市场的四集群设计
- 一款基于帧捕捉的开源图形调试器应用设计
- 即将成为标准配置的最新安全技术:后视摄像系统