Python 官方文档:入门教程 => 点击学习
目录背景现象优化背景 SpringBoot 版本 <parent> <groupId>org.springframework.boot</gro
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 配置
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刷新内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: SpringBootNacos实现自动刷新
本文链接: https://www.lsjlt.com/news/177711.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0