当前位置:网站首页>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);
}
}
总结
边栏推荐
- CKAN教程之在 AWS 上部署 CKAN 应用程序
- 用了 TCP 协议,数据一定不会丢吗?
- Numpy数组中d[True]=1的含义
- 程序员常说的“左手锟斤拷,右手烫烫烫”是怎么回事?
- VMware workstation program starts slowly
- Speech Synthesis Model Cheat Sheet (1)
- 【问题征集】向 iPod 之父、iPhone 联合设计者、Google Nest 创始人 Tony Fadell 提问啦
- What is the matter that programmers often say "the left hand is knuckled and the right hand is hot"?
- Database auditing - an essential part of network security
- flutter空安全问题,平时用到的数据一定要注意
猜你喜欢
精心整理16条MySQL使用规范,减少80%问题,推荐分享给团队
Visual Studio中vim模拟器
Jenkins汉化设置
APT level comprehensive free kill with Shell
语音合成模型小抄(1)
3、Xendesktop更改发布桌面的显示名称(MCS静态桌面)
分库分表索引设计:二级索引、全局索引的最佳设计实践
九零后程序员心声:互联网的同行们,别卷了,再卷人都卷没了
Technology Sharing | How to do assertion verification for xml format in interface automation testing?
公司招个程序员,34岁以上两年一跳的不要,开出工资以为看错了
随机推荐
Connect the Snowflake of CKAN tutorial CKAN to release to open data portal
UE5 官方案例Lyra 全特性详解 8.如何用配置表初始化角色数据
TensorFlow学习记录(一):基本介绍
APT level comprehensive free kill with Shell
程序员如何优雅地解决线上问题?
用了 TCP 协议,数据一定不会丢吗?
主流定时任务解决方案全横评
典型相关分析CCA计算过程
十年架构五年生活-04第一个工作转折点
3、Xendesktop更改发布桌面的显示名称(MCS静态桌面)
谷歌 Chrome 浏览器 104 正式版发布:加快网页加载,蓝牙 API 改进
优秀论文以及思路分析01
Test | ali internship 90 days in life: from the perspective of interns, talk about personal growth
Find My技术|智能防丢还得看苹果Find My技术
我们来浅谈代码语言的魅力
I have been in the software testing industry for nearly 20 years, let me talk to you about today's software testing
The latest real software test interview questions are shared. Are you afraid that you will not be able to enter the big factory after collecting them?
十年架构五年生活-03作为技术组长的困扰
九零后程序员心声:互联网的同行们,别卷了,再卷人都卷没了
程序员的七夕浪漫时刻