简体   繁体   English

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

[英]Spring profiles not ordering correctly when obtaining config from Spring Cloud Config Server

I have a Spring Boot application obtaining configuration from Spring Cloud config server (native mode). 我有一个Spring Boot应用程序,它从Spring Cloud配置服务器(本机模式)获取配置。 The base application.yml file in the config location loaded by the config server contains the following: 配置服务器加载的配置位置中的基本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}

When hitting the endpoint for config server ( http://mygateway/config-server/myapp/test ), I get back the following for the "myapp" application running in profile "test": 当击中配置服务器的端点( http:// mygateway / config-server / myapp / test )时,我将在配置文件“ test”中运行的“ myapp”应用程序返回以下信息:

{
"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}"

When running myApp in the test profile, the value of eureka.client.service-url.defaultZone is http://instance1.localhost:7761/eureka,http://instance2.localhost:7762/eureka,http://instance3.localhost:7763/eureka , which is unexpected. 在测试配置文件中运行myApp时,eureka.client.service-url.defaultZone的值为http://instance1.localhost:7761 / eureka,http://instance2.localhost:7762 / eureka,http:// instance3 .localhost:7763 / eureka ,这是意外的。

I would have expected the entry in the test profile to override it (as it does if you have the application.yml locally). 我希望测试配置文件中的条目将其覆盖(如果您在本地拥有application.yml,它也会这样做)。 Thoughts on why I wouldn't be getting the value from the "test" profile when using the value in myApp? 关于为什么在myApp中使用值时为什么不能从“测试”配置文件中获取值的想法?

My intent would be to add the "default" values at the top, and have the "profile" override any non-standard defaults. 我的意图是在顶部添加“默认”值,并让“配置文件”覆盖所有非标准默认值。

Update: myApp/env does NOT show the "application.yml#test" loaded, however it shows the test profile, but only the default values returned back from the config server (not the #test ones): 更新: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"

It was a complete user error. 这是一个完整的用户错误。 I was setting the default "test" active profile in the application.yml instead of passing in as an environment variable or bootstrap.yml, therefore it wasn't loaded soon enough for the config server hit. 我在application.yml中设置了默认的“测试”活动配置文件,而不是将其作为环境变量或bootstrap.yml传入,因此尚未足够快地加载该配置服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM