当前位置: 首页 > news >正文

负载均衡算法实现

负载均衡算法实现

负载均衡介绍

负责均衡主要有以下五种方法实现:
1、轮询法

将请求按顺序轮流地分配到后端服务器上,它均衡地对待后端的每一台服务器,而不关心服务器实际的连接数和当前的系统负载;

2、随机法

通过系统的随机算法,根据后端服务器的列表大小值来随机选取其中的一台服务器进行访问。由概率统计理论可以得知,随着客户端调用服务端的次数增多, 其实际效果越来越接近于平均分配调用量到后端的每一台服务器,也就是轮询的结果;

3、源地址哈希法

源地址哈希的思想是根据获取客户端的IP地址,通过哈希函数计算得到的一个数值,用该数值对服务器列表的大小进行取模运算,得到的结果便是客服端要访问服务器的序号。采用源地址哈希法进行负载均衡,同一IP地址的客户端,当后端服务器列表不变时,它每次都会映射到同一台后端服务器进行访问;

4、加权轮询法

不同的后端服务器可能机器的配置和当前系统的负载并不相同,因此它们的抗压能力也不相同。给配置高、负载低的机器配置更高的权重,让其处理更多的请;而配置低、负载高的机器,给其分配较低的权重,降低其系统负载,加权轮询能很好地处理这一问题,并将请求顺序且按照权重分配到后端;

5、加权随机法

与加权轮询法一样,加权随机法也根据后端机器的配置,系统的负载分配不同的权重。不同的是,它是按照权重随机请求后端服务器,而非顺序;

代码实现

1.轮询方式

/*** 1.负载均衡算法的轮询方式*/
public class poll {static HashMap<String, Integer> ipMap = new HashMap<>();static {ipMap.put("192.168.110.10",1);ipMap.put("192.168.110.11",1);ipMap.put("192.168.110.12",1);ipMap.put("192.168.110.13",1);}Integer pos  = 0;public String RoundRobin(){//1.将map中数据取出放到conCurrentmap中,conCurrentmap能够避免多线程时,数据出错ConcurrentHashMap<String, Integer> cMap = new ConcurrentHashMap<>();cMap.putAll(ipMap);//2.取出Key,放到set中,可以去重Set<String> ipset = cMap.keySet();//3.set放到list中List<String> iplist = new ArrayList<>();iplist.addAll(ipset);String serverName = null;synchronized (pos){if (pos >= iplist.size()){pos = 0;}serverName = iplist.get(pos);pos++;}return serverName;}public static void main(String[] args) {poll poll = new poll();for (int i = 0; i < 10; i++) {String name = poll.RoundRobin();System.out.println(name);}}
}

2.加权轮询法

public class WeightPoll {static HashMap<String, Integer> ipMap = new java.util.HashMap<>();static {ipMap.put("192.168.110.10",1);ipMap.put("192.168.110.11",2);ipMap.put("192.168.110.12",3);ipMap.put("192.168.110.13",4);}Integer pos = 0;public String WeightRobin(){ConcurrentHashMap<String, Integer> ipServerMap = new ConcurrentHashMap<>();ipServerMap.putAll(ipMap);Set<String> ipSet = ipServerMap.keySet();Iterator<String> iterator = ipSet.iterator();//定义一个list放所有serverList<String> iplist = new ArrayList<>();//循环放入iplist中while (iterator.hasNext()){String name = iterator.next();Integer num = ipServerMap.get(name);for (Integer i = 0; i < num; i++) {iplist.add(name);}}String serverName = null;if (pos >= iplist.size()){pos = 0;}serverName = iplist.get(pos);pos++;return serverName;}public static void main(String[] args) {WeightPoll weightPoll = new WeightPoll();for (int i = 0; i < 11; i++) {System.out.println(weightPoll.WeightRobin());}}}

3.随机法

public class RandomAlgrithm {static HashMap<String, Integer> ipMap = new java.util.HashMap<>();static {ipMap.put("192.168.110.10",1);ipMap.put("192.168.110.11",2);ipMap.put("192.168.110.12",3);ipMap.put("192.168.110.13",4);}public String Random(){ConcurrentHashMap<String, Integer> ipConMap = new ConcurrentHashMap<>();ipConMap.putAll(ipMap);Set<String> ipSet = ipConMap.keySet();List<String> ipList = new ArrayList<>();ipList.addAll(ipSet);int num = new Random().nextInt(ipList.size());String serverName = ipList.get(num);return serverName;}public static void main(String[] args) {RandomAlgrithm randomAlgrithm = new RandomAlgrithm();for (int i = 0; i < 10; i++) {System.out.println(randomAlgrithm.Random());}}
}

4.加权随机

public class WeightRandom {static HashMap<String, Integer> ipMap = new java.util.HashMap<>();static {ipMap.put("192.168.110.10",1);ipMap.put("192.168.110.11",2);ipMap.put("192.168.110.12",3);ipMap.put("192.168.110.13",4);}public String WeightRandom(){ConcurrentHashMap<String, Integer> ipConMap = new ConcurrentHashMap<>();ipConMap.putAll(ipMap);Set<String> ipSet = ipConMap.keySet();Iterator<String> iterator = ipSet.iterator();List<String> iplist = new ArrayList<>();while (iterator.hasNext()){String name = iterator.next();Integer num = ipConMap.get(name);for (Integer i = 0; i < num; i++) {iplist.add(name);}}int pos = new Random().nextInt(iplist.size());return iplist.get(pos);}public static void main(String[] args) {WeightRandom weightRandom = new WeightRandom();for (int i = 0; i < 10; i++) {System.out.println(weightRandom.WeightRandom());}}
}

5.源地址哈希法

public class AddressHash {static HashMap<String, Integer> ipMap = new java.util.HashMap<>();static {ipMap.put("192.168.110.10",1);ipMap.put("192.168.110.11",2);ipMap.put("192.168.110.12",3);ipMap.put("192.168.110.13",4);}public String ipHash(String clientIp){ConcurrentHashMap<String, Integer> ipConMap = new ConcurrentHashMap<>();ipConMap.putAll(ipMap);Set<String> ipSet = ipConMap.keySet();List<String> iplist = new ArrayList<>();iplist.addAll(ipSet);int hashCode = clientIp.hashCode();int num = hashCode % iplist.size();return iplist.get(num);}public static void main(String[] args) {AddressHash addressHash = new AddressHash();for (int i = 0; i < 1; i++) {System.out.println(addressHash.ipHash("192.168.110.57"));System.out.println(addressHash.ipHash("192.168.110.47"));System.out.println(addressHash.ipHash("192.168.110.23"));System.out.println(addressHash.ipHash("192.168.110.59"));}}
}
http://www.lryc.cn/news/154535.html

相关文章:

