SpringBoot两种方式刷新配置信息

 更新时间:2023年08月24日 14:41:58   作者:_不吃猫的鱼_  
这篇文章主要介绍了SpringBoot两种方式刷新配置信息,一种是@​ConfigurationProperties​不能自动刷新,需要手动调用contextRefresher.refresh()方法来刷新配置,第二种方法可以尝试下,需要的朋友可以参考下

一、第一种方式

@​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
}

使用 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: 8081

pom.xml

 <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>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

实现效果

http://localhost:8081/show/

 http://localhost:8081/refresh/

到此这篇关于SpringBoot两种方式刷新配置信息的文章就介绍到这了,更多相关SpringBoot刷新配置信息内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java中Range函数的简单介绍

    Java中Range函数的简单介绍

    这篇文章主要介绍了Java中Range函数的简单介绍,Java中的range方法用于返回IntStream和LongStream在函数参数范围内的顺序值
    2022-07-07
  • SpringBoot 整合jdbc和mybatis的方法

    SpringBoot 整合jdbc和mybatis的方法

    该文章主要为记录如何在SpringBoot项目中整合JDBC和MyBatis,在整合中我会使用简单的用法和测试用例,感兴趣的朋友跟随小编一起看看吧
    2019-11-11
  • java将一个目录下的所有数据复制到另一个目录下

    java将一个目录下的所有数据复制到另一个目录下

    这篇文章主要为大家详细介绍了java将一个目录下的所有数据复制到另一个目录下,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-08-08
  • Spring Boot接口限流的常用算法及特点

    Spring Boot接口限流的常用算法及特点

    这篇文章主要给大家介绍了关于Spring Boot接口限流的常用算法及特点的相关资料,文中通过图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • Java中设置session超时(失效)的三种方法

    Java中设置session超时(失效)的三种方法

    这篇文章主要介绍了Java中设置session超时(失效)的三种方法,本文讲解了在web容器中设置、在工程的web.xml中设置、通过java代码设置3种方法,需要的朋友可以参考下
    2015-07-07
  • 后端如何接收格式为x-www-form-urlencoded的数据

    后端如何接收格式为x-www-form-urlencoded的数据

    x-www-form-urlencoded格式是一种常见的HTTP请求数据格式,它将请求参数编码为键值对的形式,以便于传输和解析,下面这篇文章主要给大家介绍了关于后端如何接收格式为x-www-form-urlencoded的数据,需要的朋友可以参考下
    2023-05-05
  • 如何用Java模拟XN*2图灵机

    如何用Java模拟XN*2图灵机

    这篇文章主要介绍了如何用Java模拟XN*2图灵机方法,感兴趣的朋友可以参考下
    2021-04-04
  • SpringBoot中使用异步调度程序的高级方法

    SpringBoot中使用异步调度程序的高级方法

    本文主要介绍了SpringBoot中使用异步调度程序的高级方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-07-07
  • 基于params、@PathVariabl和@RequestParam的用法与区别说明

    基于params、@PathVariabl和@RequestParam的用法与区别说明

    这篇文章主要介绍了方法参数相关属性params、@PathVariabl和@RequestParam用法与区别,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • 实现一个基于Servlet的hello world程序详解步骤

    实现一个基于Servlet的hello world程序详解步骤

    Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层
    2022-02-02

最新评论