3
  1. Java版本:8
  2. Spring Boot 版本:2.4.1
  3. Spring Cloud 版本:2020.0.0,具体来说我使用了一个连接到 GIT 的 Spring Cloud Config Server,我们的服务是 Spring Cloud Config Clients。

我已经不再使用bootstrap.yml并开始使用spring.config.importspring.config.activate.on-profile如文档herehere中所述

我的服务中的配置(配置服务器的客户端)如下所示:

server.port: 9001
spring:
  application.name: my-rest-service
  config.import: configserver:http://localhost:8888
  cloud.config.profile: ${spring.profiles.active}

我在配置服务器中的配置如下所示:

application.yml(有两个文件由 --- 分隔)

logging:
  file.name: <omitted>
  level:
    root: INFO
---
spring:
  config.activate.on-profile: dev
  logging.level.root: DEBUG

my-rest-sercive.yml(有两个文件由 --- 分隔)

spring:
  datasource:
    driver-class-name: <omitted>
    username: <omitted>
    password: <omitted>
---
spring:
  config.activate.on-profile: dev
  datasource.url: <omitted>

因为有一个配置文件“dev”处于活动状态,所以我成功地从配置服务器获取了以下 4 个配置:

  • application.yml:一般日志记录级别
  • application.yml: dev 的特定日志记录
  • my-rest-sercive.yml: 通用数据源属性
  • my-rest-sercive.yml: dev 的特定数据源 url

当我使用浏览器或调试时,或者当我降低日志级别进行跟踪时,我可以看到这 4 个源被成功获取:

o.s.b.c.config.ConfigDataEnvironment     : Adding imported property source 'configserver:https://git.company.com/path.git/file:C:\configservergit\config\my-rest-service.yml'
o.s.b.c.config.ConfigDataEnvironment     : Adding imported property source 'configserver:https://git.company.com/path.git/file:C:\configservergit\config\my-rest-service.yml'
o.s.b.c.config.ConfigDataEnvironment     : Adding imported property source 'configserver:https://git.company.com/path.git/file:C:\configservergit\config\application.yml'
o.s.b.c.config.ConfigDataEnvironment     : Adding imported property source 'configserver:https://git.company.com/path.git/file:C:\configservergit\config\application.yml'

但是,请注意,因为我使用多文档 yml 文件,所以在这 4 个属性源中,仅使用了两个唯一名称。

在稍后的步骤中,当 Spring 创建数据源 bean 时,他抱怨找不到数据源 URL。如果我调试 spring bean 工厂,我确实可以看到配置服务器返回的 4 个属性文件中,只剩下两个(不包含 dev profile 特定配置的那些)。我认为这是因为它们具有相同的名称并且它们相互覆盖。这是这段代码的效果MutablePropertySource.class

public void addLast(PropertySource<?> propertySource) {
    synchronized(this.propertySourceList) {
        this.removeIfPresent(propertySource); <-- this is the culrprit!
        this.propertySourceList.add(propertySource);
    }
} 

这是 Spring 2.3/Spring Cloud Hoxton 的重大变化,它正确收集了所有属性。我认为 Spring Cloud 需要更改配置服务器,以便 yml 中的每个文档在返回 Spring 时都有一个唯一的名称。这正是 Spring Boot 通过将字符串附加(documenyt #1)到属性源名称来处理多文档 yml 文件的方式

我发现了一个关于配置文件和多文档 yml 的有趣注释,基本上说它不受支持,但这不适用于我的用例,因为我的 yml 文件不是基于配置文件的( -{profileName}文件名的最后部分没有) .

4

1 回答 1

0

这是新版本的一个已知问题。我们可以在 spring cloud config server github page上跟踪问题。

解决方法似乎是停止使用多文档 yml 文件并使用文件名中包含配置文件名称的多个不同文件。

于 2021-01-14T12:59:57.873 回答