  • Flutter 完美的验证码输入框 转载
  • SpringBoot整合Jpa实现增删改查功能(提供Gitee源码)
  • 微服务[Nacos]
  • 8K视频来了,8K 视频编辑的最低系统要求
  • AsyncContext优雅实现HTTP长轮询接口
  • 如何制作一个百货小程序
  • 【人工智能】—局部搜索算法、爬山法、模拟退火、局部剪枝、遗传算法
  • MATLAB旋转动图的绘制
  • 算法笔记 近似最近邻查找(Approximate Nearest Neighbor Search,ANN)
  • uni-app 之 vue语法
  • Android之RecyclerView仿ViewPage滑动
  • 【owt-server】AudioSendAdapter分析
  • day33 List接口
  • 云原生周刊:Linkerd 发布 v2.14 | 2023.9.4
  • CS420 课程笔记 P5 - 内存编辑 数据类型
  • oracle报错 ORA-02290: 违反检查约束条件问题
  • Prometheus + grafana 的监控平台部署
  • npm、yarn、pnpm
  • 力扣|两数相加
  • prometheus通过blackbox-exporter监控web站点证书
  • CentOS7 Hadoop3.3.0 安装与配置
  • 2023年9月CDGA/CDGP数据治理认证考试报名,当然弘博创新
  • Re45:读论文 GPT-1 Improving Language Understanding by Generative Pre-Training
  • VB.NET 如何将某个Excel的工作表中复制到另一个的Excel中的工作表中https://bbs.csdn.net/topics/392861034
  • 深入解析Kotlin类与对象:构造、伴生、单例全面剖析
  • JavaScript构造函数
  • 手写嵌入式操作系统(基于stm8单片机)
  • vue3.3 ~
  • 滑动窗口实例4(将x减到0的最小操作数)
  • 数据库原理及应用(MySQL)