application.yml 中 spring.cloud.config 的设置在应用程序执行时未被使用

Settings in application.yml for spring.cloud.config aren't used when app is executing

我在使用 spring 云时遇到问题:我在 application.yml 中对 spring.cloud.config 的设置在应用程序执行时未被使用。让我在这里详细说明一下。 我希望我的服务可以从远程 ConfigServer 获取设置。我已将 ConfigServer 创建为带有注释 @EnableConfigServer 的 spring 启动应用程序。 之后我用下一个配置文件创建了客户端应用程序:

    application:
      name: mw
    cloud:
      config:
        enabled: true
        uri: http://172.17.42.1:8888
        fail-fast: true

主要class:

    @EnableEurekaClient
    @SpringBootApplication
    public class MwApplication

和应用程序的额外配置:

    @Configuration
    @EnableJpaRepositories(basePackages = {"com.sample.repository"})
    @EnableTransactionManagement
    @EnableScheduling
    public class AppConfiguration

我还有下一个依赖项:

    spring-cloud-starter-eureka
    spring-cloud-config-client
    spring-boot-configuration-processor
    spring-boot-starter-data-jpa

当我执行我的客户端应用程序时,我收到此消息:ConfigServicePropertySourceLocator:无法找到 PropertySource:I/O 对“http://localhost:8888/mw/default”的 GET 请求出错

该应用尝试从默认 uri(localhost) 获取数据,而不是使用我的设置中的 uri。我在调试模式下查看了应用程序,发现 org.springframework.cloud.config.client.ConfigServiceBootstrapConfiguration 正在使用默认值 属性 创建 ConfigClientProperties,而我在 application.yml 中的设置未被使用。

我做错了什么? 谢谢。

您需要将以下内容添加到您的 application.yml 文件中:

spring:
    cloud:
        config:
            enabled: true

每个评论链,您还需要将属性添加到 bootstrap.yml 而不是 application.yml 。原因是在spring启动周期中前者先于后者加载。这是用户 Michael Isvy 回答的另一个 SO post 解释了原因,并复制如下 posterity:

I have just asked the Spring Cloud guys and thought I should share the info I have here.

bootstrap.yml is loaded before application.yml.

It is typically used for the following:

  • when using Spring Cloud Config Server, you should specify spring.application.name and spring.cloud.config.server.git.uri inside bootstrap.yml
  • some encryption/decryption information

Technically, bootstrap.yml is loaded by a parent Spring ApplicationContext. That parent ApplicationContext is loaded before the one that uses application.yml.