前言
在微服务的项目中,我们经常使用Nacos作为配置中心,用于管理应用程序的属性配置。当我们在Nacos上修改属性值时,希望应用程序能够自动刷新并应用最新的属性值,以避免重启应用。本篇博客将介绍Nacos属性值自动刷新的方式,并提供相应的示例代码:
项目中引入相关依赖
<!--nacos config-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
方式一:使用@RefreshScope注解
@RefreshScope注解是Spring Cloud提供的一种属性刷新机制。它可以应用于需要动态刷新的配置类或方法上,当Nacos上的属性值发生变化时,通过调用/actuator/refresh端点来刷新被注解的类或方法
这个时候是获取到配置文件信息,但是当你修改完配置,只能通过重启服务才能获取到最新的配置信息
想要实现动态刷新无需重启服务,来加载最新的配置信息:
在需要动态刷新的配置类或方法上添加@RefreshScope注解
import com.supervise.common.core.web.controller.BaseController;
import com.supervise.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
@RefreshScope //自动刷新bean配置
public class TestController extends BaseController {
//测试
@Value("${sys.test.name}")
private String name;
/**
* 测试自动刷新配置
*/
@GetMapping(value = "/onTrial")
public AjaxResult onTrial() {
return success(name);
}
}
配置文件改成:张三888,看结果,实现了配置文件动态刷新
创建配置类:
@Data
@RefreshScope //自动刷新bean配置
@Component
public class TestConfig {
//测试
@Value("${sys.test.name}")
private String name;
}
配置文件改成:张三666,看结果,这样也是实现了配置文件动态刷新
方式二:使用@ConfigurationProperties
import com.supervise.common.core.web.controller.BaseController;
import com.supervise.common.core.web.domain.AjaxResult;
import com.supervise.supervision.config.TestConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("/test")
//@RefreshScope //自动刷新bean配置
public class TestController extends BaseController {
//测试
@Value("${sys.test.name}")
private String name;
@Resource
private TestConfig testConfig;
/**
* 测试自动刷新配置
*/
@GetMapping(value = "/onTrial")
public AjaxResult onTrial() {
return success(testConfig.getName());
}
}
package com.supervise.supervision.config;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
@Data
@ConfigurationProperties(prefix = "sys.test") //只要前缀名和变量名两者拼接与配置配置文件一致,就能完成属性的自动注入
@Component
public class TestConfig {
private String name;
}
配置文件改成:张三999,看结果,这样也是能实现了配置文件动态刷新
微服务项目要特别注意一下,在服务启动的时候加载配置文件是有一个优先级的,优先加载本服务所对应的配置文件,后加载公共的配置文件,要实现配置自动刷新的情况,需要把配置信息写到当前服务所对应的配置文件中!!!
结论
本篇博客介绍了实现Nacos属性值自动刷新的方式:使用@RefreshScope注解、@ConfigurationProperties。通过这些方式,您可以在应用程序运行时动态刷新Nacos上的属性值,避免了重启应用的麻烦。希望本篇博客对您的项目开发有所帮助!