问题
We are leveraging Spring Cloud Config and Spring Cloud Config Vault. We would like to know if there is a way to "bootstrap the bootstrap", ie we want spring cloud config server to be hit and then pull properties from that to leverage in our vault configuration. We looked at order, but it didn't appear to work, and I assume it is because of the post processing order, but I was hoping I might be missing something.
回答1:
TL;DR
It doesn't work.
Explanation
What Spring Cloud does with its bootstrap context, is setting up an application context that contains a set of PropertySource
s initialized from Spring beans. The bootstrap context is used then as parent context for the actual context created by Spring Boot. A property lookup looks for properties in its own context and within the parent context.
Configuration properties are initialized very early in the startup process and they use properties from the current Environment
. At the time ConfigurationProperties
beans are initialized, the Environment
does not yet contain any remote PropertySource
s.
The only option I see here (except creating a bootstrap-bootstrap-context) is using the Spring Cloud Config client within your main
class and contribute Vault properties before any Spring context is built.
回答2:
Probably you can, but it requires PropertySourceBootstrapConfiguration#initialize()
method overriding. You can't disable bean PropertySourceBootstrapConfiguration
, but you can disable it's initialize method by using applicationContext.getBeanFactory().getBean(PropertySourceBootstrapConfiguration.class).setPropertySourceLocators(new ArrayList<>())
in CustomPropertySourceBootstrapConfiguration
(to avoid obsolete external property sources calls).
In your CustomPropertySourceBootstrapConfiguration#initialize
method you can retrieve properties from config-server and then customize your vaultPropertySourceLocator
by inserting generated in config-server secretId of token.
Don't forget to add your CustomPropertySourceBootstrapConfiguration
to spring.factories.
So, it's not easy but it is possible.
来源:https://stackoverflow.com/questions/48797436/spring-cloud-config-and-spring-cloud-vault-order-of-initialization