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)信息:

1spring.cloud.config.uri=http://localhost:8080/ 2spring.cloud.config.discovery.enabled=true 3 4spring.cloud.config.profile=test 5spring.cloud.config.label=master

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

1spring.cloud.config.discovery.serviceId=hsdConfigServer 2spring.cloud.config.discovery.enabled=true 3 4spring.cloud.config.profile=test 5spring.cloud.config.label=master

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

12017-08-30 09:21:07.546 INFO 32460 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://localhost:8888 22017-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 32017-08-30 09:21:08.727 INFO 32460 --- [ main] com.hsd.HSDConfigClientSample : No active profile set, falling back to default profiles: default 42017-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 52017-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]] 62017-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'. 72017-08-30 09:21:10.160 INFO 32460 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=d344f478-556d-3c71-aaed-5a4a80541b01 82017-08-30 09:21:10.174 INFO 32460 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring 92017-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) 102017-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) 112017-08-30 09:21:11.131 INFO 32460 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8090 (http) 122017-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问题解决,获取正确的相应配置。

1The Bootstrap Application Context 2A 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. 3 4The 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: 5 6bootstrap.yml 7spring: 8 application: 9 name: foo 10 cloud: 11 config: 12 uri: ${SPRING_CONFIG_URI:http://localhost:8888} 13It 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. 14 15You 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 中。

1Bootstrap.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. 2 3For 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]).
点赞
收藏

评论区

加载中...

相关推荐

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

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )