当前位置:网站首页>RPC中实现提供者信息变化后通知消费者
RPC中实现提供者信息变化后通知消费者
2022-07-24 08:33:00 【trigger333】
目录
Zookeeper的Watcher
使用Java可以链接Zookeeper并且进行节点的增删改查以及监听,目前有三套API可以使用。
这里用的是CuratorFramework。 这个client就是一个CuratorFramework的实现。

zk收到节点变更后就会执行 process方法。这样在process方法中执行消费者本地的缓存修改即可。为了解耦,采用了事件监听器。 在process中需要注意zk节点的消息通知是一次性的,所以在执行完process之后 需要重新监听该节点。
public void watchNodeDataChange(String newServerNodePath) {
zkClient.watchNodeData(newServerNodePath, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
String path = watchedEvent.getPath();
System.out.println("[watchNodeDataChange] 监听到zk节点下的" + path + "节点数据发生变更");
String nodeData = zkClient.getNodeData(path);
// 把 url字符串 转化为一个 providerNodeInfo
ProviderNodeInfo providerNodeInfo = URL.buildURLFromUrlStr(nodeData);
IRpcEvent iRpcEvent = new IRpcNodeChangeEvent(providerNodeInfo);
IRpcListenerLoader.sendEvent(iRpcEvent);
//因为zk节点的消息通知其实是只具有一次性的功效,所以
// 可能会出现第一次修改节点之后发送一次通知,
// 之后再次修改节点不再会发送节点变更通知操作。
ZookeeperRegister.this.watchNodeDataChange(newServerNodePath);
}
});
}消费者端的事件监听(观察者模式)
事件机制三大要素是事件源,事件监听器和事件对象,事件监听器注册到事件源上,如果事件源上发生了变化就会产生某些事件,会交给相应的事件监听器去处理。
public static void sendEvent(IRpcEvent iRpcEvent) {
if (CommonUtils.isEmptyList(iRpcListenerList)) {
//iRpcListenerList 里面有多种事件监听器
return;
}
for (IRpcListener<?> iRpcListener : iRpcListenerList) {
//通过 事件监听器的参数类型确定该事件应该倍哪一个事件监听器处理
Class<?> type = getInterfaceT(iRpcListener);
if (type.equals(iRpcEvent.getClass())) {
// 使用线程池进行异步事件处理
eventThreadPool.execute(() -> {
try {
iRpcListener.callBack(iRpcEvent.getData());
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
}
边栏推荐
- Web3≠NFT? A digital Renaissance?
- Look at the most influential infrastructure m-dao of Web3 through the current situation of Dao
- Upload and insert the execle table into the database based on ThinkPHP
- In 2022, how to choose cross end technology solutions?
- 情人节
- Use the bark app to realize the process of pushing messages to mobile phones
- Will Plato become the risk target of the meta universe? Platofarm has great opportunities
- Protocol buffer learning notes
- warning: could not execute support code to read Objective-C class data in the process.
- Digital collections are both outlets and risks!
猜你喜欢

Precautions for using kettle excel input

Brief notes on the key points of distributed system principle introduction

Use the bark app to realize the process of pushing messages to mobile phones

M-dao creates a one-stop Dao platform, allowing hundreds of millions of players to join Dao space

Web3≠NFT? A digital Renaissance?

Assembly | screen display numbers

Shanghai issued a document to support the entry of NFT cultural exchange: the trend of digital collections has arrived!

Look at the most influential infrastructure m-dao of Web3 through the current situation of Dao
![[wechat applet development] (I) development environment and applet official account application](/img/94/b93d5fb6d9e3515a1f218cc4ec6eef.png)
[wechat applet development] (I) development environment and applet official account application

国产“火箭心”人工心脏上市 不同人工心脏有什么区别?
随机推荐
JMX Console 未授权访问漏洞
Private traffic + apps, new opportunities for e-commerce drainage
2022.7.11全盘题解
Is it safe to open an account online in Beijing
Shanghai issued a document to support the entry of NFT cultural exchange: the trend of digital collections has arrived!
Arrays and linked lists
1、 Midwey addition, deletion, modification and query
[Linux] Oracle VirtualBox installation CentOS 8
Chinese brands in the historical process
Protocol buffer learning notes
「题解」蝙蝠侠的麻烦
You can't access this shared folder because your organization's security policies prevent unauthenticated guests from accessing it. These policies can help protect your computer from unsafe or malicio
Implementation of unity hub free version
Move protocol global health declaration, step into Web3 in sports
How difficult is it to build a digital collection platform?
3587. Connected graph (Jilin University postgraduate entrance examination machine test question)
Cososcreator upgrade gradle version
[MySQL] 08: aggregate function
【游戏合集】手机都要被塞爆了,6款优质Pygame游戏合集降临~(附源码)
Wechat applet file types and functions