问题
I have created a simple project using spring-cloud-config and spring-cloud-bus. For some reason, the config client calls twice to the config server. The first time happens when the context of the bootstrap is created (what is the expected behaviour) but the second time is called when the context of the binder is created.
This is done In the class:
DefaultBinderFactory#getBinderInstance:
// If the environment is not customized and a main context is available, we
// will set the latter as parent.
// This ensures that the defaults and user-defined customizations (e.g. custom
// connection factory beans)
// are propagated to the binder context. If the environment is customized,
// then the binder context should
// not inherit any beans from the parent
boolean useApplicationContextAsParent = binderProperties.isEmpty() && this.context != null;
if (useApplicationContextAsParent) {
springApplicationBuilder.parent(this.context);
}
if (useApplicationContextAsParent || (environment != null && binderConfiguration.isInheritEnvironment())) {
if (environment != null) {
StandardEnvironment binderEnvironment = new StandardEnvironment();
binderEnvironment.merge(environment);
springApplicationBuilder.environment(binderEnvironment);
}
}
ConfigurableApplicationContext binderProducingContext = springApplicationBuilder
.run(args.toArray(new String[args.size()]));
when is executed the following statement:
ConfigurableApplicationContext binderProducingContext = springApplicationBuilder
.run(args.toArray(new String[args.size()]));
As far as I see in the code, the context of the binders are different from the context of the application. It seems that the reason of calling another time to the config server is that spring-cloud-context creates its context with an initializer PropertySourceBootstrapConfiguration loaded by the listener BootstrapApplicationListener .
So, my question is: Is this the expected behaviour? I assume that the config client should make only one request to the server at startup. If it is not correct, why is the reason for it?
The versions that I am using are the included in: spring-cloud-dependencies, version Edgware.RELEASE
Many thanks in advance!
回答1:
Yes; it's expected; the spring cloud stream binder is loaded into its own Spring Boot application (it can have different environment variables).
来源:https://stackoverflow.com/questions/48406527/spring-cloud-config-bus-calls-twice-to-the-config-server-when-the-binder-is-cr