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

sli*_*ery 4 java spring spring-boot microservices spring-cloud

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

    application:
      name: mw
    cloud:
      config:
        enabled: true
        uri: http://172.17.42.1:8888
        fail-fast: true
Run Code Online (Sandbox Code Playgroud)

主类:

    @EnableEurekaClient
    @SpringBootApplication
    public class MwApplication
Run Code Online (Sandbox Code Playgroud)

并在应用程序中进行额外配置:

    @Configuration
    @EnableJpaRepositories(basePackages = {"com.sample.repository"})
    @EnableTransactionManagement
    @EnableScheduling
    public class AppConfiguration
Run Code Online (Sandbox Code Playgroud)

我还有下一个依赖项:

    spring-cloud-starter-eureka
    spring-cloud-config-client
    spring-boot-configuration-processor
    spring-boot-starter-data-jpa
Run Code Online (Sandbox Code Playgroud)

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

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

我究竟做错了什么?谢谢。

And*_*eus 5

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

spring:
    cloud:
        config:
            enabled: true
Run Code Online (Sandbox Code Playgroud)

对于每个注释链,还需要将属性添加到bootstrap.yml而不是application.yml。原因是在春季启动周期中,前者在后者之前加载。这是用户Michael Isvy回答的另一篇SO帖子,解释了原因,以下为后代而复制:在Spring Boot中将属性放在application.yml或bootstrap.yml上有什么区别

我刚刚问过Spring Cloud这些家伙,以为我应该在这里分享我的信息。

bootstrap.yml在application.yml之前加载。

通常用于以下用途:

  • 使用Spring云配置服务器时,您应指定spring.application.namespring.cloud.config.server.git.uribootstrap.yml
  • 一些encryption/decryption信息

从技术上讲,bootstrap.yml是由父Spring ApplicationContext加载的。该父ApplicationContext在使用application.yml的那个之前加载。

  • 我已经尝试过并再次添加,但没有任何改变,我仍然遇到这个问题 (2认同)
  • 尝试将application.yml配置移动到bootstrap.yml (2认同)