java - 从 Spring Cloud Config Server 获取配置时 Spring 配置文件排序不正确

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

我有一个 Spring Boot 应用程序从 Spring Cloud 配置服务器( native 模式)获取配置。配置服务器加载的配置位置中的基本 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"

最佳答案

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

关于java - 从 Spring Cloud Config Server 获取配置时 Spring 配置文件排序不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46673510/

相关文章:

java - 在网络服务中订购

java - Spring Boot Camel - Camel 组件中的 Autowiring 问题

java - 在 Spring Controller 中使用 Callable 和 WebApplicationInitializer 获取 IllegalStateException

java - 在 Spring boot 应用程序中传递常量消息的正确方法(messages.properties 或 Constant 类)

java - 如何自定义预定义的 Camel 组件?

java - Spring 集成 : how to delete a file?

Java日历日期添加和计算

java - Spring Boot Actuator 添加 X-Frame-Options = DENY 到所有端点(特别是错误端点)

java - Maven 从曾祖 parent 而不是我们的 parent 依赖管理中采购依赖版本

java - TimeSeriesChart:某个时间戳的值