java 应用程序执行时不使用 application.yml 中 spring.cloud.config 的设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37074642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Settings in application.yml for spring.cloud.config aren't used when app is executing
提问by slippery
I have a problem with spring cloud: my settings in application.yml for spring.cloud.config aren't used when app is executing. let me put more detail here. I'd like to my services could get settings from a remote ConfigServer. I've created the ConfigServer as a spring boot app with annotation @EnableConfigServer. After that i've created client app with next config file:
我对 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
main class:
主类:
@EnableEurekaClient
@SpringBootApplication
public class MwApplication
and extra configuration into app:
并在应用程序中进行额外配置:
@Configuration
@EnableJpaRepositories(basePackages = {"com.sample.repository"})
@EnableTransactionManagement
@EnableScheduling
public class AppConfiguration
also i have next dependencies:
我还有下一个依赖项:
spring-cloud-starter-eureka
spring-cloud-config-client
spring-boot-configuration-processor
spring-boot-starter-data-jpa
When i execute my client app, i've got this message: ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/mw/default"
当我执行我的客户端应用程序时,我收到以下消息:ConfigServicePropertySourceLocator : 无法定位 PropertySource: I/O error on GET request for " http://localhost:8888/mw/default"
The app try to get data from default uri(localhost) instead of to use uri from my setting. I've looked at app in debug mode and saw org.springframework.cloud.config.client.ConfigServiceBootstrapConfiguration was creating ConfigClientProperties with default property and my settings from application.yml weren't used.
该应用程序尝试从默认 uri(localhost) 获取数据,而不是从我的设置中使用 uri。我在调试模式下查看了应用程序,看到 org.springframework.cloud.config.client.ConfigServiceBootstrapConfiguration 正在创建具有默认属性的 ConfigClientProperties 并且我没有使用 application.yml 中的设置。
What am i doing wrong? thanks.
我究竟做错了什么?谢谢。
采纳答案by Andonaeus
You need to add the following to your application.yml file:
您需要将以下内容添加到 application.yml 文件中:
spring:
cloud:
config:
enabled: true
Per comment chain, you also need to add the properties to bootstrap.yml instead of application.yml . The reason is that the former is loaded before the latter in the spring startup cycle. Here is another SO post answered by user Michael Isvy explaining why, and copied below for posterity: What is the diference between putting a property on application.yml or bootstrap.yml in spring boot?
每个注释链,您还需要将属性添加到 bootstrap.yml 而不是 application.yml 。原因是前者在spring启动循环中先于后者加载。这是用户 Michael Isvy 回答的另一篇 SO 帖子,解释了原因,并在下面复制以供后代使用:spring boot 中将属性放在 application.yml 或 bootstrap.yml 上有什么区别?
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.
我刚刚问了
Spring Cloud
这些人,并认为我应该分享我在这里的信息。bootstrap.yml 在 application.yml 之前加载。
它通常用于以下情况:
- 当使用 Spring Cloud Config Server 时,你应该指定
spring.application.name
andspring.cloud.config.server.git.uri
insidebootstrap.yml
- 一些
encryption/decryption
信息从技术上讲,
bootstrap.yml
由父 Spring ApplicationContext 加载。该父 ApplicationContext 在使用 application.yml 的那个之前加载。