Nacos配置中心自动加载JSON配置,这种需求场景下,我们
分享于 点击 27016 次 点评:207
Nacos配置中心自动加载JSON配置,这种需求场景下,我们
在项目配置中,有个别场景需要通过nacos配置中心来维护一些项目中非spring上下文中的配置,比如:第三方特殊配置、一些非标准化的配置,想通过nacos来实现灵活管理与实时更新。这种需求场景下,我们可以通过Nacos中提供的两个注解来非常简单的实现我们需求。
- @NacosConfig:需要声明在,由spring管理的bean中,比如:bean的属性上,或者bean的类上。当应用启动时,会将声明了该注解的属性或类,进行赋值。
- @NacosConfigListener:需要声明在,由spring管理的bean中。作用于Bean的方法上,当Nacos中的配置发生变化时,会以方法入参形式将最新配置内容传入,且支持基本数据类型、对象、泛型类。
版本要求:
- 2023.x 系列需升级版本至2023.0.3.2
- 2022.x 系列需升级版本至2022.0.0.2
- 2021.x 系列需升级版本至2021.0.6.2
- 2.2.x 系列需升级至2.2.11
@NacosConfig注解用法介绍
- 此注解可作用于bean属性上和类上,前提是需要声明由spring进行管理。支持多种数据类型:基本类型、List、Map等
List集合,接收JSON格式配置
@Component
public class NacosConfigData {
@NacosConfig(dataId = "list_demo.json",group = "default_group")
private List<MyDemo> myDemoList;
}
Map泛型,接收JSON格式配置
@Component
public class NacosConfigData {
@NacosConfig(dataId = "map_demo.json",group = "default_group")
private Map<Long,MyDemo> myDemoMap;
}
@NacosConfigListener 注解用法介绍
- 此注解主要作用于方法上,在方法上进行声明,当配置发生变化时,会触发声明了此注解的方法,将最新的配置内容以入参方式传入。
自定义bean 接收最新配置
@Component
public class NacosConfigData {
@NacosConfig(dataId = "list_demo.json",group = "default_group")
private List<MyDemo> myDemoList;
@NacosConfigListener(dataId = "list_demo.json",groupId = "default_group")
private void myDemoListChanged(List<MyDemo> myDemoList){
this.myDemoList = myDemoList;
}
@NacosConfig(dataId = "map_demo.json",group = "default_group")
private Map<Long,MyDemo> myDemoMap;
@NacosConfigListener(dataId = "map_demo.json",groupId = "default_group")
private void myDemoMapChanged(List<MyDemo> myDemoList){
this.myDemoList = myDemoList;
}
}
- 此注解可作用于bean属性上和类上,前提是需要声明由spring进行管理。支持多种数据类型:基本类型、List、Map等
List集合,接收JSON格式配置
@Component
public class NacosConfigData {
@NacosConfig(dataId = "list_demo.json",group = "default_group")
private List<MyDemo> myDemoList;
}
Map泛型,接收JSON格式配置
@Component
public class NacosConfigData {
@NacosConfig(dataId = "map_demo.json",group = "default_group")
private Map<Long,MyDemo> myDemoMap;
}
@NacosConfigListener 注解用法介绍
- 此注解主要作用于方法上,在方法上进行声明,当配置发生变化时,会触发声明了此注解的方法,将最新的配置内容以入参方式传入。
自定义bean 接收最新配置
@Component
public class NacosConfigData {
@NacosConfig(dataId = "list_demo.json",group = "default_group")
private List<MyDemo> myDemoList;
@NacosConfigListener(dataId = "list_demo.json",groupId = "default_group")
private void myDemoListChanged(List<MyDemo> myDemoList){
this.myDemoList = myDemoList;
}
@NacosConfig(dataId = "map_demo.json",group = "default_group")
private Map<Long,MyDemo> myDemoMap;
@NacosConfigListener(dataId = "map_demo.json",groupId = "default_group")
private void myDemoMapChanged(List<MyDemo> myDemoList){
this.myDemoList = myDemoList;
}
}
@Component
public class NacosConfigData {
@NacosConfig(dataId = "list_demo.json",group = "default_group")
private List<MyDemo> myDemoList;
@NacosConfigListener(dataId = "list_demo.json",groupId = "default_group")
private void myDemoListChanged(List<MyDemo> myDemoList){
this.myDemoList = myDemoList;
}
@NacosConfig(dataId = "map_demo.json",group = "default_group")
private Map<Long,MyDemo> myDemoMap;
@NacosConfigListener(dataId = "map_demo.json",groupId = "default_group")
private void myDemoMapChanged(List<MyDemo> myDemoList){
this.myDemoList = myDemoList;
}
}
更为详实介绍,可以查看,参考来源:https://sca.aliyun.com/blog/sca-gvr7dx_awbbpb_xr9f0v45pxz9ubnu/?spm=5176.29160081.0.0.74805c721Hvyc4&source=blog/
用户点评