当前位置: 首页 > 知识库问答 >
问题:

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

丁晋
2023-03-14

我有一个从Spring Cloud config server(纯模式)获取配置的Spring Boot应用程序。配置服务器加载的配置位置中的基本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)的endpoint时,我会返回在配置文件“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中使用值时,为什么我不会从“测试”配置文件中获得值?

我的意图是在顶部添加“default”值,并让“profile”覆盖任何非标准的默认值。

更新: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个答案

鲜于峰
2023-03-14

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

 类似资料:
  • 我刚刚将我们的Spring Boot项目从引导升级到v2.6.2,从Spring Cloud升级到2021.0.0。 现在,我的远程配置获取没有任何效果,应用程序也无法获取正确的属性文件 [main]INFO o. s. c. c. c. ConfigServiceProperty tySourceLocator-从服务器获取配置:http://localhost:8080 [main]WARN

  • 我不确定我是否很好地理解了Spring概要文件是如何处理yaml和属性文件的。我试图将这两种类型的配置分开(这两个文件不共享任何配置),但在从yaml配置中读取配置文件时遇到了问题。 我的问题是,当我试图(通过xml)配置我的数据源时,这样做: Spring总是使用YAML文件中的最后一个配置,忽略概要文件。我试图通过web.xml中的contex-parameter或直接将活动概要文件传递给JV

  • 问题内容: 在我的application.yml中,我得到了: 还有其他一些使用不同的配置文件。启动应用程序时,我得到以下信息: 如果我只是将log4j2.xml放在已分析的文件旁边,则它可以工作。所以我认为这是我错过依赖关系的原因,或者使用log4j2无法实现? 问题答案: 在我这边,我正在使用属性文件而不是Yaml文件。我需要两个日志文件:一个将所有内容记录到控制台,另一个用于记录文件。因此,

  • 我有这个配置文件: 当我尝试检查数据时,它无论如何都连接到“jdbc: h2: mem: testdb”: 为什么Spring启动找不到正确的数据库配置?

  • 我正在尝试这样做: https://developers.google.com/identity/sign-in/web/backend-auth#calling-the-tokeninfo-endpoint 我用CLIENT_ID复制了粘贴的Java代码,但是除了用户标识、电子邮件和电子邮件验证之外,我无法获得任何更多的信息。idTokenString验证确定。还有其他人可以这样做吗? 我要求这