SpringBoot兩種方式刷新配置信息
一、第一種方式
@?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/refresh/
到此這篇關(guān)于SpringBoot兩種方式刷新配置信息的文章就介紹到這了,更多相關(guān)SpringBoot刷新配置信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java將一個目錄下的所有數(shù)據(jù)復(fù)制到另一個目錄下
這篇文章主要為大家詳細介紹了java將一個目錄下的所有數(shù)據(jù)復(fù)制到另一個目錄下,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08Java中設(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格式是一種常見的HTTP請求數(shù)據(jù)格式,它將請求參數(shù)編碼為鍵值對的形式,以便于傳輸和解析,下面這篇文章主要給大家介紹了關(guān)于后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù),需要的朋友可以參考下2023-05-05SpringBoot中使用異步調(diào)度程序的高級方法
本文主要介紹了SpringBoot中使用異步調(diào)度程序的高級方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07基于params、@PathVariabl和@RequestParam的用法與區(qū)別說明
這篇文章主要介紹了方法參數(shù)相關(guān)屬性params、@PathVariabl和@RequestParam用法與區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08實現(xiàn)一個基于Servlet的hello world程序詳解步驟
Java Servlet 是運行在 Web 服務(wù)器或應(yīng)用服務(wù)器上的程序,它是作為來自 Web 瀏覽器或其他 HTTP 客戶端的請求和 HTTP 服務(wù)器上的數(shù)據(jù)庫或應(yīng)用程序之間的中間層2022-02-02