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

标签:SpringBoot,Nacos,自动刷新
0
投稿

猜你喜欢

  • Android开发中TextView 实现右上角跟随文本动态追加圆形红点

    2022-09-07 07:20:57
  • C#网站生成静态页面的实例讲解

    2021-11-01 16:33:08
  • Java中实现简单的Excel导出

    2021-10-21 07:23:25
  • java实现文件上传下载功能

    2021-11-26 17:19:05
  • 关于spring boot中几种注入方法的一些个人看法

    2022-09-07 10:15:14
  • 解决android studio卡顿,提升studio运行速度的方法

    2021-11-19 01:25:05
  • JVM类运行机制实现原理解析

    2023-11-01 18:26:20
  • Android中banner的使用步骤

    2023-07-04 17:47:23
  • C语言实现BMP图像处理(哈夫曼编码)

    2022-10-08 02:04:02
  • 实例讲解Java 自旋锁

    2021-09-17 09:38:50
  • 实例讲述Java IO文件复制

    2023-01-30 17:25:32
  • Java静态代理和动态代理总结

    2022-01-02 18:49:21
  • BootStrap mvcpager分页样式(get请求,刷新页面)

    2023-09-19 04:18:29
  • native.js获取手机硬件基本信息实例代码android版

    2023-10-19 13:33:09
  • C#实现支付宝沙箱支付的项目实践

    2021-07-29 17:18:18
  • 分布式调度XXL-Job整合Springboot2.X实战操作过程(推荐)

    2023-11-23 09:43:38
  • 如何使用MybatisPlus快速进行增删改查详解

    2023-11-03 06:58:13
  • C#中实现Json序列化与反序列化的几种方式

    2021-11-06 01:31:07
  • SpringBoot切面拦截@PathVariable参数及抛出异常的全局处理方式

    2023-05-27 13:59:52
  • 探讨Java中的深浅拷贝问题

    2023-01-07 01:12:44
  • asp之家 软件编程 m.aspxhome.com