0


【SpringCloud负载均衡】【源码+图解】【二】LoadBalancer配置

【SpringCloud负载均衡】【源码+图解】【一】LoadBalancer的HelloWorld体验

目录

2. LoadBalancer的配置

下图是User客户端启动时配置文件的加载情况,箭头代表依赖。
在这里插入图片描述

2.1 config.LoadBalancerAutoConfiguration

// LoadBalancerClientsProperties即spring.cloud.loadbalancer.*的配置@EnableConfigurationProperties(LoadBalancerClientsProperties.class)// 如果不想要loadBalance功能可以将spring.cloud.loadbalancer.enabled=false,默认true,即默认开启LoadBalance功能@ConditionalOnProperty(value ="spring.cloud.loadbalancer.enabled", havingValue ="true", matchIfMissing =true)publicclassLoadBalancerAutoConfiguration{// 默认下是这些配置类//        default.org.springframework.cloud.netflix.eureka.loadbalancer.LoadBalancerEurekaAutoConfiguration//        org.springframework.cloud.netflix.eureka.loadbalancer.EurekaLoadBalancerClientConfiguration//        default.org.springframework.cloud.loadbalancer.config.BlockingLoadBalancerClientAutoConfiguration//        default.org.springframework.cloud.loadbalancer.config.LoadBalancerAutoConfigurationprivatefinalObjectProvider<List<LoadBalancerClientSpecification>> configurations;publicLoadBalancerAutoConfiguration(ObjectProvider<List<LoadBalancerClientSpecification>> configurations){this.configurations = configurations;}// 它的作用是提供ZonePreferenceServiceInstanceListSupplier所需的zone,见4.2.2节@Bean@ConditionalOnMissingBeanpublicLoadBalancerZoneConfigzoneConfig(Environment environment){returnnewLoadBalancerZoneConfig(environment.getProperty("spring.cloud.loadbalancer.zone"));}// 注入LoadBalancerClientFactory,它的作用是创建ApplicationContext并提供loadBalance所需的类,见3.2节@ConditionalOnMissingBean@BeanpublicLoadBalancerClientFactoryloadBalancerClientFactory(LoadBalancerClientsProperties properties){LoadBalancerClientFactory clientFactory =newLoadBalancerClientFactory(properties);
        clientFactory.setConfigurations(this.configurations.getIfAvailable(Collections::emptyList));return clientFactory;}}

2.2 BlockingLoadBalancerClientAutoConfiguration

@LoadBalancerClients// 在前面的LoadBalancerAutoConfiguration之后配置@AutoConfigureAfter(LoadBalancerAutoConfiguration.class)@ConditionalOnClass(RestTemplate.class)publicclassBlockingLoadBalancerClientAutoConfiguration{// 默认的BlockingLoadBalancerClient,它的作用是执行loadBalance的真正逻辑,见3节@Bean@ConditionalOnBean(LoadBalancerClientFactory.class)@ConditionalOnMissingBeanpublicLoadBalancerClientblockingLoadBalancerClient(LoadBalancerClientFactory loadBalancerClientFactory){returnnewBlockingLoadBalancerClient(loadBalancerClientFactory);}// 注入LoadBalancerServiceInstanceCookieTransformer,它的作用是将配置sticky-session的信息添加到请求头,见3.5节@Bean@ConditionalOnBean(LoadBalancerClientFactory.class)@ConditionalOnMissingBean(LoadBalancerServiceInstanceCookieTransformer.class)publicLoadBalancerServiceInstanceCookieTransformerloadBalancerServiceInstanceCookieTransformer(LoadBalancerClientFactory loadBalancerClientFactory){returnnewLoadBalancerServiceInstanceCookieTransformer(loadBalancerClientFactory);}// 注入XForwardedHeadersTransformer,它的作用是将配置中的x-forwarded信息添加到请求头,见3.5节@Bean@ConditionalOnMissingBean(XForwardedHeadersTransformer.class)@ConditionalOnBean(LoadBalancerClientFactory.class)publicXForwardedHeadersTransformerxForwarderHeadersTransformer(LoadBalancerClientFactory loadBalancerClientFactory){returnnewXForwardedHeadersTransformer(loadBalancerClientFactory);}}

