我在使用 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
andspring.cloud.config.server.git.uri
insidebootstrap.yml
- some
encryption/decryption
informationTechnically,
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/