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-cl编程客栈oud-starter-alibaba-nacos-config</artifactId> <version>2.2.6.RELEASE</version> </dependency> </dependencies>
Spring-ClcYgKTWuECoud 版本
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> 开发者_Go入门 <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2021.0.1</version> www.devze.com <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='bpythonootstrapProperties-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 runninpythong 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 的值并没有改变
优化
如何修改为自动更新,加上注解 @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刷新内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
精彩评论