SpringBoot Nacos實現(xiàn)自動刷新
背景
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進行版本管理
<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:標識分組
- 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 的值
后臺可以看到 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)化
如何修改為自動更新,加上注解 @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實現(xiàn)自動刷新的文章就介紹到這了,更多相關(guān)SpringBoot Nacos刷新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java使用Sftp和Ftp實現(xiàn)對文件的上傳和下載
這篇文章主要介紹了Java使用Sftp和Ftp實現(xiàn)對文件的上傳和下載,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03
SpringBoot利用AOP實現(xiàn)一個日志管理詳解
目前有這么個問題,有兩個系統(tǒng)CSP和OMS,這倆系統(tǒng)共用的是同一套日志操作:Log;目前想?yún)^(qū)分下這倆系統(tǒng)的日志操作,那沒辦法了,只能重寫一份Log的日志操作。本文就將利用AOP實現(xiàn)一個日志管理,需要的可以參考一下2022-09-09
IDEA?2021.3?使用及idea2021.3.1激活使用方法
IDEA?全稱?IntelliJ?IDEA,是java語言開發(fā)的集成環(huán)境,IntelliJ在業(yè)界被公認為最好的java開發(fā)工具之一,今天通過本文給大家介紹idea2021.3.1激活及使用教程,感興趣的朋友一起看看吧2022-01-01
springboot+mybatis-plus實現(xiàn)內(nèi)置的CRUD使用詳解
這篇文章主要介紹了springboot+mybatis-plus實現(xiàn)內(nèi)置的CRUD使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12
詳解Spring中實現(xiàn)接口動態(tài)的解決方法
最近在工作遇到的一個,發(fā)現(xiàn)網(wǎng)上的資料較少,所以想著總結(jié)分享下,下面這篇文章主要給大家介紹了關(guān)于Spring中實現(xiàn)接口動態(tài)的解決方法,文中通過完整的示例代碼給大家介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧。2017-07-07
springboot整合quartz實現(xiàn)定時任務示例
spring支持多種定時任務的實現(xiàn)。我們來介紹下使用spring的定時器和使用quartz定時器,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04
基于strict-origin-when-cross-origin問題的解決
這篇文章主要介紹了基于strict-origin-when-cross-origin問題的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03

