当前位置:网站首页>Ribbon源码分析之@LoadBalanced与LoadBalancerClient
Ribbon源码分析之@LoadBalanced与LoadBalancerClient
2022-06-24 18:58:00 【华为云】
Ribbon源码分析之@LoadBalanced与LoadBalancerClient
@LoadBalanced注解
@LoadBalanced注解用来给RestTemplate做标记,方便使用负载均衡的客户端LoadBalancerClient来配置它。
LoadBalancerClient:
public interface LoadBalancerClient { /** * Choose a ServiceInstance from the LoadBalancer for the specified service * @param serviceId the service id to look up the LoadBalancer * @return a ServiceInstance that matches the serviceId */ ServiceInstance choose(String serviceId); /** * execute request using a ServiceInstance from the LoadBalancer for the specified * service * @param serviceId the service id to look up the LoadBalancer * @param request allows implementations to execute pre and post actions such as * incrementing metrics * @return the result of the LoadBalancerRequest callback on the selected * ServiceInstance */ <T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException; /** * Create a proper URI with a real host and port for systems to utilize. * Some systems use a URI with the logical serivce name as the host, * such as http://myservice/path/to/service. This will replace the * service name with the host:port from the ServiceInstance. * @param instance * @param original a URI with the host as a logical service name * @return a reconstructed URI */ URI reconstructURI(ServiceInstance instance, URI original);}接口中,我们通过定义抽象方法来了解客户端负载均衡器中应具备的能力
ServiceInstance choose(String serviceId); 根据传入的服务名serviceId从负载均衡器中挑选一个对应服务的实例
T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException; 从负载均衡器中挑选出的服务实例来执行请求内容
URI reconstructURI(ServiceInstance instance, URI original); 为系统构建一个合适的host:port 形式的URI
在分布式系统中,我们使用逻辑上的服务名称作为host来构建URI进行请求。在操作定义中,ServiceInstance对象带有host和port具体服务实例,后者URI对象使用逻辑服务名定义为host的URI,返回的URI内容是通过ServiceInstance 的服务实例详情拼接出来的host:port形式的请求地址。
小知识,大挑战!本文正在参与「程序员必备小知识」创作活动
LoadBalancerClient
LoadBalancerAutoConfiguration实现客户端负载均衡器的自动化配置类。
LoadBalancerAutoConfiguration:
import java.util.ArrayList;import java.util.Collections;import java.util.List;import org.springframework.beans.factory.SmartInitializingSingleton;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.http.client.ClientHttpRequestInterceptor;import org.springframework.web.client.RestTemplate;/** * Auto configuration for Ribbon (client side load balancing). * * @author Spencer Gibb * @author Dave Syer */@Configuration@ConditionalOnClass(RestTemplate.class)@ConditionalOnBean(LoadBalancerClient.class)public class LoadBalancerAutoConfiguration { @LoadBalanced @Autowired(required = false) private List<RestTemplate> restTemplates = Collections.emptyList(); @Bean public SmartInitializingSingleton loadBalancedRestTemplateInitializer( final List<RestTemplateCustomizer> customizers) { return new SmartInitializingSingleton() { @Override public void afterSingletonsInstantiated() { for (RestTemplate restTemplate : LoadBalancerAutoConfiguration.this.restTemplates) { for (RestTemplateCustomizer customizer : customizers) { customizer.customize(restTemplate); } } } }; } @Bean @ConditionalOnMissingBean public RestTemplateCustomizer restTemplateCustomizer( final LoadBalancerInterceptor loadBalancerInterceptor) { return new RestTemplateCustomizer() { @Override public void customize(RestTemplate restTemplate) { List<ClientHttpRequestInterceptor> list = new ArrayList<>( restTemplate.getInterceptors()); list.add(loadBalancerInterceptor); restTemplate.setInterceptors(list); } }; } @Bean public LoadBalancerInterceptor ribbonInterceptor( LoadBalancerClient loadBalancerClient) { return new LoadBalancerInterceptor(loadBalancerClient); }}LoadBalancerAutoConfiguration类头的注解表示Ribbon实现的负载均衡自动化配置需要满足两个条件
- @ConditionalOnClass(RestTemplate.class):RestTemplate必须在当前工程的环境中
- @ConditionalOnBean(LoadBalancerClient.class):在Spring的Bean工厂中必须有LoadBalancerClient的实现Bean
自动化配置类中主要做三件事:
- 创建一个LoadBalancerInterceptor的Bean,用于实现对客户端发起请求时进行拦截,实现客户端负载均衡
- 创建RestTemplateCustomizer的Bean,用于RestTemplate增加LoadBalancerInterceptor拦截器
- 维护被LoadBalanced注解修饰的RestTemplate对象列表,并在这里进行初始化,通过RestTemplateCustomizer的实例给客户端负载均衡的RestTemplate增加LoadBalancerInterceptor拦截器
总结
这就是LoadBalancerAutoConfiguration的源码分析,它可以实现客户端负载均衡器的自动化配置类。这下一篇文章中我们要分析拦截器LoadBalancerInterceptor做了什么功能,我们下一篇文章不见不散咯。让我们一起深入Ribbon源码,共同学习
边栏推荐
- Data backup and recovery of PgSQL
- finkcdc支持sqlserver2008么?
- Volcano becomes spark default batch scheduler
- LCD1602 string display (STM32F103)
- Ls common parameters
- Install the custom module into the system and use find in the independent project_ Package found
- php OSS文件讀取和寫入文件,workerman生成臨時文件並輸出瀏覽器下載
- 【Go語言刷題篇】Go從0到入門4:切片的高級用法、初級複習與Map入門學習
- 60 divine vs Code plug-ins!!
- [video tutorial] functions that need to be turned off in win10 system. How to turn off the privacy option in win10 computer
猜你喜欢

应用实践 | 海量数据,秒级分析!Flink+Doris 构建实时数仓方案

Zadig + cave Iast: let safety dissolve in continuous delivery

Geoscience remote sensing data collection online

一次 MySQL 误操作导致的事故,高可用都不顶不住!

Working for 6 years with a monthly salary of 3W and a history of striving for one PM

Confirm whether the host is a large terminal or a small terminal

Error in Android connection database query statement

Experience of MDM master data project implementation for manufacturing projects
![[R tidyverse] use of select verb](/img/2d/768391bc6ec497432915024bc90842.jpg)
[R tidyverse] use of select verb

【Go語言刷題篇】Go從0到入門4:切片的高級用法、初級複習與Map入門學習
随机推荐
Unityshader world coordinates do not change with the model
Apache+PHP+MySQL环境搭建超详细!!!
Generate the last login user account report of the computer through SCCM SQL
php OSS文件读取和写入文件,workerman生成临时文件并输出浏览器下载
thinkphp6中怎么使用jwt认证
Ls common parameters
First understand redis' data structure - string
PHP OSS file reads and writes files, and workman generates temporary files and outputs them to the browser for download
What are the functions of IBPs open source form designer?
60 个神级 VS Code 插件!!
Buddha bless you that there will never be a bug
finkcdc支持sqlserver2008么?
8 challenges of BSS application cloud native deployment
Instruction rearrangement concept
Example analysis of corrplot related heat map beautification in R language
Fundamentals of performance testing -- definitions of common terms
R语言corrplot相关热图美化实例分析
Why is the executor thread pool framework introduced
Write a positive integer to the node and return a floating-point number multiplied by 0.85 when reading the node
LCD1602 string display (STM32F103)