SpringBootNacos实现自动刷新
背景
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 的值并没有改变
优化
如何修改为自动更新,加上注解 @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刷新内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341