SpringBoot Nacos實(shí)現(xiàn)自動(dòng)刷新
背景
SpringBoot 版本
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.4</version> <relativePath/> <!-- lookup parent from repository --> </parent>
Nacos 版本
<dependencies> ... <!--nacos--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2.2.6.RELEASE</version> </dependency> </dependencies>
Spring-Cloud 版本
spring-cloud-alibaba依賴,能對nacos進(jìn)行版本管理
<dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.6.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2021.0.1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
application.yml 配置
- server-addr:nacos地址
- namespace:命名空間,即 id
- group:標(biāo)識(shí)分組
- file-extension:文件后綴名
spring:
cloud:
nacos:
config:
server-addr: http://xxx.com
namespace: name
group: name
file-extension: yml
現(xiàn)象
application.yml 配置 myvalue 的值
myvalue: myvalue-test
接口類引用 myvalue
@RestController @Slf4j public class TestController extends BaseController { @Value("${myvalue}") private String myvalue; @ApiOperation(value = "測試", notes = "測試value") @GetMapping(value = "/test/feng/test") NjcResponseEntity testValue() { log.info( myvalue); return super.success("查詢", myvalue); } }
在線修 nacos 上 myvalue 的值
后臺(tái)可以看到 myvalue 已被修改
2023-01-10 10:56:03.402 WARN [TID: N/A] c.a.c.n.c.NacosPropertySourceBuilder: Ignore the empty nacos configuration and get it based on dataId[pm] & group[pm]
2023-01-10 10:56:03.407 WARN [TID: N/A] c.a.c.n.c.NacosPropertySourceBuilder: Ignore the empty nacos configuration and get it based on dataId[pm.yml] & group[pm]
2023-01-10 10:56:03.415 INFO [TID: N/A] o.s.c.b.c.PropertySourceBootstrapConfiguration: Located property source: [BootstrapPropertySource {name='bootstrapProperties-pm-feng.yml,pm'}, BootstrapPropertySource {name='bootstrapProperties-pm.yml,pm'}, BootstrapPropertySource {name='bootstrapProperties-pm,pm'}]
2023-01-10 10:56:03.417 INFO [TID: N/A] o.s.boot.SpringApplication: The following 1 profile is active: "feng"
2023-01-10 10:56:03.425 INFO [TID: N/A] o.s.boot.SpringApplication: Started application in 0.227 seconds (JVM running for 38.127)
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
2023-01-10 10:56:03.508 INFO [TID: N/A] o.s.c.e.event.RefreshEventListener: Refresh keys changed: [myvalue]
但通過接口獲取 myvalue 的值并沒有改變
優(yōu)化
如何修改為自動(dòng)更新,加上注解 @RefreshScope 即可
@RestController @Slf4j @RefreshScope public class TestController extends BaseController { @Value("${myvalue}") private String myvalue; @ApiOperation(value = "測試", notes = "測試value") @GetMapping(value = "/test/feng/test") NjcResponseEntity testValue() { log.info( myvalue); return super.success("查詢", myvalue); } }
到此這篇關(guān)于SpringBoot Nacos實(shí)現(xiàn)自動(dòng)刷新的文章就介紹到這了,更多相關(guān)SpringBoot Nacos刷新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java使用Sftp和Ftp實(shí)現(xiàn)對文件的上傳和下載
這篇文章主要介紹了Java使用Sftp和Ftp實(shí)現(xiàn)對文件的上傳和下載,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03SpringBoot利用AOP實(shí)現(xiàn)一個(gè)日志管理詳解
目前有這么個(gè)問題,有兩個(gè)系統(tǒng)CSP和OMS,這倆系統(tǒng)共用的是同一套日志操作:Log;目前想?yún)^(qū)分下這倆系統(tǒng)的日志操作,那沒辦法了,只能重寫一份Log的日志操作。本文就將利用AOP實(shí)現(xiàn)一個(gè)日志管理,需要的可以參考一下2022-09-09IDEA?2021.3?使用及idea2021.3.1激活使用方法
IDEA?全稱?IntelliJ?IDEA,是java語言開發(fā)的集成環(huán)境,IntelliJ在業(yè)界被公認(rèn)為最好的java開發(fā)工具之一,今天通過本文給大家介紹idea2021.3.1激活及使用教程,感興趣的朋友一起看看吧2022-01-01springboot+mybatis-plus實(shí)現(xiàn)內(nèi)置的CRUD使用詳解
這篇文章主要介紹了springboot+mybatis-plus實(shí)現(xiàn)內(nèi)置的CRUD使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12基于Java實(shí)現(xiàn)修改圖片分辨率示例代碼
這篇文章主要介紹了一個(gè)可以修改圖片分辨率的java工具類,文中的示例代碼講解詳細(xì),對學(xué)習(xí)JAVA有一定的幫助,感興趣的小伙伴快來跟隨小編一起學(xué)習(xí)吧2021-12-12詳解Spring中實(shí)現(xiàn)接口動(dòng)態(tài)的解決方法
最近在工作遇到的一個(gè),發(fā)現(xiàn)網(wǎng)上的資料較少,所以想著總結(jié)分享下,下面這篇文章主要給大家介紹了關(guān)于Spring中實(shí)現(xiàn)接口動(dòng)態(tài)的解決方法,文中通過完整的示例代碼給大家介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-07-07springboot整合quartz實(shí)現(xiàn)定時(shí)任務(wù)示例
spring支持多種定時(shí)任務(wù)的實(shí)現(xiàn)。我們來介紹下使用spring的定時(shí)器和使用quartz定時(shí)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04基于strict-origin-when-cross-origin問題的解決
這篇文章主要介紹了基于strict-origin-when-cross-origin問題的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03