zookeeper随笔

Zookeeper 依托zab协议,实现分部式的“管理者”,下面记录一些使用摘要

1、节点

     根是  /  ,整个数据结构呈现树形

     每个节点可以有子节点。可以有数据。 并还有状态信息。

     

czxid
The zxid of the change that caused this znode to be created.
创建次节点的事物ID
mzxid
The zxid of the change that last modified this znode.
最后修改次节点内容的事物ID
ctime
The time in milliseconds from epoch when this znode was created.
创建时间
mtime
The time in milliseconds from epoch when this znode was last modified.
最后修改时间,毫秒
version
The number of changes to the data of this znode.
数据的当前版本
cversion
The number of changes to the children of this znode.
子节点的当前版本
aversion
The number of changes to the ACL of this znode.

ephemeralOwner
The session id of the owner of this znode if the znode is an ephemeral node. If it is not an ephemeral node, it will be zero.
临时节点的sessionId,如果是持久节点,则为0
dataLength
The length of the data field of this znode.

numChildren
The number of children of this znode.

 

 

     创建节点时,可以有4种。 临时/持久  *   序列/非  

2、 事件持久监听

     创建节点 ADDED、 修改数据UPDATED 、 创建子节点 CHILD_ADDED 、删除子节点 CHILD_REMOVED 、修改子节点数据 CHILD_UPDATED; 都会触发事件。

如果使用 curator-framework 框架作为zk的客户端工具,那么可以使用 PathChildrenCache 来监听 子节点上的所有变化,包含结构和数据的。

eg (任何对本身node 节点的操作不会触发下面监听,如 修改数据,删除node 自身节点 ):

PathChildrenCache pcc = new PathChildrenCache(client, "/t", true);
pcc.getListenable().addListener(new PathChildrenCacheListener() {
	public void childEvent(CuratorFramework zkClient, PathChildrenCacheEvent event) throws Exception {
		System.out.println("gun::"+event.toString());
	}
});

 如果要监听 node 自身的变化,那么需要用 NodeCache(/t 的创建、修改值 会触发下面的监听器,但是任何关于子节点的操作,不会触发):

NodeCache nc = new NodeCache(client, "/t");
nc.getListenable().addListener(new NodeCacheListener() {
	public void nodeChanged() throws Exception {
		System.out.println("any event occured ...");
	}
});

 

 3、一次性事件监听

       可以通过 checkExsits 方法进行一次性监听,当节点被 create ,  set ,   delete 时,会有事件触发(NodeDataChanged  , NodeCreated,  NodeDeleted ):

client.checkExists().usingWatcher(new CuratorWatcher() {
public void process(WatchedEvent event) throws Exception {
	System.out.println("exsistEvent====\n"+event.toString());
}
}).forPath("/t/c");

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值