当前位置:网站首页>LoadBalancer 负载均衡
LoadBalancer 负载均衡
2022-07-30 03:25:00 【Leon_Jinhai_Sun】
前面我们讲解了如何对服务进行拆分、如何通过Eureka服务器进行服务注册与发现,那么现在我们来看看,它的负载均衡到底是如何实现的,实际上之前演示的负载均衡是依靠LoadBalancer实现的。
在2020年前的SpringCloud版本是采用Ribbon作为负载均衡实现,但是2020年的版本之后SpringCloud把Ribbon移除了,进而用自己编写的LoadBalancer替代。
那么,负载均衡是如何进行的呢?
负载均衡
实际上,在添加@LoadBalanced注解之后,会启用拦截器对我们发起的服务调用请求进行拦截(注意这里是针对我们发起的请求进行拦截),叫做LoadBalancerInterceptor,它实现ClientHttpRequestInterceptor接口:
@FunctionalInterface
public interface ClientHttpRequestInterceptor {
ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException;
}主要是对intercept方法的实现:
public ClientHttpResponse intercept(final HttpRequest request, final byte[] body, final ClientHttpRequestExecution execution) throws IOException {
URI originalUri = request.getURI();
String serviceName = originalUri.getHost();
Assert.state(serviceName != null, "Request URI does not contain a valid hostname: " + originalUri);
return (ClientHttpResponse)this.loadBalancer.execute(serviceName, this.requestFactory.createRequest(request, body, execution));
}我们可以打个断点看看实际是怎么在执行的,可以看到:


服务端会在发起请求时执行这些拦截器。
那么这个拦截器做了什么事情呢,首先我们要明确,我们给过来的请求地址,并不是一个有效的主机名称,而是服务名称,那么怎么才能得到真正需要访问的主机名称呢,肯定是得找Eureka获取的。
我们来看看loadBalancer.execute()做了什么,它的具体实现为BlockingLoadBalancerClient:
//从上面给进来了服务的名称和具体的请求实体
public <T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException {
String hint = this.getHint(serviceId);
LoadBalancerRequestAdapter<T, DefaultRequestContext> lbRequest = new LoadBalancerRequestAdapter(request, new DefaultRequestContext(request, hint));
Set<LoadBalancerLifecycle> supportedLifecycleProcessors = this.getSupportedLifecycleProcessors(serviceId);
supportedLifecycleProcessors.forEach((lifecycle) -> {
lifecycle.onStart(lbRequest);
});
//可以看到在这里会调用choose方法自动获取对应的服务实例信息
ServiceInstance serviceInstance = this.choose(serviceId, lbRequest);
if (serviceInstance == null) {
supportedLifecycleProcessors.forEach((lifecycle) -> {
lifecycle.onComplete(new CompletionContext(Status.DISCARD, lbRequest, new EmptyResponse()));
});
//没有发现任何此服务的实例就抛异常(之前的测试中可能已经遇到了)
throw new IllegalStateException("No instances available for " + serviceId);
} else {
//成功获取到对应服务的实例,这时就可以发起HTTP请求获取信息了
return this.execute(serviceId, serviceInstance, lbRequest);
}
}所以,实际上在进行负载均衡的时候,会向Eureka发起请求,选择一个可用的对应服务,然后会返回此服务的主机地址等信息:

自定义负载均衡策略
LoadBalancer默认提供了两种负载均衡策略:
- RandomLoadBalancer - 随机分配策略
- (默认) RoundRobinLoadBalancer - 轮询分配策略
现在我们希望修改默认的负载均衡策略,可以进行指定,比如我们现在希望用户服务采用随机分配策略,我们需要先创建随机分配策略的配置类(不用加@Configuration):
public class LoadBalancerConfig {
//将官方提供的 RandomLoadBalancer 注册为Bean
@Bean
public ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment environment, LoadBalancerClientFactory loadBalancerClientFactory){
String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
return new RandomLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
}
}接着我们需要为对应的服务指定负载均衡策略,直接使用注解即可:
@Configuration
@LoadBalancerClient(value = "userservice", //指定为 userservice 服务,只要是调用此服务都会使用我们指定的策略
configuration = LoadBalancerConfig.class) //指定我们刚刚定义好的配置类
public class BeanConfig {
@Bean
@LoadBalanced
RestTemplate template(){
return new RestTemplate();
}
}接着我们在BlockingLoadBalancerClient中添加断点,观察是否采用我们指定的策略进行请求:


发现访问userservice服务的策略已经更改为我们指定的策略了。
边栏推荐
- Nuxt3学习
- The most important transport layer
- Drools(7):WorkBench
- SQL Server中如何在date类型中提取年、月、日数据
- 基于全志D1-H和XR806的名贵植物监控装置
- [JS] embedded iframe page usage
- Leetcode.24 两两交换链表中的节点(递归)
- Excuse me, when datax is synchronized to the oceanbase database, it is written according to the primary key update method. How to fill in the content in the drop-down box?
- 【JS】iframe 嵌入页面用法
- Overview of Federated Learning (1) - Background, Definition and Value of Federated Learning
猜你喜欢

英诺特生物上市市值45亿:年营收降68% 红杉与元生是股东
![[3D检测系列-PointRCNN]复现PointRCNN代码,并实现PointRCNN3D目标检测可视化,包含预训练权重下载链接(从0开始以及各种报错的解决方法)](/img/ca/17c047b8b1f030cc4ebb7e354070d9.png)
[3D检测系列-PointRCNN]复现PointRCNN代码,并实现PointRCNN3D目标检测可视化,包含预训练权重下载链接(从0开始以及各种报错的解决方法)

Simple Operations on Sequence

Software testing interview questions and answer analysis, the strongest version in 2022

状态空间表示

【GPU并行计算】利用OpenCL&OpenCLUtilty进行GPU并行计算

【ModelArts系列】华为ModelArts Notebook训练yolov3模型(开发环境)

Detailed explanation of carousel picture 2 - carousel pictures through left positioning

最重要的传输层

应用在光伏逆变器中的IGBT晶圆
随机推荐
【Node访问MongoDB数据库】
[Andrioid开发] Splash界面/用户协议与隐私政策弹窗/界面开发
【C补充】整数与字符串的转换
群论-Burnside引理与Polya定理 三千字
JUC (8) : synchronized little exercise
华宝新能通过注册:拟募资近7亿 营收增加利润反而下降
JIT VS AOT
【Flink】从开发到生产上线,如何确定集群规划大小 ?
day10--在Linux上安装mysql
【无标题】
应用在光伏逆变器中的IGBT晶圆
记录NLP各种资源网址
阿里云EasyNLP中文文图生成模型带你秒变艺术家
如何有效进行回顾会议(上)?
OA项目之待开会议&历史会议&所有会议
解决谷歌浏览器跨域问题has been blocked by CORS policy: The request client is not a secure context and the resou
Three years of experience will only be a little bit (functional testing), and you may not even be able to find a job after resigning.
力扣(LeetCode)210. 课程表 II(2022.07.29)
淘宝H5接口获取app数据6.0格式
动态绑定href url