欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot兩種方式刷新配置信息

 更新時間:2023年08月24日 14:41:58   作者:_不吃貓的魚_  
這篇文章主要介紹了SpringBoot兩種方式刷新配置信息,一種是@?ConfigurationProperties?不能自動刷新,需要手動調(diào)用contextRefresher.refresh()方法來刷新配置,第二種方法可以嘗試下,需要的朋友可以參考下

一、第一種方式

@?ConfigurationProperties?不能自動刷新,需要手動調(diào)用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 注解可以使得這個類具備刷新功能,可以在配置文件內(nèi)容變更后,刷新并更新這個配置類的屬性值。

 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
/**
 *
 * @RefreshScope 注解可以使得這個類具備刷新功能,可以在配置文件內(nèi)容變更后,刷新并更新這個配置類的屬性值。使用@RefreshScope注解,僅僅只能刷新@Value的配置屬性。
 *
 * 在這個配置類中,使用了 @Value 注解注入了一個屬性 uuid,該屬性的值來源于配置文件中 rest.uuid 屬性的值。
 * 由于該配置類使用了 @RefreshScope 注解,所以如果配置文件中 rest.uuid 的值發(fā)生變化,
 * Spring Cloud Config Server 就會通知該配置類,然后調(diào)用 setUuid() 方法刷新該屬性的值,從而實現(xiàn)了動態(tài)刷新配置的效果。
 *
 *
 * 如果使用@RefreshScope注解,僅僅只能刷新@Value的配置屬性,
 * 而對于@ConfigurationProperties則不能自動刷新,需要手動調(diào)用contextRefresher.refresh()方法來刷新配置。
 *
 * 因此,在DemoController中的refresh()方法,通過開啟一個新的線程來異步調(diào)用contextRefresher.refresh()方法,
 * 實現(xiàn)對@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>

實現(xiàn)效果

http://localhost:8081/show/

 http://localhost:8081/refresh/

到此這篇關(guān)于SpringBoot兩種方式刷新配置信息的文章就介紹到這了,更多相關(guān)SpringBoot刷新配置信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java中Range函數(shù)的簡單介紹

    Java中Range函數(shù)的簡單介紹

    這篇文章主要介紹了Java中Range函數(shù)的簡單介紹,Java中的range方法用于返回IntStream和LongStream在函數(shù)參數(shù)范圍內(nèi)的順序值
    2022-07-07
  • SpringBoot 整合jdbc和mybatis的方法

    SpringBoot 整合jdbc和mybatis的方法

    該文章主要為記錄如何在SpringBoot項目中整合JDBC和MyBatis,在整合中我會使用簡單的用法和測試用例,感興趣的朋友跟隨小編一起看看吧
    2019-11-11
  • java將一個目錄下的所有數(shù)據(jù)復(fù)制到另一個目錄下

    java將一個目錄下的所有數(shù)據(jù)復(fù)制到另一個目錄下

    這篇文章主要為大家詳細介紹了java將一個目錄下的所有數(shù)據(jù)復(fù)制到另一個目錄下,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • Spring Boot接口限流的常用算法及特點

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

    這篇文章主要給大家介紹了關(guān)于Spring Boot接口限流的常用算法及特點的相關(guān)資料,文中通過圖文介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Java中設(shè)置session超時(失效)的三種方法

    Java中設(shè)置session超時(失效)的三種方法

    這篇文章主要介紹了Java中設(shè)置session超時(失效)的三種方法,本文講解了在web容器中設(shè)置、在工程的web.xml中設(shè)置、通過java代碼設(shè)置3種方法,需要的朋友可以參考下
    2015-07-07
  • 后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù)

    后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù)

    x-www-form-urlencoded格式是一種常見的HTTP請求數(shù)據(jù)格式,它將請求參數(shù)編碼為鍵值對的形式,以便于傳輸和解析,下面這篇文章主要給大家介紹了關(guān)于后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù),需要的朋友可以參考下
    2023-05-05
  • 如何用Java模擬XN*2圖靈機

    如何用Java模擬XN*2圖靈機

    這篇文章主要介紹了如何用Java模擬XN*2圖靈機方法,感興趣的朋友可以參考下
    2021-04-04
  • SpringBoot中使用異步調(diào)度程序的高級方法

    SpringBoot中使用異步調(diào)度程序的高級方法

    本文主要介紹了SpringBoot中使用異步調(diào)度程序的高級方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-07-07
  • 基于params、@PathVariabl和@RequestParam的用法與區(qū)別說明

    基于params、@PathVariabl和@RequestParam的用法與區(qū)別說明

    這篇文章主要介紹了方法參數(shù)相關(guān)屬性params、@PathVariabl和@RequestParam用法與區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 實現(xiàn)一個基于Servlet的hello world程序詳解步驟

    實現(xiàn)一個基于Servlet的hello world程序詳解步驟

    Java Servlet 是運行在 Web 服務(wù)器或應(yīng)用服務(wù)器上的程序,它是作為來自 Web 瀏覽器或其他 HTTP 客戶端的請求和 HTTP 服務(wù)器上的數(shù)據(jù)庫或應(yīng)用程序之間的中間層
    2022-02-02

最新評論