SpringBoot Nacos实现自动刷新
作者:我有一只肥螳螂 发布时间:2023-09-16 04:17:09
背景
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);
}
}
来源:https://blog.csdn.net/weixin_42555971/article/details/128625639
猜你喜欢
- 要想实现android手机通过扫描名片,得到名片信息,可以使用脉可寻提供的第三方SDK,即Maketion ScanCard SDK,脉可寻
- 前言很多时候,当你以为掌握了事实真相的时间,如果你能再深入一点,你可能会发现另外一些真相。比如面向切面编程的最佳编程实践是AOP,AOP的主
- 在之前项目中有用到关于获取手机联系人的部分,闲置就想和大家分享一下,话不多说,上代码:java部分:package com.example.
- 概述:App几乎都离不开与服务器的交互,本文主要讲解了flutter网络请求三种方式 flutter自带的HttpClient、 第三方库h
- Android之文件数据存储一、文件保存数据介绍Activity提供了openFileOutput()方法可以用于把数据输出到文件中,具体的
- package cn.response;import java.awt.Color;import java.awt.Font;import
- 概述: 当希望能直接在数据库语言中只检索符合条件的记录,不需要再通过程序对其做处理时,SQL语句分页
- 本文实例为大家分享了Unity实现场景漫游相机的具体代码,供大家参考,具体内容如下前言拿到场景后总喜欢在场景里面玩一段时间,那这个脚本就是你
- 使用前准备Build.gradle文件配置dependencies配置compile 'com.squareup.retrofit2
- 概述在移动应用开发中,消息推送可以说是一项非常重要的功能,它能够起到提醒或者唤醒用户的作用,同时也是产品运营人员更高效地实现运营目标的重要手
- 摘要:想必大家做开发的时候都会用到下拉刷新的控件,现在各种第三方的下拉刷新控件不胜枚举。当然最NB的还是XListView。其他也有针对Gr
- 本文实例为大家分享了Java实现简单幸运抽奖的具体代码,供大家参考,具体内容如下代码模块:User类:package test1;publi
- 路由做Android/iOS原生开发的时候,要打开一个新的页面,你得知道你的目标页面对象,然后初始化一个Intent或者ViewContro
- 前言Flutter (Channel stable, 2.10.3, on Microsoft Windows [Version 10.0.
- CSRF介绍CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click atta
- .c 源程序 ----- 编译 ----- 链接 ---- exe ----运行 -------->程序翻译环境和执行环境翻译环境:源
- 一、使用Optional引言1.1、代码问题引出在写程序的时候一般都遇到过 NullPointerException,所以经常会对程序进行非
- Servlet3.0的出现是servlet史上最大的变革,其中的许多新特性大大的简化了web应用的开发,为广大劳苦的程序员减轻了压力,提高了
- PreparedStatement介绍可以通过调用 Connection 对象的 prepareStatement(String sql)
- 1、说明isInterrupted()可以判断当前线程是否被中断,仅仅是对interrupt()标识的一个判断,并不会影响标识发生任何改变(