SpringBoot Nacos實現自動刷新
背景
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
現象
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); } }
到此這篇關于SpringBoot Nacos實現自動刷新的文章就介紹到這了,更多相關SpringBoot Nacos刷新內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
IDEA?2021.3?使用及idea2021.3.1激活使用方法
IDEA?全稱?IntelliJ?IDEA,是java語言開發(fā)的集成環(huán)境,IntelliJ在業(yè)界被公認為最好的java開發(fā)工具之一,今天通過本文給大家介紹idea2021.3.1激活及使用教程,感興趣的朋友一起看看吧2022-01-01springboot+mybatis-plus實現內置的CRUD使用詳解
這篇文章主要介紹了springboot+mybatis-plus實現內置的CRUD使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12基于strict-origin-when-cross-origin問題的解決
這篇文章主要介紹了基于strict-origin-when-cross-origin問題的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03