2.3 LoadBalancerEurekaAutoConfiguration

// 如果开启eureka客户端功能,则注入相应功能的类@ConditionalOnProperty(name ="eureka.client.enabled", matchIfMissing =true)publicclassLoadBalancerEurekaAutoConfiguration{publicstaticfinalStringLOADBALANCER_ZONE="spring.cloud.loadbalancer.zone";// spring.cloud.loadbalancer.eureka.*的配置@Bean@ConditionalOnMissingBeanEurekaLoadBalancerPropertieseurekaLoadBalancerProperties(){returnnewEurekaLoadBalancerProperties();}// spring.cloud.loadbalancer.zone@Bean@ConditionalOnMissingBeanLoadBalancerZoneConfigzoneConfig(Environment environment){returnnewLoadBalancerZoneConfig(environment.getProperty(LOADBALANCER_ZONE));}}

2.4 loadbalancer.LoadBalancerAutoConfiguration & LoadBalancerInterceptorConfig

LoadBalancerInterceptorConfig是loadbalancer.LoadBalancerAutoConfiguration的内部类,按着顺序看逻辑更清晰

publicclassLoadBalancerAutoConfiguration{@LoadBalanced@Autowired(required =false)// 4、获取所有需要LoadBalanced的RestTemplateprivateList<RestTemplate> restTemplates =Collections.emptyList();// LoadBalancerServiceInstanceCookieTransformer// XForwardedHeadersTransformer@Autowired(required =false)privateList<LoadBalancerRequestTransformer> transformers =Collections.emptyList();@BeanpublicSmartInitializingSingletonloadBalancedRestTemplateInitializerDeprecated(finalObjectProvider<List<RestTemplateCustomizer>> restTemplateCustomizers){return()-> restTemplateCustomizers.ifAvailable(customizers ->{for(RestTemplate restTemplate :LoadBalancerAutoConfiguration.this.restTemplates){for(RestTemplateCustomizer customizer : customizers){// 5、给每个RestTemplate添加所有装饰器的功能
                    customizer.customize(restTemplate);}}});}@Bean@ConditionalOnMissingBeanpublicLoadBalancerRequestFactoryloadBalancerRequestFactory(LoadBalancerClient loadBalancerClient){// 1、初始化创建LoadBalancerRequest的工厂,作用是将如同http请求封装成BlockingLoadBalancerRequestreturnnewLoadBalancerRequestFactory(loadBalancerClient,this.transformers);}@Configuration(proxyBeanMethods =false)// 如果org.springframework.retry.support.RetryTemplate不存在或者spring.cloud.loadbalancer.retry.enabled为false// 默认情况下两个条件都符合,所以默认情况下走LoadBalancerInterceptor,否则走RetryLoadBalancerInterceptor@Conditional(RetryMissingOrDisabledCondition.class)staticclassLoadBalancerInterceptorConfig{// 2、初始化拦截器LoadBalancerInterceptor,作用是拦截@LoadBalanced的RestTemplate发出的请求@BeanpublicLoadBalancerInterceptorloadBalancerInterceptor(LoadBalancerClient loadBalancerClient,LoadBalancerRequestFactory requestFactory){returnnewLoadBalancerInterceptor(loadBalancerClient, requestFactory);}// 3、初始化装饰器,作用是将LoadBalancerInterceptor添加到RestTemplate@Bean@ConditionalOnMissingBeanpublicRestTemplateCustomizerrestTemplateCustomizer(finalLoadBalancerInterceptor loadBalancerInterceptor){return restTemplate ->{// 装饰器的功能:给restTemplate添加拦截器List<ClientHttpRequestInterceptor> list =newArrayList<>(restTemplate.getInterceptors());
                list.add(loadBalancerInterceptor);
                restTemplate.setInterceptors(list);};}}}

客户端成功启动后上述的配置就会生效,当客户端调用restTemplate发出请求时请求被Interceptor拦截,然后实现负载均衡。

未完待续


本文转载自: https://blog.csdn.net/Lanna_w/article/details/128210078
版权归原作者 三横兰 所有, 如有侵权,请联系我们删除。

“【SpringCloud负载均衡】【源码+图解】【二】LoadBalancer配置”的评论:

还没有评论