这篇文章主要介绍“Spring的@Value怎么从Nacos配置中心获取值并自动刷新”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Spring的@Value怎么从Nacos配置中心获取值并自动刷新”文章能帮助大家解决问题。
在使用Nacos作为配置中心时,除了@NacosValue可以做到自动刷新外,nacos-spring-context:0.3.4版本是支持@Value获取Nacos配置中心的值,并动态刷新的,该功能是Spri依靠ngValueAnnotationBeanPostProcessor类来实现:
@Override
protected Tuple<String, NacosValueTarget> doWithAnnotation(String beanName,
Object bean, Value annotation, int modifiers, Method method, Field field) {
if (annotation != null) {
if (Modifier.isStatic(modifiers)) {
return Tuple.empty();
}
if (bean.getClass().isAnnotationPresent(NacosRefresh.class)) {
String placeholder = resolvePlaceholder(annotation.value());
if (placeholder == null) {
return Tuple.empty();
}
NacosValueTarget nacosValueTarget = new NacosValueTarget(bean, beanName,
method, field);
nacosValueTarget.setAnnotationType(getAnnotationType().getSimpleName());
logger.debug("@Value register auto refresh");
return Tuple.of(placeholder, nacosValueTarget);
}
}
return Tuple.empty();
}
分析其源码可以看到,如果bean上有注解@NacosRefresh,则会自动刷新。
在实际使用时,发现bean上的注解是@Configuration则不会自动刷新,而使用@Component则可以做到自动刷新。
代码如下:
@NacosRefresh
//@Component
@Configuration
public class BeanTest {
@Value("${autofresh.test}")
private String testValue;
@NacosValue(value="${autofresh.test2}",autoRefreshed = true)
private String testValue2;
/**
* @return the testValue2
*/
public String getTestValue2() {
return testValue2;
}
/**
* @param testValue2 the testValue2 to set
*/
public void setTestValue2(String testValue2) {
this.testValue2 = testValue2;
}
/**
* @return the testValue
*/
public String getTestValue() {
return testValue;
}
/**
* @param testValue the testValue to set
*/
public void setTestValue(String testValue) {
this.testValue = testValue;
}
}
测试类:
@Test
public void testValueRefreshinNacos() throws InterruptedException {
System.out.println(beanTest.getTestValue());
System.out.println(beanTest.getTestValue2());
System.out.println("------修改前");
Thread.sleep(1000*60);
System.out.println(beanTest.getTestValue());
System.out.println(beanTest.getTestValue2());
System.out.println("------修改后");
}
这就和@Component与@Configuration的区别有关了,@Component注解的bean是原生bean,@Configuration是被cglib动态增加的bean。
引入jar包:
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.7</version>
</dependency>
编写配置类:
@Configuration
@EnableNacosConfig(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848"))
@NacosPropertySource(dataId = "example", group="test",autoRefreshed = true)
public class NacosConfiguration { }
编写测试类:
@Controller
public class ConfigController {
@NacosValue(value = "${test.data}", autoRefreshed = true)
private boolean data;
@RequestMapping(value = "/test", method = GET)
@ResponseBody
public boolean get() { return data; }
}
引入jar包:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
引入配置:
spring:
application:
name: example
cloud:
nacos:
config:
extension-configs[0]:
dataId: test.yml
group: test
refresh: true
server-addr: 127.0.0.1:8848
namespace: c845e96f-4423-4618-8c26-5e4d510f566a
enabled: true
refresh-enabled: true
编写测试类:
@RestController
@RefreshScope
public class TestController {
@NacosValue(value = "${test.data}", autoRefreshed = true)
private String data;
@Value(value = "${test.data}")
private String datas;
@GetMapping("test")
public String test() {
return "data :" + data + ",datas="+datas;
}
}
备注:
方式一@NacosValue获取最新值nacos配置信息需要写在配置类上
方式二@NacosValue获取不到值(如果需要使用该注解需要引入方式一的jar,但是不重启服务获取不到最新值),@Value获取最新值一定要加@RefreshScope注解,配置文件中配置refresh: true
关于“Spring的@Value怎么从Nacos配置中心获取值并自动刷新”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。