郑州网站建设贝斯特,唐山网站建设设计,网站开发制作案例,wordpress屏蔽外国ip前言
本篇实现一下分布式中负载均衡的实现策略#xff0c;以及负载均衡算法是如何实现的。
什么是负载均衡#xff1f;
Java负载均衡是指在多台服务器之间分配负载#xff0c;以提高服务器的性能和可用性。它通过将请求分发到多台服务器来减少单个服务器的压力#xff0…前言
本篇实现一下分布式中负载均衡的实现策略以及负载均衡算法是如何实现的。
什么是负载均衡
Java负载均衡是指在多台服务器之间分配负载以提高服务器的性能和可用性。它通过将请求分发到多台服务器来减少单个服务器的压力从而提高系统的性能和可用性。
算法实现 举个例子在hcr-user服务中存在三条集群实例。 每次请求都要请求不同的服务那么就需要有一个全局的计数器来进行计数。 AtomicInteger是concurrent包下的一个原子类。 public class MyTest {private AtomicInteger count new AtomicInteger(0);Testpublic void test() {ListString list new LinkedList();list.add(服务A);list.add(服务B);list.add(服务C);//让原子计数器1并 % 当前服务集合长度 当前请求执行集群的实例//返回实例下标int index count.incrementAndGet() % list.size();//根据集合下标找到当前集群的实例String service list.get(index);System.err.println(service);}
}代码实现
1轮询
Component
public class RoundLoadBalance implements LoadBalance {Resourceprivate DiscoveryClient client;//创建一个原子类的计数器private AtomicInteger atomicCount new AtomicInteger(0);/*** 使用轮询的方式完成负载均衡算法实现** param serviceId 服务id* return {link ServiceInstance}*/public ServiceInstance getInstances(String serviceId) {//获取到服务的集合集群ListServiceInstance list client.getInstances(serviceId);if (list ! null list.size() 0) {//将当前原子计数器 1incrementAndGet方法相当于i% 服务集合的长度 本次调用服务int index atomicCount.incrementAndGet() % list.size();return list.get(index);}return null;}
}2随机
Component
public class RandomLoadBalance implements LoadBalance {Resourceprivate DiscoveryClient client;/*** 实现随机算法完成负载均衡** param serviceId 服务id* return {link ServiceInstance}*/public ServiceInstance getInstances(String serviceId) {ListServiceInstance list client.getInstances(serviceId);if (list ! null list.size() 0) {Random random new Random();//随机数值范围0-集合长度例如集合为3那么值的范围就是 0 1 2int index random.nextInt(list.size());return list.get(index);}return null;}
}3权重
Component
public class WeightLoadBalance implements LoadBalance {Resourceprivate DiscoveryClient client;private AtomicInteger atomicCount new AtomicInteger(0);/*** 使用权重算法实现负载均衡** param serviceId 服务id* return {link ServiceInstance}*/public ServiceInstance getInstances(String serviceId) {//通过服务名称获取到服务集群实例ListServiceInstance list client.getInstances(serviceId);if (list ! null list.size() 0) {ListServiceInstance total new LinkedListServiceInstance();for (ServiceInstance instance : list) {//获取到nacos中服务配置的权重Double weight Double.parseDouble(instance.getMetadata().get(nacos.weight));for (int i 0; i weight; i) {//对权重的大小进行循环比对,将服务实例添加到新的集合中。//权重越小循环越少集合中该服务就越少//权重越大循环越多集合中该服务就越多total.add(instance);}}//将新的集合与计数器进行计算获取到服务下标int index atomicCount.incrementAndGet() % total.size();return total.get(index);}return null;}
}总结
这篇只是了解一下负载均衡的算法是如何实现的在平常使用的话feign里已经实现了负载均衡策略所以不需要我们手动去撸代码实现。