SpringBoot不重启服务的情况下刷新配置文件

文章介绍了如何在SpringBoot应用中实现不重启服务的情况下刷新配置文件,主要依赖于`spring-cloud-starter-config`和`spring-boot-starter-actuator`。通过`@RefreshScope`注解和`ContextRefresher`类,可以实现在运行时更新配置并立即生效。测试Controller展示了如何调用刷新接口来更新配置并验证新值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot不重启服务的情况下刷新配置文件

  1. 所需要的依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <!--监控+refresh配置-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    
  2. 测试controller

    package com.dist.ytgz.approve.controller;
    
    import io.swagger.v3.oas.annotations.tags.Tag;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.cloud.context.refresh.ContextRefresher;
    import org.springframework.web.bind.annotation.*;
    
    /**
     * TODO
     *
     * @author <a href="mailto:zhangxiao@dist.com.cn">Zhang Xiao</a>
     * @since
     */
    @RefreshScope
    @Tag(name = "test")
    @RestController
    @RequestMapping("/test")
    public class TestController {
    
        @Autowired
        private ContextRefresher contextRefresher;
    
        @Value("${test}")
        private String t;
    
        /**
         * 刷新配置文件, 每个需要用到配置文件的类, 都需要加@RefreshScope, 所以放我们写配置类@Configuration/@ConfigurationProperties等...最好吧@RefreshScope加上
         * @return
         */
        @GetMapping("/refresh")
        public String refresh() {
            new Thread(() -> contextRefresher.refresh()).start();
            return "success";
        }
    
        @GetMapping("/test")
        public String test() {
            return this.t;
        }
    
    }
    
  3. 启动类正常启动就行不需要加其他任何的注解

  4. 编译后的文件是1

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Rr6elj96-1677921952438)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20230304170440801.png)]

  5. 调用test方法得到的结果

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SP4j9Nkk-1677921952439)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20230304171445189.png)]

  6. 把编译后的配置文件test修改为2, 然后调用我们refresh方法, 然后再次调用test方法

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ClXO0fKz-1677921952440)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20230304171611166.png)]

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nRl2dMUR-1677921952440)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20230304171650567.png)]

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IsS33qDb-1677921952441)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20230304171714904.png)]

  7. 总结

    主要是refreshEnvironment它是比较前一个版本的配置文件和修改后的配置文件的却别, 把修改过得key当放入到对应的环境变量修改事件中, 放到对应的上下文, 返回对应的key, 刷新标注了RefreshScope注解的bean, 上下文会触发对应的事件进行修改。

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-g8j43ht5-1677921952441)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20230304172006010.png)]

    ContextRefresher 是用来刷新容器中标记了@RefreshScope Bean的类,它的refresh()方法可以实现这一功能。大概流程就是刷新当前容器的environment,删除掉Scope中的实例,然后在获取实例时从小建立bean,这时候新的enviroment已经生成,就拿到了最新的配置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

只因为你温柔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值