当前位置:网站首页>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博客“
温馨提示此为有偿服务;
边栏推荐
猜你喜欢

Go语学习笔记 - gorm使用 - 原生sql、命名参数、Rows、ToSQL Web框架Gin(九)

WebShell Feature Value Summary and Detection Tool

Istio微服务治理网格的全方面可视化监控(微服务架构展示、资源监控、流量监控、链路监控)

Nacos source code analysis topic (2) - service registration

JDBC--Druid数据库连接池以及Template基本用法

ReentrantLock工作原理

R16 Type II量化反馈码本的产生

MySQL六脉神剑,SQL通关大总结

嵌入式分享合集25

MySQL8.0.28安装教程
随机推荐
"Paid paddling" stealthily brushes Brother Ali's face scriptures, challenges bytes three times, and finally achieves positive results
非稳压 源特电子 隔离电源模块芯片 5W VPS8504B 24V
Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)
svm.SVC application practice 1--Breast cancer detection
AcWing 1053. Repair DNA problem solution (state machine DP, AC automata)
【LeetCode】1374. Generate a string with an odd number of each character
(1) Redis: Key-Value based storage system
MySQL8--Windows下使用压缩包安装的方法
WebShell connection tools (Chinese kitchen knife, WeBaCoo, Weevely) use
7-35 城市间紧急救援 (25 分)c语言(测试点二未通过)
【每日一道LeetCode】——9. 回文数
mysql使用on duplicate key update批量更新数据
MySql中的like和in走不走索引
Chapter 7 Noise analysis
消息队列经典十连问
just write blindly = feelings
analog IC layout
【Koltin Flow(三)】Flow操作符之中间操作符(一)
MySQL中根据日期进行范围查询
MySQL修改最大连接数限制