文档章节

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

neverforget
 neverforget
发布于 2017/08/30 10:03
字数 1071
阅读 2463
收藏 29

问题

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

© 著作权归作者所有

neverforget
粉丝 3
博文 11
码字总数 8969
作品 0
深圳
私信 提问
SpringCloud实战7-Config分布式配置管理

分布式环境下的统一配置框架,已经有不少了,比如百度的disconf,阿里的diamand 官方文档对spring Cloud Config的描述如下:   Spring Cloud Config为分布式系统中的外部配置提供服务器和客...

狂小白
2018/05/21
0
0
spring cloud config配置中心

研究spring cloud自然少不了配置中心,项目中使用的是apollo,想研究下spring cloud config基于git的。 1、首先在git上面创建你的配置文件信息,我用自己搭建的gitlab来搞下,创建了两个配置...

朝如青丝暮成雪
06/17
9
0
SpringCloud实战9-Stream消息驱动

官方定义 Spring Cloud Stream 是一个构建消息驱动微服务的框架。   应用程序通过 inputs 或者 outputs 来与 Spring Cloud Stream 中binder 交互,通过我们配置来 binding ,而 Spring Clo...

狂小白
2018/05/29
0
0
idea springboot项目配置文件 绿叶子

File->Project Structure..->Facets,然后你会看到Spring (你的项目名称),然后选择你的项目,右边出现springboot和springcloud的两项东西,因为我添加有springcloud的引用,不添加不知道有没...

朝如青丝暮成雪
04/09
59
0
SpringCloud学习系列之四-----配置中心(Config)使用详解

前言 本篇主要介绍的是SpringCloud中的分布式配置中心(SpringCloud Config)的相关使用教程。 SpringCloud Config Config 介绍 Spring Cloud Config项目是一个解决分布式系统的配置管理方案。...

虚无境
06/01
157
0

没有更多内容

加载失败,请刷新页面

加载更多

关于运维,该怎么决定它的方向,这个似工作又似兴趣的存在

我之前主要从事网络、桌面、机房管理等相关工作,这些工作使我迷惘,这应该是大多数运维人都经历过的过程; 18年国庆,我从国内前三的消费金融公司裸辞,下海创业,就是想要摆脱这样的困境。...

网络小虾米
14分钟前
3
0
Java Timer的用法

Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { System.out.println("11232"); } }, 200000 , 1000); public void schedule(TimerTask task, long delay......

林词
17分钟前
3
0
使用js动态加载外部js文件以及动态创建script脚本

动态脚本指的是在页面加载时不存在,但将来的某一时刻通过修改该DOM动态添加的脚本。和操作HTML元素一样,创建动态脚本也有两种方式:插入外部文件和直接插入JavaScript代码。 动态加载外的外...

Bing309
25分钟前
2
0
从零开始入门 K8s | Kubernetes 网络概念及策略控制

作者 | 阿里巴巴高级技术专家 叶磊 一、Kubernetes 基本网络模型 本文来介绍一下 Kubernetes 对网络模型的一些想法。大家知道 Kubernetes 对于网络具体实现方案,没有什么限制,也没有给出特...

阿里巴巴云原生
29分钟前
2
0
天气获取

本文转载于:专业的前端网站➨天气获取 $.get("http://wthrcdn.etouch.cn/WeatherApi", { citykey: cityCode }, function (d) { //创建文档对象 var parser = new ......

前端老手
29分钟前
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部