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

问题

近期,因项目需求,需将统一配置中心由原来的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]).

转载于:https://my.oschina.net/neverforget/blog/1525947

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值