【问题标题】:Spring profiles not ordering correctly when obtaining config from Spring Cloud Config Server从 Spring Cloud Config Server 获取配置时,Spring 配置文件未正确排序
【发布时间】:2018-03-22 06:15:09
【问题描述】:

我有一个 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) 的端点时,我会返回在配置文件“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}"

在测试配置文件中运行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"

【问题讨论】:

  • myApp /env 说什么?
  • 添加了附加信息以回答您上面的问题。很好的问题,可能会导致答案。

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


【解决方案1】:

这是一个完全的用户错误。我在 application.yml 中设置了默认的“测试”活动配置文件,而不是作为环境变量或 bootstrap.yml 传递,因此它没有足够快地加载到配置服务器命中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-03
    • 2017-10-12
    • 2017-02-23
    • 2015-08-25
    • 2016-02-27
    • 2022-12-04
    • 2017-06-23
    • 2016-07-06
    相关资源
    最近更新 更多