Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of spring cloud netflix ribbon's eager load?

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the role of eager load of spring cloud netflix ribbon". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Order

This paper mainly studies the eager load of spring cloud netflix ribbon.

RibbonAutoConfiguration

SpringMurray NetflixUp 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 enables two configurations: RibbonEagerLoadProperties and ServerIntrospectorProperties. Here, SpringClientFactory is registered to create LoadBalancerClient, LoadBalancedRetryFactory, RibbonApplicationContextInitializer, RestTemplateCustomizer and RibbonClientHttpRequestFactory.

RibbonApplicationContextInitializer

SpringMurphy NetflixMuffle RibbonWhile 2.1.1.RELEASEMethsources.jarAccording orgAccording to springframeworkGreater cloudLichtBond 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 implements the ApplicationListener interface, which performs initialize operations in response to ApplicationReadyEvent events. Here, springClientFactory.getContext (clientName) operations are performed on the configured clientNames one by one.

SpringClientFactory

SpringMurray, Netflix, Netflix, Ribbon, 2.1.1.RELEASEMethsources.jarAccording to orgframe, springframeworkand cloudLetflix, SpringClientFactory.java.

Public class SpringClientFactory extends NamedContextFactory {/ /. Protected AnnotationConfigApplicationContext getContext (String name) {return super.getContext (name);} /.}

The getContext method of SpringClientFactory calls the getContext of the parent class NamedContextFactory

NamedContextFactory

Springhouse _ house _ contextr _ 2.2.0.M1 _ house _ sources.jar _ balance _ orgUnix _ frameworkhand _ cloudbank _ contextlap namedbank 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 } / /.}

The getContext of NamedContextFactory is mainly to return or create AnnotationConfigApplicationContext.

Summary

RibbonAutoConfiguration enables two configurations: RibbonEagerLoadProperties and ServerIntrospectorProperties. Here, SpringClientFactory is registered to create LoadBalancerClient, LoadBalancedRetryFactory, RibbonApplicationContextInitializer, RestTemplateCustomizer and RibbonClientHttpRequestFactory.

RibbonApplicationContextInitializer implements the ApplicationListener interface, which performs initialize operations in response to ApplicationReadyEvent events. Here, springClientFactory.getContext (clientName) operations are performed on the configured clientNames one by one.

The getContext method of SpringClientFactory calls the getContext of the getContext;NamedContextFactory of the parent class NamedContextFactory mainly to return or create an AnnotationConfigApplicationContext

This is the end of the content of "what is the use of spring cloud netflix ribbon's eager load"? thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report