4

我使用 Spring Boot 开发了一个微服务。该服务正在使用 Spring 云配置服务器获取属性。此微服务在标头中接受版本,并根据版本执行相应的功能。在我的 github repo 中,我有 2 个分支,每个版本 1 个。该服务通常将以下信息发送到配置服务器以获取属性 -

应用程序名称 + 配置文件 + 标签

有没有办法在我的 .yml 文件中使用占位符代替标签?如果我在标题中看到 v1,我希望标签动态设置为 v1,否则为 v2。

编辑:

我在本文档(http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html)的“Git URI 中的占位符”部分看到了对占位符的引用,但是我不确定值如何从传入的请求中动态替换

4

1 回答 1

4

spring-cloud-config-server 提供了几个可用的 REST API,允许直接查询属性文件:

$ hostname:port/{label}/{name}-{profiles}.properties]

您可以动态使用您选择的任何标签,只要它与 git 上的现有标签匹配。

例如,要检索,在 gitapplication.properties中标记:v1

 $ http://${hostname}:${port}/v1/application.properties

配置服务器 REST API:

  • /{name}/{profiles}/{label:.*
  • /{label}/{name}-{profiles}.properties
  • /{name}-{profiles}.json
  • /{label}/{name}-{profiles}.json
  • /{label}/{name}-{profiles}.yml
  • /{label}/{name}-{profiles}.yaml
  • /{name}-{profiles}.yml
  • /{name}-{profiles}.yaml
  • /{名称}/{个人资料:。[^-]。}
  • /{name}-{profiles}.properties
  • /{name}/{profile}/{label}/**

我在 git 上尝试了一个spring-cloud-server带有属性文件的示例项目。我为每个标签应用了 git 标签v1v2在文件中使用了不同的值(我使用了 profile remote):

标签 v1

http://localhost:8888/v1/application-remote.properties
> testproperty: remotevalue-v1

标签 v2

http://localhost:8888/v2/application-remote.properties
> testproperty: remotevalue-v2

没有标签

http://localhost:8888/application-remote.properties
> testproperty: remotevalue-master

Java 代码

我没有尝试过,但我想您也可以使用 cloud-config-server 的 java API(直接注入和调用控制器,而不是执行 http 请求):

@Autowired
EnvironmentController environmentController;
...

Environment labelled = environmentController.labelled("application", "remote", "v1");
Map<?, ?> keyValues = labelled.getPropertySources().get(0).getSource();
于 2016-09-10T21:46:56.267 回答