目录
一、 概述
二、搭配Nacos配置Josn数据
1. bootstrap.yml
2. nacos配置-测试案例
3. 配置详情
4. NacosConfig 组装配置中心DataId
5. 底层实现过程
6. NacosConfigLocalCatch 核心监听触发类
7. User 用于配置映射的数据实体
8. 测试使用
如果发现本文有错误的地方,请大家毫不吝啬,多多指教,欢迎大家评论,谢谢!
在我业务开发中,需要在配置中心配置Json数据,返回给前端。因Nacos默认不支持Json格式配置,需要搭配监听器获取配置中心Json数据,返回给客户端。
server:
port: 9000
spring:
application:
name: nacos-demo
cloud:
## 注册中心地址
discovery:
server-addr: 127.0.0.1:8848
nacos:
config:
#nacos地址
server-addr: 127.0.0.1:8848
#命名空间
namespace: 096ad119-e2e0-4371-94c9-430b01102af2
#配置文件类型,目前只支持yaml,yml,properties,目前不需要了
file-extension: yaml
#默认提供者,目前不需要了
refresh-enabled: true
enabled: true
group: NACOS_DEMO
image.png
具体使用 只需要 在 NacosConfig 中nacosConfigLocalCacheInfoMap方法添加对应配置映射即可,如下
@Configuration
public class NacosConfig {
/**
* 前提条件 要求 serverAddr、namespace、group使用bootstrap.yml 中 spring cloud nacos config 的。
* @return
*/
@Bean
public Map nacosConfigLocalCacheInfoMap() {
// key:dataId, value:对应数据类型
Map map = new HashMap<>();
map.put("testText", String.class);
map.put("testJson1", List.class);
map.put("testJson2", User.class);
map.put("testInteger", Integer.class);
return map;
}
}
/**
* nacos 配置 信息
*/
public class NacosConfigInfo {
public NacosConfigInfo(String serverAddr, String namespace, String group, String dataId, boolean refresh, Class cls) {
this.serverAddr = serverAddr;
this.namespace = namespace;
this.group = group;
this.dataId = dataId;
this.refresh = refresh;
this.cls = cls;
}
public NacosConfigInfo() {
}
private String serverAddr;
private String namespace;
private String group;
private String dataId;
private boolean refresh = true;
private Class cls = String.class;
public String getServerAddr() {
return serverAddr;
}
public void setServerAddr(String serverAddr) {
this.serverAddr = serverAddr;
}
public String getNamespace() {
return namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getDataId() {
return dataId;
}
public void setDataId(String dataId) {
this.dataId = dataId;
}
public boolean isRefresh() {
return refresh;
}
public void setRefresh(boolean refresh) {
this.refresh = refresh;
}
public Class getCls() {
return cls;
}
public void setCls(Class cls) {
this.cls = cls;
}
public long getTimeout() {
return 5000L;
}
}
@Component
public class NacosConfigLocalCatch implements InitializingBean {
private Map localCatchMap = new HashMap<>();
@Resource(name = "nacosConfigLocalCacheInfoMap")
private Map nacosConfigLocalCacheInfoMap;
@Autowired
private NacosConfigProperties nacosConfigProperties;
private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
2, 4, 1, TimeUnit.SECONDS, new LinkedBlockingDeque<>(100),
new ThreadPoolExecutor.CallerRunsPolicy()
);
protected final Logger logger = LoggerFactory.getLogger(getClass());
protected final String clazzSimpleName = getClass().getSimpleName();
@Override
public void afterPropertiesSet() throws Exception {
nacosConfigLocalCacheInfoMap.forEach((k, v) -> {
NacosConfigInfo nacosConfigInfo = new NacosConfigInfo(nacosConfigProperties.getServerAddr(),
nacosConfigProperties.getNamespace(), nacosConfigProperties.getGroup(),
k, true, nacosConfigLocalCacheInfoMap.get(k));
this.listener(nacosConfigInfo);
});
}
public void listener(NacosConfigInfo nacosConfigInfo) {
Listener listener = new Listener() {
@Override
public Executor getExecutor() {
return threadPoolExecutor;
}
@Override
public void receiveConfigInfo(String configInfo) {
logger.info("{}#receiveConfigInfo receive configInfo. configInfo={}", clazzSimpleName, configInfo);
compile(configInfo, nacosConfigInfo);
}
};
ConfigService configService = this.getConfigService(nacosConfigInfo);
String config = null;
try {
config = configService.getConfig(nacosConfigInfo.getDataId(), nacosConfigInfo.getGroup(), nacosConfigInfo.getTimeout());
logger.info("{}#afterPropertiesSet init configInfo. configInfo={}", clazzSimpleName, config);
// 初始化
compile(config, nacosConfigInfo);
// 监听
configService.addListener(nacosConfigInfo.getDataId(), nacosConfigInfo.getGroup(), listener);
} catch (NacosException e) {
e.printStackTrace();
throw new RuntimeException("nacos server 监听 异常! dataId = " + nacosConfigInfo.getDataId());
}
}
private void compile(String config, NacosConfigInfo nacosConfigInfo) {
Object initValue = JSON.parseObject(config, nacosConfigInfo.getCls());
localCatchMap.put(nacosConfigInfo.getDataId(), initValue);
}
/**
* 获取ConfigService
*
* @return
*/
private ConfigService getConfigService(NacosConfigInfo nacosConfigInfo) {
String serverAddr = nacosConfigInfo.getServerAddr();
String nameSpace = nacosConfigInfo.getNamespace();
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
properties.put(PropertyKeyConst.NAMESPACE, nameSpace);
ConfigService configService;
try {
configService = NacosFactory.createConfigService(properties);
} catch (NacosException e) {
e.printStackTrace();
throw new RuntimeException("Nacos config 配置 异常");
}
return configService;
}
public T get(String dataId, Class cls) {
if (cls != nacosConfigLocalCacheInfoMap.get(dataId)) {
throw new IllegalArgumentException("类型异常");
}
return (T) localCatchMap.get(dataId);
}
}
@Data
public class User {
private String name;
private String phone;
private Integer age;
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("User{");
sb.append("name='").append(name).append('\'');
sb.append(", phone='").append(phone).append('\'');
sb.append(", age=").append(age);
sb.append('}');
return sb.toString();
}
}
@Autowired
private NacosConfigLocalCatch nacosConfigLocalCatch;
@RequestMapping("/test2")
public String test2() {
logger.info("-------------test2---------------");
String testText = nacosConfigLocalCatch.get("testText", String.class);
List testJson1 = nacosConfigLocalCatch.get("testJson1", List.class);
User testJson2 = nacosConfigLocalCatch.get("testJson2", User.class);
Integer testInteger = nacosConfigLocalCatch.get("testInteger", Integer.class);
return testText+" , "+testJson1+" , "+testJson2+" , "+testInteger;
}
测试结果
实时修改,不停服更新配置
再看
nacos config 配置中心 至此结束!
源码地址:
Nacos配置代码: feign-demo→order-service-nacos
https://gitee.com/gaibianzlp/springcould-alibaba-example.git
点关注不迷路,觉得对你有帮助请给一个赞或者长按一键三连,谢谢