【问题标题】:Placeholders resolution for client specific configuration客户端特定配置的占位符解析
【发布时间】:2017-03-12 15:37:11
【问题描述】:

我正在学习Spring Cloud Config 项目功能。我想知道是否可以使用占位符进行特定于应用程序的配置?

例如:

我的 application.yml 文件:

server:
  port: 8888

spring:
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/config

config 文件夹包含一个 my-app.yml 文件:

key:
  value: ${my.password}

服务器以 -Dmy.password=password 环境变量启动。对 /my-app/native url 的获取请求返回:

{
    "name": "my-app",
    "profiles": ["native"],
    "label": null,
    "version": null,
    "state": null,
    "propertySources": [{
        "name": "classpath:/config/my-app.yml",
        "source": {
            "key.value": "${my.password}"
        }
    }]
}

placeholder 中的 env 属性没有评估就返回给客户端,有没有办法在向客户端发送响应之前评估该属性?

【问题讨论】:

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


    【解决方案1】:

    您可以在启动服务器时使用override 功能设置key.value 的值:

    -Dspring.cloud.config.server.overrides.key.value=overrideValue
    

    认为您的客户必须在之后启动。

    Spring documentation:

    属性覆盖

    配置服务器具有“覆盖”功能,可让操作员向所有应用程序提供配置属性。使用普通 Spring Boot 挂钩的应用程序不会意外更改覆盖的属性。要声明覆盖,请将名称-值对的映射添加到 spring.cloud.config.server.overrides,如下例所示:

    spring:   
      cloud:
        config:
          server:
            overrides:
              foo: bar 
    

    前面的示例导致所有作为配置客户端的应用程序读取foo=bar,独立于它们自己的配置。

    ...

    【讨论】:

    • 感谢您的回答,这部分解决了我的问题。例如,如果配置服务器为不同的应用程序提供不同的值(相同的键)。一个值取自环境,而另一个值硬编码在配置文件中。有什么办法可以控制吗?
    • 我不认为这是可能的(至少我不知道)。您的密码经常更改吗?为什么不在服务器 GIT 中使用 spring boot cloud {cipher}。您每个客户端都有一个 {cipher}(按 applicationName 或不同标签组织)?您将拥有加密密码的好处。这符合您的需要吗?如果您不了解cyper,请告诉我,我会更新答案
    • 我对密码功能很熟悉。问题是密码可能会在安装过程中根据要求更改。
    • 那么,我不知道还能做什么(不是说没有,只是我不知道)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    • 2014-03-23
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多