SpringBoot不重启,刷新配置文件信息

项目配置文件更改,不重启的情况下重载配置文件,emmmmm第一想法是刷新上下文可以实现,真香。

Spring-Boot版本: 2.1.17.RELEASE

1. AbstractApplicationContext Refresh(不可用)

        以下是refresh的部分源码,这里不分析了,可以看到做了很多准备,这个方法是不能实现上下文刷新的。
        因为refresh只能被调起一次,启动类中多次调起会被catch住然后抛出异常。
        那不在启动类中调起呢,会检测线程池,然后给你shut down掉
        所以刷新上下文,一言难尽

   this.prepareRefresh();
   ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
   this.prepareBeanFactory(beanFactory);

   try {
       this.postProcessBeanFactory(beanFactory);
       this.invokeBeanFactoryPostProcessors(beanFactory);
       this.registerBeanPostProcessors(beanFactory);
       this.initMessageSource();
       this.initApplicationEventMulticaster();
       this.onRefresh();
       this.registerListeners();
       this.finishBeanFactoryInitialization(beanFactory);
       this.finishRefresh();
   } catch (BeansException var9) {
       if (this.logger.isWarnEnabled()) {
           this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9);
       }

       this.destroyBeans();
       this.cancelRefresh(var9);
       throw var9;
   } finally {
       this.resetCommonCaches();
   }

在启动时,调起refresh,会被告知仅能调起一次。
在这里插入图片描述
在请求方法中调起refresh,会检测线程池,然后shut down
在这里插入图片描述

2. RefreshScope 刷新配置

这里只写了SpringBoot 2.0.3.RELEASE 版本以后的demo
依赖:cloud依赖与SpringBoot版本不能差太大,不然启动报错

	<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
        <version>2.1.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

SpringBoot 2.0.3.RELEASE以后的版本需要加配置

management.endpoints.web.exposure.include=refresh

启动项目,查看配置,然后发送请求,然后再看配置是否成功刷新

# 这个是刷新接口
curl -X POST http://localhost:8080/actuator/refresh  

成功后会返回哪些属性的值有变化
在这里插入图片描述
注意,在Idea中,启动项目时,读取的配置文件不是工程的配置文件,是打包后的配置文件,别忽略这个了,不然影响结果。
在这里插入图片描述

借鉴博客:https://www.cnblogs.com/weihuang6620/p/13723219.html

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以使用Spring Cloud Config Server来实现全局动态刷新配置文件Spring Cloud Config Server是一个基于Git仓库的配置中心,它可以管理和提供应用程序的配置文件,并支持动态刷新配置文件。 下面是实现全局动态刷新配置文件的步骤: 1. 引入Spring Cloud Config Server依赖 在pom.xml文件中加入以下依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> ``` 2. 配置Spring Cloud Config Server 在application.properties文件中配置Spring Cloud Config Server: ``` spring.application.name=config-server spring.cloud.config.server.git.uri=<git-repo-url> ``` 其中,`<git-repo-url>`是Git仓库的URL地址。 3. 配置客户端应用程序 在客户端应用程序中,需要引入以下依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> ``` 同时,在application.properties文件中配置客户端应用程序: ``` spring.application.name=my-app spring.cloud.config.uri=http://localhost:8888 ``` 其中,`my-app`是应用程序的名称,`http://localhost:8888`是Spring Cloud Config Server的地址。 4. 配置动态刷新 在客户端应用程序中,需要配置动态刷新。可以在application.properties文件中添加以下配置: ``` management.endpoints.web.exposure.include=* management.endpoint.refresh.enabled=true ``` 其中,`management.endpoints.web.exposure.include=*`是开启所有的Actuator端点,`management.endpoint.refresh.enabled=true`是开启动态刷新功能。 5. 发布配置文件 将应用程序的配置文件上传到Git仓库中。 6. 动态刷新配置文件配置文件发生变化时,可以通过访问以下URL来触发动态刷新: ``` POST http://localhost:8080/actuator/refresh ``` 其中,`http://localhost:8080`是客户端应用程序的地址。 以上就是实现全局动态刷新配置文件的方法。通过Spring Cloud Config Server和动态刷新功能,可以方便地管理和更新应用程序的配置文件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值