• Spring Cloud 客服端负载均衡 Ribbon


    /**
     * Auto configuration for Ribbon (client side load balancing).
     *
     * @author Spencer Gibb
     * @author Dave Syer
     * @author Will Tran
     */
    @Configuration
    @ConditionalOnClass(RestTemplate.class//条件 : RestTemplate必须在工程的类路径下
    @ConditionalOnBean(LoadBalancerClient.class)  //条件: Spring 容器中必须包含LoadBalancerClient的实现,即RibbonLoadBalancerClient
    @EnableConfigurationProperties(LoadBalancerRetryProperties.class//启动重试功能,可以spring.cloud.loadbalancer.retry=false,取消重试,默认参数为true
    public class LoadBalancerAutoConfiguration {
        @LoadBalanced
        @Autowired(required = false)
        private List<RestTemplate> restTemplates = Collections.emptyList(); //维护一个RestTemplate列表,通过LoadBalanced来注解。
        @Bean
        public SmartInitializingSingleton loadBalancedRestTemplateInitializer( //加载初始话自定义的restTeplate,实质是初始化InterceptingHttpAccessor具体调用
                final List<RestTemplateCustomizer> customizers) {
            return new SmartInitializingSingleton() {
                @Override
                public void afterSingletonsInstantiated() {
                    for (RestTemplate restTemplate : LoadBalancerAutoConfiguration.this.restTemplates) {
                        for (RestTemplateCustomizer customizer : customizers) {
                            customizer.customize(restTemplate);
                        }
                    }
                }
            };
        }
        @Autowired(required = false)
        private List<LoadBalancerRequestTransformer> transformers = Collections.emptyList();
        @Bean
        @ConditionalOnMissingBean
        public LoadBalancerRequestFactory loadBalancerRequestFactory(
                LoadBalancerClient loadBalancerClient) {
            return new LoadBalancerRequestFactory(loadBalancerClient, transformers);
        }
        @Configuration
        @ConditionalOnMissingClass("org.springframework.retry.support.RetryTemplate")
        static class LoadBalancerInterceptorConfig {
            @Bean
            public LoadBalancerInterceptor ribbonInterceptor(
                    LoadBalancerClient loadBalancerClient,
                    LoadBalancerRequestFactory requestFactory) {
                return new LoadBalancerInterceptor(loadBalancerClient, requestFactory);
            }
            @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);
                    }
                };
            }
        }
        @Configuration
        @ConditionalOnClass(RetryTemplate.class)
        static class RetryAutoConfiguration {
            @Bean
            public RetryTemplate retryTemplate() {
                RetryTemplate template =  new RetryTemplate();
                template.setThrowLastExceptionOnExhausted(true);
                return template;
            }
            @Bean
            @ConditionalOnMissingBean
            public LoadBalancedRetryPolicyFactory loadBalancedRetryPolicyFactory() {
                return new LoadBalancedRetryPolicyFactory.NeverRetryFactory();
            }
            @Bean
            public RetryLoadBalancerInterceptor ribbonInterceptor(
                    LoadBalancerClient loadBalancerClient, LoadBalancerRetryProperties properties,
                    LoadBalancedRetryPolicyFactory lbRetryPolicyFactory,
                    LoadBalancerRequestFactory requestFactory) {
                return new RetryLoadBalancerInterceptor(loadBalancerClient, retryTemplate(), properties,
                        lbRetryPolicyFactory, requestFactory);
            }
            @Bean
            @ConditionalOnMissingBean
            public RestTemplateCustomizer restTemplateCustomizer( //自定义RestTemplate ,实质是初始化InterceptingHttpAccessor
                    final RetryLoadBalancerInterceptor loadBalancerInterceptor) {
                return new RestTemplateCustomizer() {
                    @Override
                    public void customize(RestTemplate restTemplate) {
                        List<ClientHttpRequestInterceptor> list = new ArrayList<>(
                                restTemplate.getInterceptors());
                        list.add(loadBalancerInterceptor);
                        restTemplate.setInterceptors(list);
                    }
                };
            }
        }
    }
  • 相关阅读:
    1132: 零起点学算法39——多组测试数据(a+b)
    1131: 零起点学算法38——求阶乘和
    1130: 零起点学算法37——求阶乘
    1129: 零起点学算法36——3n+1问题
    1128: 零起点学算法35——再求多项式(含浮点)
    1127: 零起点学算法34——继续求多项式
    1126: 零起点学算法33——求多项式
    Netty源码分析第1章(Netty启动流程)---->第1节: 服务端初始化
    Netty源码分析(前言, 概述及目录)
    基于HTK语音工具包进行孤立词识别的使用教程
  • 原文地址:https://www.cnblogs.com/zhangyuhang3/p/6909902.html
Copyright © 2020-2023  润新知