当前位置:网站首页>Ribbon source code analysis @loadbalanced and loadbalancerclient
Ribbon source code analysis @loadbalanced and loadbalancerclient
2022-06-24 19:58:00 【Hua Weiyun】
Ribbon Source code analysis @LoadBalanced And LoadBalancerClient
@LoadBalanced annotation
@LoadBalanced Annotations are used to give RestTemplate Make a mark , Easy to use load balancing client LoadBalancerClient To configure it .
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);}Interface , We define abstract methods to understand the capabilities that should be available in the client load balancer
ServiceInstance choose(String serviceId); According to the incoming service name serviceId Select an instance of the corresponding service from the load balancer
T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException; The service instance selected from the load balancer executes the requested content
URI reconstructURI(ServiceInstance instance, URI original); Build an appropriate... For the system host:port Formal URI
In distributed systems , We use the logical service name as host To build URI For the request . In the operation definition ,ServiceInstance Object with host and port Specific service examples , the latter URI The object is defined as... Using the logical service name host Of URI, Back to URI The content is through ServiceInstance The details of the service instance are spliced together host:port Form of request address .
Little knowledge , Great challenge ! This article is participating in 「 A programmer must have a little knowledge 」 Creative activities
LoadBalancerClient
LoadBalancerAutoConfiguration Implement the automatic configuration class of client load balancer .
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 The annotation of the class header indicates Ribbon The automatic configuration of load balancing needs to meet two conditions
- @ConditionalOnClass(RestTemplate.class):RestTemplate Must be in the context of the current project
- @ConditionalOnBean(LoadBalancerClient.class): stay Spring Of Bean There must be... In the factory LoadBalancerClient The implementation of the Bean
The automatic configuration class mainly does three things :
- Create a LoadBalancerInterceptor Of Bean, It is used to intercept requests from clients , Achieve client load balancing
- establish RestTemplateCustomizer Of Bean, be used for RestTemplate increase LoadBalancerInterceptor Interceptor
- Maintenance by LoadBalanced annotated RestTemplate The object list , And initialize here , adopt RestTemplateCustomizer The instance of is for client load balancing RestTemplate increase LoadBalancerInterceptor Interceptor
summary
This is it. LoadBalancerAutoConfiguration Source code analysis , It can realize the automatic configuration class of client load balancer . In this next article, we will analyze interceptors LoadBalancerInterceptor What's the function , Let's see you in our next article . Let's go deeper Ribbon Source code , Learning together
边栏推荐
- 【CANN文档速递04期】揭秘昇腾CANN算子开发
- 【CANN文档速递06期】初识TBE DSL算子开发
- RF_DC系统时钟设置GEN1/GEN2
- Php OSS file read and write file, workerman Generate Temporary file and Output Browser Download
- A detailed explanation of the implementation principle of go Distributed Link Tracking
- gateway
- Two solutions to the problem of 0xv0000225 unable to start the computer
- Full link service tracking implementation scheme
- Obstacle avoidance sensor module (stm32f103c8t6)
- 【CANN文档速递05期】一文让您了解什么是算子
猜你喜欢

Information theory of popular science Shannon

Write a positive integer to the node and return a floating-point number multiplied by 0.85 when reading the node

Full link service tracking implementation scheme

Nodered has no return value after successfully inserting into the database (the request cannot be ended)

怎么使用R包ggtreeExtra绘制进化树

1、 Downloading and installing appium

Teach you how to view the number of connected people on WiFi in detail how to view the number of connected people on WiFi

Based on STM32F103 0.96 inch OLED LCD driver (IIC communication)

R language 4.1.0 software installation package and installation tutorial

Install the custom module into the system and use find in the independent project_ Package found
随机推荐
IP address to integer
R language 4.1.0 software installation package and installation tutorial
Oracle create tablespaces and tables
Database index can improve query efficiency. Ask what will improve, what is the difference between inapplicable index and index use, and what will happen.
unity实战之lol技能释放范围
Using dynamic time warping (DTW) to solve the similarity measurement of time series and the similarity identification analysis of pollution concentration in upstream and downstream rivers
What type of datetime in the CDC SQL table should be replaced
程序员大部分时间不是写代码,而是。。。
Does version 2.2.0 support dynamic addition of MySQL synchronization tables
Zadig + 洞态 IAST:让安全溶于持续交付
Unityshader world coordinates do not change with the model
Why is the executor thread pool framework introduced
UART communication (STM32F103 library function)
敏捷之道 | 敏捷开发真的过时了么?
R for Data Science (note) -- data transformation (select basic use)
First understand redis' data structure - string
The script implements the automated deployment of raid0
PHP OSS file reads and writes files, and workman generates temporary files and outputs them to the browser for download
Kubernetes cluster deployment
Camera module and hardware interface of Camera1 camera