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

标签 java spring spring-boot spring-cloud microservices

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

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

主类:

    @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:“http://localhost:8888/mw/default”的 GET 请求出现 I/O 错误

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

我做错了什么? 谢谢。

最佳答案

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

spring:
    cloud:
        config:
            enabled: true

每个评论链,您还需要将属性添加到 bootstrap.yml 而不是 application.yml 。原因是前者在spring启动周期中先于后者加载。这是用户 Michael Isvy 回答的另一篇 SO 帖子,解释了原因,并复制如下以供后代使用:What is the diference between putting a property on application.yml or bootstrap.yml in spring boot?

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.

关于java - 应用程序执行时不使用 spring.cloud.config 的 application.yml 中的设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37074642/

相关文章:

Java拼图: modifiers

java - 使用字符串数组来标记每个条目的字母

java - 使用 Maven 生成生产就绪输出

spring - Spring App 的自定义 tomcat context.xml

spring-boot - 如何以编程方式从 spring-boot-actuator 获取指标?

java - 如果在 SP 中使用临时表,则 JDBC 可调用语句返回 null 结果集

java - Twitter 中的 Get.text()

spring - 在Grails中加载bean时发生ClassNotFound异常

spring-boot - Swagger 的HK2服务修改失败

spring-boot - Zuul -> Eureka 服务器,基本认证问题