读取配置文件中的改动【无需重启springboot】

依赖引入pom.xml

在官网查找依赖cloud和springboot关系
在这里插入图片描述
查找spring-cloud-context依赖
在这里插入图片描述

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2022.0.3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

@ConfigurationProperties

此注解不能自动刷新,需要手动调用contextRefresher.refresh()方法来刷新配置。

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "biz")
public class BizConfig {
    private String key;
    private Long refresh;

    //省略 gettersetter...
}

@RefreshScope

此注解可以使得这个类具备刷新功能,可以在配置文件内容变更后,刷新并更新这个配置类的属性值。

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

/**
 *
 * @RefreshScope 注解可以使得这个类具备刷新功能,可以在配置文件内容变更后,刷新并更新这个配置类的属性值。使用@RefreshScope注解,仅仅只能刷新@Value的配置属性。
 *
 * 在这个配置类中,使用了 @Value 注解注入了一个属性 uuid,该属性的值来源于配置文件中 rest.uuid 属性的值。
 * 由于该配置类使用了 @RefreshScope 注解,所以如果配置文件中 rest.uuid 的值发生变化,
 * Spring Cloud Config Server 就会通知该配置类,然后调用 setUuid() 方法刷新该属性的值,从而实现了动态刷新配置的效果。
 *
 *
 * 如果使用@RefreshScope注解,仅仅只能刷新@Value的配置属性,
 * 而对于@ConfigurationProperties则不能自动刷新,需要手动调用contextRefresher.refresh()方法来刷新配置。
 *
 * 因此,在DemoController中的refresh()方法,通过开启一个新的线程来异步调用contextRefresher.refresh()方法,
 * 实现对@ConfigurationProperties的刷新。这样就可以保证对于@Value和@ConfigurationProperties两种配置属性都可以进行刷新。
 */
@RefreshScope
@Component
public class ValueConfig {
    @Value("${rest.uuid}")
    private String uuid;

   // 省略 gettersetter
}

ContextReferer

主要借助这个类来实现配置刷新
这个类全路径为 org.springframework.cloud.context.refresh.ContextRefresher
直接调用这个类的refresh()
使用 contextRefresher.refresh() 刷新

import com.alibaba.fastjson.JSONObject;
import com.lfsun.bootdynamicrefresh.config.BizConfig;
import com.lfsun.bootdynamicrefresh.config.ValueConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {
    @Autowired
    private ContextRefresher contextRefresher;

    @Autowired
    private BizConfig bizConfig;

    @Autowired
    private ValueConfig valueConfig;

    /**
     * 查询配置信息
     */
    @GetMapping(path = "/show")
    public String show() {
        JSONObject res = new JSONObject();
        res.put("biz", JSONObject.toJSONString(bizConfig));
        res.put("uuid", valueConfig.getUuid());
        return res.toJSONString();
    }

    /**
     * 刷新配置信息
     */
    @GetMapping(path = "/refresh")
    public String refresh() {
        // 新开一个线程进行配置信息的刷新,避免阻塞其他请求的处理
        new Thread(() -> contextRefresher.refresh()).start();
        // 返回刷新后的配置信息
        return show();
    }
}

配置文件 application.yml
biz:
  refresh: ${random.long}
  key: refresh-test

rest:
  uuid: ${random.uuid}

server:
  port: 8888

总结说明

使用ConfigurationProperties方式获取注解时,自动支持刷新配置
使用@Value注解的方式,需要开启@RefreshScope注解
然后需要调用refresh()方法才能刷新

演示

在这里插入图片描述

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值