Spring Cloud 配置服务器 - 如何在 EnvironmentEncryptorEnvironmentRepository 的 findOne() 方法中添加可见的自定义 PropertySource [英] Spring cloud config server - how to add custom PropertySource visible in findOne() method of EnvironmentEncryptorEnvironmentRepository
问题描述
我的目标是将自定义 PropertySource 添加到 spring-cloud-server.我想要实现的是从 spring-cloud-config-client 应用程序中的自定义源获取一些自定义属性.
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.
基于 在 spring-config-server 中添加环境存储库的建议 我已经创建了 spring-cloud-config-server
应用程序和单独的项目 spring-cloud-config-custom
.第二个是基于 spring-cloud-consul-config 代码.因此,我创建了所有必要的类,如 CustomPropertySource
、CustomPropertySourceLocator
、CustomConfigBootstrapConfiguration
等,并在 spring.factories代码>.
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
.
最后,我在 spring-cloud-config-server
中为 spring-cloud-config-custom
添加了 maven 依赖.
At the end, I've added maven dependency to spring-cloud-config-custom
inside my spring-cloud-config-server
.
到目前为止一切顺利.一切正常.当我启动服务器时,我可以看到我的 CustomPropertySource
位于注入 EnvironmentController
的 EnviromentRepository
bean 内的 propertySources 列表中.
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
.
问题: 当我向 @RequestMapping("/{name}/{profiles}/{label:.*}")
发送 GET 请求时(在 EnvironmentController
),注入的 EnviromentRepository
bean 用于查找请求的属性源(repository.findOne(name, profiles, label)
方法).不幸的是,在这里找不到我的财产来源.为什么?
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?
我花了很多时间调试这个.我发现存储库将 findOne()
方法调用委托给其他存储库:MultipleJGitEnvironmentRepository
将它委托给 NativeEnvironmentRepository
.在这个委托中, findOne() 方法不使用来自 EnviromentRepository
主注入到控制器的 propertySources.它使用新的 PropertySource 列表和新的单独 SpringApplication 创建新的环境存储库.最后,此列表不包含我的 CustomPropertySource
,这就是 findOne()
在结果 Environment
对象中返回空 propertySources 的原因.
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.
- 我做错了什么吗?
CustomPropertySourceLocator
(和/或ConsulPropertySourceLocator
)是否应该在spring-cloud-config-server
或spring-cloud-config-client
- spring-cloud-config-server 能否通过 REST 接口同时提供多种不同类型的
PropertySources
(说不同"是指所有 Git、Consul 和 Zookeeper)?
- 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)?
推荐答案
您所做的是将属性源添加到配置服务器本身,而不是它所服务的配置.将 spring-boot-starter-actuator
添加到您的配置服务器并查看 /env
显示:
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"
},
}
要添加将由配置服务器提供的内容,您必须实现一个 EnvironmentRepository
.
To add something that will be served by config server, you have to implement an EnvironmentRepository
.
对复合EnvironmentRepository
的支持是最近添加.
这篇关于Spring Cloud 配置服务器 - 如何在 EnvironmentEncryptorEnvironmentRepository 的 findOne() 方法中添加可见的自定义 PropertySource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!