Springcloud中配置文件名 bootstrap.yml 和 application.yml 区别


声明:本文转载自https://my.oschina.net/neverforget/blog/1525947,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

问题

近期,因项目需求,需将统一配置中心由原来的disconf升级至spring cloud config + git. 话说disconf自15年我们引入后,两年的使用,在多环境下,新增和变更一套环境,或是配置项时,disconf更新的繁琐操作及不友好的UI交互体验,谁用谁知道。而且disconf版本化支持的不友好,没有git的时序版本及历史记录。

在使用spring cloud config + git 的 config server 由原来的单节点升级为多节点,并在eureka上注册时。相应配置变更如下:

原单节点的 app(config client)对应配置(application.properties)信息:

spring.cloud.config.uri=http://localhost:8080/ spring.cloud.config.discovery.enabled=true  spring.cloud.config.profile=test spring.cloud.config.label=master 

基于eureka多节点负载的 app(config client)对应配置(application.properties)信息

spring.cloud.config.discovery.serviceId=hsdConfigServer spring.cloud.config.discovery.enabled=true  spring.cloud.config.profile=test spring.cloud.config.label=master 

变更后,app(config client) 无法启动,启动日志如下:

2017-08-30 09:21:07.546  INFO 32460 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://localhost:8888 2017-08-30 09:21:08.726  WARN 32460 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/hsdConfigClientSample/test/master": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect 2017-08-30 09:21:08.727  INFO 32460 --- [           main] com.hsd.HSDConfigClientSample            : No active profile set, falling back to default profiles: default 2017-08-30 09:21:08.742  INFO 32460 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5ddcc487: startup date [Wed Aug 30 09:21:08 CST 2017]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@1184ab05 2017-08-30 09:21:09.638  INFO 32460 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'hystrixFeature' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration$HystrixWebConfiguration; factoryMethodName=hystrixFeature; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/cloud/netflix/hystrix/HystrixCircuitBreakerConfiguration$HystrixWebConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration; factoryMethodName=hystrixFeature; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/cloud/netflix/hystrix/HystrixCircuitBreakerConfiguration.class]] 2017-08-30 09:21:09.968  WARN 32460 --- [           main] o.s.c.a.ConfigurationClassPostProcessor  : Cannot enhance @Configuration bean definition 'refreshScope' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'. 2017-08-30 09:21:10.160  INFO 32460 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=d344f478-556d-3c71-aaed-5a4a80541b01 2017-08-30 09:21:10.174  INFO 32460 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring 2017-08-30 09:21:10.537  INFO 32460 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [class org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$5236f8c3] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2017-08-30 09:21:10.553  INFO 32460 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.RefreshEndpointAutoConfiguration' of type [class org.springframework.cloud.autoconfigure.RefreshEndpointAutoConfiguration$$EnhancerBySpringCGLIB$$fb95fab9] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2017-08-30 09:21:11.131  INFO 32460 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8090 (http) 2017-08-30 09:21:11.148  INFO 32460 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat 

日志中,config client 为什么会去请求 http://localhost:8888,而不是我们配置的 spring.cloud.config.discovery.serviceId=hsdConfigServer,跟踪spring cloud config client源码类org.springframework.cloud.config.client.ConfigClientProperties.class,发现这是它的默认配置项。意谓着在application.properties中的discovery.serviceId=hsdConfigServer没生效。

2017-08-30 09:21:07.546  INFO 32460 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://localhost:8888 

关于 bootstrap vs application 配置文件

苦思后,在spring官网找到答案,将spring config client的相关配置从application.properties移运bootstrap.properties问题解决,获取正确的相应配置。

The Bootstrap Application Context A Spring Cloud application operates by creating a "bootstrap" context, which is a parent context for the main application. Out of the box it is responsible for loading configuration properties from the external sources, and also decrypting properties in the local external configuration files. The two contexts share an Environment which is the source of external properties for any Spring application. Bootstrap properties are added with high precedence, so they cannot be overridden by local configuration.  The bootstrap context uses a different convention for locating external configuration than the main application context, so instead of application.yml (or .properties) you use bootstrap.yml, keeping the external configuration for bootstrap and main context nicely separate. Example:  bootstrap.yml spring:   application:     name: foo   cloud:     config:       uri: ${SPRING_CONFIG_URI:http://localhost:8888} It is a good idea to set the spring.application.name (in bootstrap.yml or application.yml) if your application needs any application-specific configuration from the server.  You can disable the bootstrap process completely by setting spring.cloud.bootstrap.enabled=false (e.g. in System properties). 

Bootstrap.yml (bootstrap.properties) 会先于 application.yml (application.properties) 被加载, 特别是在使用 Spring Cloud Config Server 时,建议相关配置,如spring.application.name, spring.cloud.config.server.git.uri 应该配置在文件 bootstrap.yml 中。

Bootstrap.yml (bootstrap.properties) is loaded before application.yml (application.properties),it's like application.yml but for the bootstrap phase of an application context. It is typically used for the case "when using Spring Cloud Config Server, you should specify spring.application.name and spring.cloud.config.server.git.uri inside bootstrap.yml", and also 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.  For example, when using Spring Cloud, the 'real' configuration data is usually loaded from a server. In order to get the URL (and other connection configuration, such as passwords, etc.), you need an earlier or "bootstrap" configuration. Thus, you put the config server attributes in the bootstrap.yml, which is used to load the real configuration data (which generally overrides what's in an application.yml [if present]). 

本文发表于2017年08月30日 12:36
(c)注:本文转载自https://my.oschina.net/neverforget/blog/1525947,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 2361 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1