千家信息网

spring cloud netflix ribbon的eager load有什么作用

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,本篇内容介绍了"spring cloud netflix ribbon的eager load有什么作用"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何
千家信息网最后更新 2026年11月03日spring cloud netflix ribbon的eager load有什么作用

本篇内容介绍了"spring cloud netflix ribbon的eager load有什么作用"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

序

本文主要研究一下spring cloud netflix ribbon的eager load

RibbonAutoConfiguration

spring-cloud-netflix-ribbon-2.1.1.RELEASE-sources.jar!/org/springframework/cloud/netflix/ribbon/RibbonAutoConfiguration.java

@Configuration@Conditional(RibbonAutoConfiguration.RibbonClassesConditions.class)@RibbonClients@AutoConfigureAfter(name = "org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration")@AutoConfigureBefore({ LoadBalancerAutoConfiguration.class,                AsyncLoadBalancerAutoConfiguration.class })@EnableConfigurationProperties({ RibbonEagerLoadProperties.class,                ServerIntrospectorProperties.class })public class RibbonAutoConfiguration {        @Autowired(required = false)        private List configurations = new ArrayList<>();        @Autowired        private RibbonEagerLoadProperties ribbonEagerLoadProperties;        @Bean        public HasFeatures ribbonFeature() {                return HasFeatures.namedFeature("Ribbon", Ribbon.class);        }        @Bean        public SpringClientFactory springClientFactory() {                SpringClientFactory factory = new SpringClientFactory();                factory.setConfigurations(this.configurations);                return factory;        }        @Bean        @ConditionalOnMissingBean(LoadBalancerClient.class)        public LoadBalancerClient loadBalancerClient() {                return new RibbonLoadBalancerClient(springClientFactory());        }        @Bean        @ConditionalOnClass(name = "org.springframework.retry.support.RetryTemplate")        @ConditionalOnMissingBean        public LoadBalancedRetryFactory loadBalancedRetryPolicyFactory(                        final SpringClientFactory clientFactory) {                return new RibbonLoadBalancedRetryFactory(clientFactory);        }        @Bean        @ConditionalOnMissingBean        public PropertiesFactory propertiesFactory() {                return new PropertiesFactory();        }        @Bean        @ConditionalOnProperty("ribbon.eager-load.enabled")        public RibbonApplicationContextInitializer ribbonApplicationContextInitializer() {                return new RibbonApplicationContextInitializer(springClientFactory(),                                ribbonEagerLoadProperties.getClients());        }        @Configuration        @ConditionalOnClass(HttpRequest.class)        @ConditionalOnRibbonRestClient        protected static class RibbonClientHttpRequestFactoryConfiguration {                @Autowired                private SpringClientFactory springClientFactory;                @Bean                public RestTemplateCustomizer restTemplateCustomizer(                                final RibbonClientHttpRequestFactory ribbonClientHttpRequestFactory) {                        return restTemplate -> restTemplate                                        .setRequestFactory(ribbonClientHttpRequestFactory);                }                @Bean                public RibbonClientHttpRequestFactory ribbonClientHttpRequestFactory() {                        return new RibbonClientHttpRequestFactory(this.springClientFactory);                }        }        // TODO: support for autoconfiguring restemplate to use apache http client or okhttp        @Target({ ElementType.TYPE, ElementType.METHOD })        @Retention(RetentionPolicy.RUNTIME)        @Documented        @Conditional(OnRibbonRestClientCondition.class)        @interface ConditionalOnRibbonRestClient {        }        private static class OnRibbonRestClientCondition extends AnyNestedCondition {                OnRibbonRestClientCondition() {                        super(ConfigurationPhase.REGISTER_BEAN);                }                @Deprecated // remove in Edgware"                @ConditionalOnProperty("ribbon.http.client.enabled")                static class ZuulProperty {                }                @ConditionalOnProperty("ribbon.restclient.enabled")                static class RibbonProperty {                }        }        /**         * {@link AllNestedConditions} that checks that either multiple classes are present.         */        static class RibbonClassesConditions extends AllNestedConditions {                RibbonClassesConditions() {                        super(ConfigurationPhase.PARSE_CONFIGURATION);                }                @ConditionalOnClass(IClient.class)                static class IClientPresent {                }                @ConditionalOnClass(RestTemplate.class)                static class RestTemplatePresent {                }                @ConditionalOnClass(AsyncRestTemplate.class)                static class AsyncRestTemplatePresent {                }                @ConditionalOnClass(Ribbon.class)                static class RibbonPresent {                }        }}
  • RibbonAutoConfiguration启用了RibbonEagerLoadProperties、ServerIntrospectorProperties两个配置,这里主要是注册了SpringClientFactory,创建LoadBalancerClient、LoadBalancedRetryFactory、RibbonApplicationContextInitializer、RestTemplateCustomizer及RibbonClientHttpRequestFactory

RibbonApplicationContextInitializer

spring-cloud-netflix-ribbon-2.1.1.RELEASE-sources.jar!/org/springframework/cloud/netflix/ribbon/RibbonApplicationContextInitializer.java

public class RibbonApplicationContextInitializer                implements ApplicationListener {        private final SpringClientFactory springClientFactory;        // List of Ribbon client names        private final List clientNames;        public RibbonApplicationContextInitializer(SpringClientFactory springClientFactory,                        List clientNames) {                this.springClientFactory = springClientFactory;                this.clientNames = clientNames;        }        protected void initialize() {                if (clientNames != null) {                        for (String clientName : clientNames) {                                this.springClientFactory.getContext(clientName);                        }                }        }        @Override        public void onApplicationEvent(ApplicationReadyEvent event) {                initialize();        }}
  • RibbonApplicationContextInitializer实现了ApplicationListener接口,其响应ApplicationReadyEvent事件执行initialize操作,这里主要是挨个对配置的clientNames执行springClientFactory.getContext(clientName)操作

SpringClientFactory

spring-cloud-netflix-ribbon-2.1.1.RELEASE-sources.jar!/org/springframework/cloud/netflix/ribbon/SpringClientFactory.java

public class SpringClientFactory extends NamedContextFactory {        //......        protected AnnotationConfigApplicationContext getContext(String name) {                return super.getContext(name);        }        //......}
  • SpringClientFactory的getContext方法调用了父类NamedContextFactory的getContext

NamedContextFactory

spring-cloud-context-2.2.0.M1-sources.jar!/org/springframework/cloud/context/named/NamedContextFactory.java

public abstract class NamedContextFactory                implements DisposableBean, ApplicationContextAware {        //......        protected AnnotationConfigApplicationContext getContext(String name) {                if (!this.contexts.containsKey(name)) {                        synchronized (this.contexts) {                                if (!this.contexts.containsKey(name)) {                                        this.contexts.put(name, createContext(name));                                }                        }                }                return this.contexts.get(name);        }        protected AnnotationConfigApplicationContext createContext(String name) {                AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();                if (this.configurations.containsKey(name)) {                        for (Class configuration : this.configurations.get(name)                                        .getConfiguration()) {                                context.register(configuration);                        }                }                for (Map.Entry entry : this.configurations.entrySet()) {                        if (entry.getKey().startsWith("default.")) {                                for (Class configuration : entry.getValue().getConfiguration()) {                                        context.register(configuration);                                }                        }                }                context.register(PropertyPlaceholderAutoConfiguration.class,                                this.defaultConfigType);                context.getEnvironment().getPropertySources().addFirst(new MapPropertySource(                                this.propertySourceName,                                Collections.singletonMap(this.propertyName, name)));                if (this.parent != null) {                        // Uses Environment from parent as well as beans                        context.setParent(this.parent);                        // jdk11 issue                        // https://github.com/spring-cloud/spring-cloud-netflix/issues/3101                        context.setClassLoader(this.parent.getClassLoader());                }                context.setDisplayName(generateDisplayName(name));                context.refresh();                return context;        }        //......}               
  • NamedContextFactory的getContext主要是返回或者创建AnnotationConfigApplicationContext

小结

  • RibbonAutoConfiguration启用了RibbonEagerLoadProperties、ServerIntrospectorProperties两个配置,这里主要是注册了SpringClientFactory,创建LoadBalancerClient、LoadBalancedRetryFactory、RibbonApplicationContextInitializer、RestTemplateCustomizer及RibbonClientHttpRequestFactory

  • RibbonApplicationContextInitializer实现了ApplicationListener接口,其响应ApplicationReadyEvent事件执行initialize操作,这里主要是挨个对配置的clientNames执行springClientFactory.getContext(clientName)操作

  • SpringClientFactory的getContext方法调用了父类NamedContextFactory的getContext;NamedContextFactory的getContext主要是返回或者创建AnnotationConfigApplicationContext

"spring cloud netflix ribbon的eager load有什么作用"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

配置 作用 两个 事件 内容 接口 方法 更多 知识 实用 学有所成 接下来 困境 实际 小结 情况 文章 案例 编带 网站 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 广东万易互联网科技有限公司惠州分公司 山东特亿宝互联网科技有限公司 西安纸贵互联网科技有限公司 河南真二互联网科技有限公司 互联网科技金融公司 九亚互联网科技 电子科技大学互联网 苏州大垚互联网科技 互联网科技论坛 互联网科技公司做什么 恒生科技互联网指数分析 银川新华互联网科技学校地址 央行互联网金融科技公司 厦门千工互联网科技有限公司 中和天地互联网科技有限公司 黑科技 体验场景 互联网大会 梅州GM互联网络科技有限公司 互联网时代科技型公司的机会 互联网科技手机排行榜 互联网金融运营模式中的金融科技 朗涯互联网科技 融势互联网科技 华脉科技工业互联网产品 深圳市优智互联网科技有限公司 本届互联网博览会上的黑科技 2021世界互联网领先科技成果发布 科技信息时代互联网发展 青岛易易互联网科技 网互联网络科技 科技美学互联网 艺术
0