前言
在微服务的项目中,我们经常使用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上的属性值,避免了重启应用的麻烦。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论