当前位置:网站首页>Ribbon本地实现负载均衡
Ribbon本地实现负载均衡
2022-08-02 02:58:00 【小码助力助你前行】
负载均衡Ribbon
LB负载均衡(Load Balance)是什么
简单的来说就是将用户的请求平摊的分配到多个服务上,从而达到系统的HA(高可用)。长建的负载均衡软件有Nginx,LVS,硬件F5等。
Ribbon本地负载均衡客户端和Nginx服务端负载均衡的区别
Nginx是服务器负载均衡,客户端所有请求都会交给Nginx,然后由Nginx实现转发请求,即负载均衡是由服务端实现的。
Ribbon本地负载均衡,在调用服务接口的时候,会在注册中心上获取注册信息服务列表之后缓存到JVM本地,从而在本地实现RPC远程服务调用技术。
负载均衡的算法
负载均衡算法:rest接口第几次请求数%服务器集群总数量 = 实际调用服务器下表,每次服务器重启rest接口计数从1开始
例:
如果这里有y台机器
x为访问的次数
负载均衡算法就是 x % y => 访问的下标(注:如果重启服务器将从第一次开始)
使用entity返回
@GetMapping("/getForEntity/{id}")
public CommonResult<Payment> queryById2(@PathVariable Long id){
ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL + "payment/get/"+id,CommonResult.class);
//表示返回编码是2开头的
if(entity.getStatusCode().is2xxSuccessful()){
return entity.getBody();
}else{
return new CommonResult<>(404,"操作失败");
}
}
Ribbon负载均衡策略
- 随机:RandomRule
- 轮询:RoundRobinRule
- 最小并发:BestAvailabelRule
- 过滤:AvailabilityFilteringRule
- 响应时间:WeightedResponseTimeRule
- 轮询重试:RetryRule
- 性能可用性:ZoneAvoidanceRule
自定义负载均衡算法
1.定义负载均衡接口
package com.yuan.lb;
import org.springframework.cloud.client.ServiceInstance;
import java.util.List;
public interface LoadBalancer {
//1.收集Eureka集群所有存活的服务器
//ServiceInstance 服务实例
ServiceInstance instances(List<ServiceInstance> serviceInstances);
}
2.实现自己的负载均衡接口
package com.yuan.lb.imp;
import com.yuan.lb.LoadBalancer;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
@Component //让容器扫描到自己写的组件
public class MyLB implements LoadBalancer {
private AtomicInteger atomicInteger = new AtomicInteger(0);
//得到数量在进行增加
public final int getAndIncrement(){
int current;
int next;
do{
//第一次是0 因为上面设设置数值时赋值为0
current = this.atomicInteger.get();
//整型最大数 ——> 2147483647
//第一次是0 0不大于2147483647 所以 next = 0 +1 注:current上面得到的是0 后面以此类推
next = current >= 2147483647 ? 0 :current +1;
//如果取到值就返回true 但是这里我们取反所以返回false跳出循环
}while (!this.atomicInteger.compareAndSet(current,next));
System.out.println("第几次访问next ========》 " + next);
return next;
}
@Override
//负载均衡算法:rest 接口第几次请求%服务器集群总数量 = 实际调用服务器位置小标,每次重启后rest接口计数从1开始
public ServiceInstance instances(List<ServiceInstance> serviceInstances) {
//获取下标值
int index = getAndIncrement() % serviceInstances.size();
return serviceInstances.get(index);
}
}
3.使用注解找到我们的load Balancer
@Resource
private LoadBalancer loadBalancer;
4.使用我们的load Balancer来获取URI
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
if (instances == null || instances.size()<=0){
return null;
}
ServiceInstance serviceInstance = loadBalancer.instances(instances);
//获取uri
URI uri = serviceInstance.getUri();
//拼接uri
return restTemplate.getForObject(uri+"/payment/lb",String.class);
我是本期小编
遇到Bug需要帮助,
欢迎加wx:
xmzl1988
备注"csdn博客“
温馨提示此为有偿服务;
边栏推荐
猜你喜欢
随机推荐
DVWA之SQL注入
svm.SVC application practice 1--Breast cancer detection
非稳压 源特电子 隔离电源模块芯片 5W VPS8504B 24V
MySQL index optimization in practice
WebShell Feature Value Summary and Detection Tool
Reasons and solutions for Invalid bound statement (not found)
ReentrantLock工作原理
AcWing 1285. 单词 题解(AC自动机)
第 304 场力扣周赛
Nacos source code analysis topic (2) - service registration
Navicat cannot connect to database Mysql because of WiFi
MySQL函数(经典收藏)
合奥科技网络 面试(含参考答案)
程序员的七夕浪漫时刻
I will give you a chance to interview in a big factory. Can you interview?Come in and see!
- daily a LeetCode 】 【 9. Palindrome
PHP WebSehll 后门脚本与检测工具
很有意思的经历,很有意思的项目--文件夹对比工具
AcWing 1053. 修复DNA 题解(状态机DP、AC自动机)
Heuristic merge, DSU on Tree









