当前位置:网站首页>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

原网站

版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241734429673.html