springcloud配置文件启动加载以及自动刷新 一

spring cloud 配置文件启动加载

一直对spring cloud配置文件加载没太弄明白,这次想整合spring cloud加载过程发现的问题进行整理分析

一、启动加载bootstrap.yml/.properties

SpringBoot默认支持properties和YAML两种格式的配置文件。前者格式简单,但是只支持键值对。如果需要表达列表,最好使用YAML格式。

bootstrap.yml(bootstrap.properties)用来在程序引导时执行,应用于更加早期配置信息读取,如可以使用来配置application.yml中使用到参数等应用程序特有配置信息。可以理解成系统级别的一些参数配置,这些参数一般是不会变动的。一旦bootStrap.yml 被加载,则内容不会被覆盖。
application.yml 可以用来定义应用级别的, 应用程序特有配置信息,可以用来配置后续各个模块中需使用的公共参数等。

bootstrap.yml 先于 application.yml 加载

二、典型的应用场景如下:

1,当使用 Spring Cloud Config Server 的时候,你应该在 bootstrap.yml 里面指定 spring.application.name 和 spring.cloud.config.server.git.uri,如果非Config Server则不必要。
技术上,bootstrap.yml 是被一个父级的 Spring ApplicationContext 加载的。这个父级的 Spring ApplicationContext是先加载的,在加载application.yml 的 ApplicationContext之前。

为何需要把 config server 的信息放在 bootstrap.yml 里?

当使用 Spring Cloud 的时候,配置信息一般是从 config server 加载的,为了取得配置信息(比如密码等),你需要一些提早的引导配置。因此,把 config server 信息放在 bootstrap.yml,用来加载在这个时期真正需要的配置信息。

三、高级使用场景

启动上下文
Spring Cloud会创建一个Bootstrap Context,作为Spring应用的Application Context的父上下文。初始化的时候,Bootstrap Context负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的EnvironmentBootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。 也就是说如果加载的 application.yml 的内容标签与 bootstrap 的标签一致,application 也不会覆盖 bootstrap,而 application.yml 里面的内容可以动态替换。Bootstrap contextApplication Context有着不同的约定,所以新增了一个bootstrap.yml文件,而不是使用application.yml (或者application.properties)。保证Bootstrap ContextApplication Context配置的分离。下面是一个例子: bootstrap.yml

spring:
  application:
    name: foo
  cloud:
    config:
      uri: ${
   SPRING_CONFIG_URI:http://localhost:8888}

推荐在bootstrap.yml or application.yml里面配置spring.application.name. 你可以通过设置spring.cloud.bootstrap.enabled=false来禁用bootstrap
#应用上下文层次结构
如果你通过SpringApplication或者SpringApplicationBuilder创建一个Application Context,那么会为spring应用的Application Context创建父上下文Bootstrap Context。在Spring里有个特性,子上下文会继承父类的property sources and profiles ,所以main application context 相对于没有使用Spring Cloud Config,会新增额外的property sources
额外的property sources有:
“bootstrap” : 如果在Bootstrap Context扫描到PropertySourceLocator并且有属性,则会添加到CompositePropertySource。Spirng Cloud Config就是通过这种方式来添加的属性的,具体详情如下:在同文件夹的spring.factories中定义了自动配置所需要引入的类,【同样适用springboot其他组件】

该文件内容如下:

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.config.client.ConfigClientAutoConfiguration

# Bootstrap components
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
配置动态刷新_SpringCloud配置中心动态加载(刷新)配置文件可以通过Spring Cloud Config实现。具体实现步骤如下: 1. 在pom.xml文件中引入Spring Cloud Config依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> ``` 2. 在启动类中添加@EnableConfigServer注解,开启配置中心服务: ``` @SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } ``` 3. 在配置文件中添加配置中心相关配置: ``` spring: cloud: config: server: git: uri: https://github.com/{git_username}/{git_repository}.git search-paths: '{config_files_path}' username: {git_username} password: {git_password} ``` 其中,uri为Git仓库地址,search-paths为配置文件路径,username和password为Git仓库的用户名和密码。 4. 在需要动态加载配置的应用程序中,添加@RefreshScope注解,表示该类中的配置可以被动态刷新: ``` @RestController @RefreshScope public class ConfigController { @Value("${config_name}") private String configValue; @GetMapping("/config") public String getConfig() { return configValue; } } ``` 5. 在需要动态刷新配置的时候,向应用程序发送POST请求,请求路径为/actuator/refresh: ``` curl -X POST http://localhost:8080/actuator/refresh ``` 以上就是配置动态刷新_SpringCloud配置中心动态加载(刷新)配置文件的实现步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值