【问题标题】:Spring Cloud Config Server - Placeholder LabelSpring Cloud Config Server - 占位符标签
【发布时间】:2016-10-22 10:43:50
【问题描述】:

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

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

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

编辑:

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

【问题讨论】:

    标签: spring spring-boot spring-cloud cloud-foundry spring-cloud-config


    【解决方案1】:

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

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

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

    例如,要在 git 中检索 application.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:.[^-].}
    • /{name}-{profiles}.properties
    • /{name}/{profile}/{label}/**

    我尝试了一个示例 spring-cloud-server 项目,其中包含 git 上的属性文件。我在文件中为每个标签应用了具有不同值的 git 标签v1v2(我使用了配置文件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();
    

    【讨论】:

      猜你喜欢
      • 2019-01-12
      • 2017-05-01
      • 2020-10-05
      • 2017-08-17
      • 2018-08-14
      • 2016-09-26
      • 1970-01-01
      • 2021-02-06
      • 2016-07-09
      相关资源
      最近更新 更多