问题
My goal is to add custom PropertySource to spring-cloud-server. What I want to achieve is to get some custom properties from that custom source in spring-cloud-config-client application.
Basing on suggestions from Adding environment repository in spring-config-server I've created spring-cloud-config-server
application and separate project spring-cloud-config-custom
. Second one is based on spring-cloud-consul-config code. So, I've created all necessary classes like CustomPropertySource
, CustomPropertySourceLocator
, CustomConfigBootstrapConfiguration
and so on and configured them in spring.factories
.
At the end, I've added maven dependency to spring-cloud-config-custom
inside my spring-cloud-config-server
.
So far so good. Everything works well. When I start server I can see that my CustomPropertySource
is on the list of propertySources inside EnviromentRepository
bean injected to EnvironmentController
.
Problem: When I send GET request to @RequestMapping("/{name}/{profiles}/{label:.*}")
(in EnvironmentController
), injected EnviromentRepository
bean is being used to find requested property source (repository.findOne(name, profiles, label)
method).
Unfortunately my property source could not be found here. Why?
I've spent a lot of time on debugging this. I've found that repository delegates findOne()
method call to other repositories: MultipleJGitEnvironmentRepository
which delegates it to NativeEnvironmentRepository
. Inside this delegates, findOne() method doesn't use propertySources from EnviromentRepository
primary injected to controller. It creates new environment repository with new list of PropertySources and new separate SpringApplication. At the end, this list does not contain my CustomPropertySource
and that is why findOne()
returns empty propertySources in resulting Environment
object.
- Am I doing something wrong?
- Is
CustomPropertySourceLocator
(and/orConsulPropertySourceLocator
) supposed to be used (autowired/bootstrapped) inspring-cloud-config-server
orspring-cloud-config-client
- Can spring-cloud-config-server deliver many different kind of
PropertySources
at the same time, via REST interface (saying "different" I mean all Git, Consul and Zookeeper)?
回答1:
What you are doing is adding a property source to the config server itself, not the configuration it serves. Adding spring-boot-starter-actuator
to your config server and viewing /env
reveals:
{
"profiles": [
],
"server.ports": {
"local.server.port": 8888
},
"bootstrapProperties:custom": {
"test.prop3": "CUSTOM-VALUE-3",
"test.prop2": "CUSTOM-VALUE-2",
"test.prop1": "CUSTOM-VALUE-1"
},
}
To add something that will be served by config server, you have to implement an EnvironmentRepository
.
Support for a composite EnvironmentRepository
was recently added.
来源:https://stackoverflow.com/questions/41267970/spring-cloud-config-server-how-to-add-custom-propertysource-visible-in-findone