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

Easter79
• 阅读 853

问题

近期,因项目需求,需将统一配置中心由原来的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]).
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k