从Spring Cloud Config Server获取配置时,Spring配置文件未正确排序

时间:2017-10-10 18:27:01

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

我有一个Spring Boot应用程序从Spring Cloud配置服务器(本机模式)获取配置。配置服务器加载的配置位置中的基本application.yml文件包含以下内容:

eureka:
  client:
    service-url:
      defaultZone: ${BOOT_EUREKA_LOCATIONS:http://instance1.localhost:7761/eureka,http://instance2.localhost:7762/eureka,http://instance3.localhost:7763/eureka}
  register-with-eureka: true
---
spring:
  profiles: test
eureka:
  client:
    register-with-eureka: false #no registration on Eureka when testing
    service-url:
      defaultZone: ${BOOT_EUREKA_LOCATIONS:http://sparky:8761/eureka}

当点击配置服务器(http://mygateway/config-server/myapp/test)的端点时,我会回复以下内容:" myapp"应用程序在配置文件中运行" test":

{
"name": "myapp",
"profiles": [
    "test"
],
"label": null,
"version": null,
"state": null,
"propertySources": [
    {
        "name": "file:////wherever/application.yml#test",
        "source": {
            "spring.profiles": "test",
            "eureka.client.register-with-eureka": false,
            "eureka.client.service-url.defaultZone": "${BOOT_EUREKA_LOCATIONS:http://sparky:8761/eureka}"
        }
    },
    {
        "name": "file:////whereever/application.yml",
        "source": {
            "eureka.client.service-url.defaultZone": "${BOOT_EUREKA_LOCATIONS:http://instance1.localhost:7761/eureka,http://instance2.localhost:7762/eureka,http://instance3.localhost:7763/eureka}"

在测试配置文件中运行myApp时,eureka.client.service-url.defaultZone的值为http://instance1.localhost:7761/eureka,http://instance2.localhost:7762/eureka,http://instance3.localhost:7763/eureka,这是意外的。

我原本希望测试配置文件中的条目覆盖它(就像你在本地有application.yml一样)。关于为什么我不能从"测试"获得价值的想法?使用myApp中的值时的配置文件?

我的意图是添加"默认"值位于顶部,并具有"配置文件"覆盖任何非标准默认值。

更新: myApp / env不会显示" application.yml #test"加载,但它显示测试配置文件,但只显示从配置服务器(而不是#test)返回的默认值:

{
  "profiles": [
    "test"
  ],
  "server.ports": {
    "local.server.port": 7761
  },
  "configService:file:////wherever/application.yml": {
    "eureka.client.service-url.defaultZone": "http://instance1.localhost:7761/eureka,http://instance2.localhost:7762/eureka,http://instance3.localhost:7763/eureka"

1 个答案:

答案 0 :(得分:1)

这是一个完整的用户错误。我正在设置默认"测试" application.yml中的活动配置文件,而不是作为环境变量或bootstrap.yml传入,因此它很快就没有加载配置服务器命中。

相关问题