当前位置:网站首页>Nacos配置中心之事件订阅
Nacos配置中心之事件订阅
2022-08-02 23:40:00 【InfoQ】
Nacos配置中心之事件订阅
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
// many Spring context
if (this.ready.compareAndSet(false, true)) {
this.registerNacosListenersForApplications();
}
}
private void registerNacosListenersForApplications() {
if (refreshProperties.isEnabled()) {
for (NacosPropertySource nacosPropertySource : NacosPropertySourceRepository
.getAll()) {
if (!nacosPropertySource.isRefreshable()) {
continue;
}
String dataId = nacosPropertySource.getDataId();
registerNacosListener(nacosPropertySource.getGroup(), dataId);
}
}
}
- 获取dataId
- 如果属性源不支持刷新就跳过,支持就调用registerNacosListener()方法,我们看一下这个方法
registerNacosListener()方法
private void registerNacosListener(final String group, final String dataId) {
Listener listener = listenerMap.computeIfAbsent(dataId, i -> new Listener() {
@Override
public void receiveConfigInfo(String configInfo) {
refreshCountIncrement();
String md5 = "";
if (!StringUtils.isEmpty(configInfo)) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md5 = new BigInteger(1, md.digest(configInfo.getBytes("UTF-8")))
.toString(16);
}
catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
log.warn("[Nacos] unable to get md5 for dataId: " + dataId, e);
}
}
refreshHistory.add(dataId, md5);
applicationContext.publishEvent(
new RefreshEvent(this, null, "Refresh Nacos config"));
if (log.isDebugEnabled()) {
log.debug("Refresh Nacos config group " + group + ",dataId" + dataId);
}
}
@Override
public Executor getExecutor() {
return null;
}
});
try {
configService.addListener(dataId, group, listener);
}
catch (NacosException e) {
e.printStackTrace();
}
}
- 创建监听器添加到listenerMap集合中,监听器中主要刷新个数,添加dataId和md5到监听历史中,最后发布监听事件
- 把监听器在添加到configService中,这篇文章我们有介绍这个类,它是Nacos客户端提供的用于访问实现配置中心基本操作的类
事件监听
public class RefreshEventListener implements SmartApplicationListener {
private ContextRefresher refresh;
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationReadyEvent) {
this.handle((ApplicationReadyEvent)event);
} else if (event instanceof RefreshEvent) {
this.handle((RefreshEvent)event);
}
}
public void handle(RefreshEvent event) {
if (this.ready.get()) {
log.debug("Event received " + event.getEventDesc());
Set<String> keys = this.refresh.refresh();
log.info("Refresh keys changed: " + keys);
}
}
总结
边栏推荐
- 解决错误:Optional int parameter ‘pageSize‘ is present but cannot be translated into a null value due to
- 数据库审计 - 网络安全的重要组成部分
- Speech Synthesis Model Cheat Sheet (1)
- 同一份数据,Redis为什么要存两次?
- NLP常用Backbone模型小抄(1)
- 九零后程序员心声:互联网的同行们,别卷了,再卷人都卷没了
- Connect the Snowflake of CKAN tutorial CKAN to release to open data portal
- 2022杭电多校第一场(K/L/B/C)
- Apache Doris 1.1 特性揭秘:Flink 实时写入如何兼顾高吞吐和低延时
- Strict feedback nonlinear systems based on event trigger preset since the immunity of finite time tracking control
猜你喜欢

剑指 Offer 21. 调整数组顺序使奇数位于偶数前面

基于rt-thread studio的STM32裸机开发——LED

解决错误:Optional int parameter ‘pageSize‘ is present but cannot be translated into a null value due to

CAS:1445723-73-8,DSPE-PEG-NHS,磷脂-聚乙二醇-活性酯两亲性脂质PEG共轭物

数据库主键一定要自增吗?有哪些场景不建议自增?

Heartwarming AI Review (1)

谷歌 Chrome 浏览器 104 正式版发布:加快网页加载,蓝牙 API 改进

关于地图GIS开发事项的一次实践整理(上)

NLP commonly used Backbone model cheat sheet (1)

为了面试阿里,熬夜肝完这份软件测试笔记后,Offer终于到手了
随机推荐
vant-swipe adaptive picture height + picture preview
What is the matter that programmers often say "the left hand is knuckled and the right hand is hot"?
2022暑假牛客多校1 (A/G/D/I)
resubmit 渐进式防重复提交框架简介
I have been in the software testing industry for nearly 20 years, let me talk to you about today's software testing
【QT】自定义工程封装成DLL并如何调用(带ui界面的)
科研用Cholesterol-PEG-NHS,NHS-PEG-CLS,胆固醇-聚乙二醇-活性酯
程序员的七夕浪漫时刻
用了 TCP 协议,数据一定不会丢吗?
Controller层代码这么写,简洁又优雅!
Cholesterol-PEG-Acid,胆固醇-聚乙二醇-羧基保持在干燥、低温环境下
Pytest配置项-pytest.ini
秒懂网络拓扑中的下一跳地址
Rasa 3.x study series - Rasa - Issues 4792 socket debug logs clog up debug feed study notes
Rasa 3.x 学习系列- Rasa - Issues 4792 socket debug logs clog up debug feed学习笔记
程序员英语自我介绍
LVM与磁盘配额原理及配置
D experimental new anomaly
DataGuard日常维护常见问题之数据同步异常
为了面试阿里,熬夜肝完这份软件测试笔记后,Offer终于到手了