添加依赖
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
这里注意一下版本的问题
package com.itcast.zookeeper;
import org.apache.zookeeper.*;
import java.io.IOException;
public class ZKClient {
public static void main(String[] args) throws KeeperException, InterruptedException, IOException {
ZooKeeper zk = new ZooKeeper("192.168.13.138:2181",3000, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
System.out.println(watchedEvent.getState());
System.out.println(watchedEvent.getPath());
System.out.println(watchedEvent.getType());
}
});
#PERSISTENT 创建永久节点
zk.create("/myGirls","性感的".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
zk.close();
}
}
查看确实有myGirls节点,数据为“性感的”
[zk: localhost:2181(CONNECTED) 8] get /myGirls
性感的
cZxid = 0xd00000590
ctime = Mon Apr 01 00:08:26 CST 2019
mZxid = 0xd00000590
mtime = Mon Apr 01 00:08:26 CST 2019
pZxid = 0xd00000590
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 0
监听watcher回调:
package com.itcast.zookeeper;
import org.apache.zookeeper.*;
import java.io.IOException;
public class ZKClient {
public static void main(String[] args) throws KeeperException, InterruptedException, IOException {
ZooKeeper zk = new ZooKeeper("192.168.13.138:2181",3000, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
System.out.println(watchedEvent.getState());
System.out.println(watchedEvent.getPath());
System.out.println(watchedEvent.getType());
}
});
// zk.create("/myGirls","性感的".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
//开启监听,数据有变化,则出发客户端监听回调
zk.getData("/myGirls",true,null);
// -1代表我们不维护版本号,有服务器自己维护
zk.setData("/myGirls","大方的".getBytes(),-1);
//再次改变数据,不会触发数据监听
zk.setData("/myGirls","善良的".getBytes(),-1);
zk.close();
}
}
日志输出:
00:10:22.736 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:java.compiler=<NA>
00:10:22.736 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:os.name=Windows 7
00:10:22.737 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:os.arch=amd64
00:10:22.737 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:os.version=6.1
00:10:22.737 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:user.name=nmj
00:10:22.737 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:user.home=C:\Users\nmj
00:10:22.737 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:user.dir=D:\IntelIJWorkSpace\springbootdemo\springbootdemo
00:10:22.740 [main] INFO org.apache.zookeeper.ZooKeeper - Initiating client connection, connectString=192.168.13.138:2181 sessionTimeout=3000 watcher=com.itcast.zookeeper.ZKClient$1@f2a0b8e
00:10:22.748 [main] DEBUG org.apache.zookeeper.ClientCnxn - zookeeper.disableAutoWatchReset is false
00:10:23.332 [main-SendThread(192.168.13.138:2181)] INFO org.apache.zookeeper.ClientCnxn - Opening socket connection to server 192.168.13.138/192.168.13.138:2181. Will not attempt to authenticate using SASL (unknown error)
00:10:23.336 [main-SendThread(192.168.13.138:2181)] INFO org.apache.zookeeper.ClientCnxn - Socket connection established to 192.168.13.138/192.168.13.138:2181, initiating session
00:10:23.340 [main-SendThread(192.168.13.138:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Session establishment request sent on 192.168.13.138/192.168.13.138:2181
00:10:23.354 [main-SendThread(192.168.13.138:2181)] INFO org.apache.zookeeper.ClientCnxn - Session establishment complete on server 192.168.13.138/192.168.13.138:2181, sessionid = 0x169d46efbb30007, negotiated timeout = 4000
#程序起来,客户端和服务器建链
SyncConnected
null
None
00:10:23.371 [main-SendThread(192.168.13.138:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x169d46efbb30007, packet:: clientPath:null serverPath:null finished:false header:: 1,4 replyHeader:: 1,55834576274,0 request:: '/myGirls,T response:: #ffffffe6ffffff80ffffffa7ffffffe6ffffff84ffffff9fffffffe7ffffff9affffff84,s{55834576272,55834576272,1554048506293,1554048506293,0,0,0,0,9,0,55834576272}
00:10:23.384 [main-SendThread(192.168.13.138:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got notification sessionid:0x169d46efbb30007
00:10:23.386 [main-SendThread(192.168.13.138:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got WatchedEvent state:SyncConnected type:NodeDataChanged path:/myGirls for sessionid 0x169d46efbb30007
#mgGirls数据发生变化,则出发watch回调函数
SyncConnected
/myGirls
NodeDataChanged
00:10:23.388 [main-SendThread(192.168.13.138:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x169d46efbb30007, packet:: clientPath:null serverPath:null finished:false header:: 2,5 replyHeader:: 2,55834576275,0 request:: '/myGirls,#ffffffe5ffffffa4ffffffa7ffffffe6ffffff96ffffffb9ffffffe7ffffff9affffff84,-1 response:: s{55834576272,55834576275,1554048506293,1554048622434,1,0,0,0,9,0,55834576272}
00:10:23.396 [main-SendThread(192.168.13.138:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x169d46efbb30007, packet:: clientPath:null serverPath:null finished:false header:: 3,5 replyHeader:: 3,55834576276,0 request:: '/myGirls,#ffffffe5ffffff96ffffff84ffffffe8ffffff89ffffffafffffffe7ffffff9affffff84,-1 response:: s{55834576272,55834576276,1554048506293,1554048622449,2,0,0,0,9,0,55834576272}
00:10:23.396 [main] DEBUG org.apache.zookeeper.ZooKeeper - Closing session: 0x169d46efbb30007
00:10:23.397 [main] DEBUG org.apache.zookeeper.ClientCnxn - Closing client for session: 0x169d46efbb30007
00:10:23.401 [main-SendThread(192.168.13.138:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x169d46efbb30007, packet:: clientPath:null serverPath:null finished:false header:: 4,-11 replyHeader:: 4,55834576277,0 request:: null response:: null
00:10:23.402 [main] DEBUG org.apache.zookeeper.ClientCnxn - Disconnecting client for session: 0x169d46efbb30007
00:10:23.402 [main] INFO org.apache.zookeeper.ZooKeeper - Session: 0x169d46efbb30007 closed
//获取子节点数据个数变化
List<String> children = zk.getChildren("/hellozk", true);
for (String s: children){
System.out.println(s);
}
00:49:06.060 [main-SendThread(192.168.13.138:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x169d46efbb30009, packet:: clientPath:null serverPath:null finished:false header:: 4,8 replyHeader:: 4,55834576284,0 request:: '/hellozk,T response:: v{'a,'c0000000003,'b0000000001,'c0000000002}
a
c0000000003
b0000000001
c0000000002
发现一样
[zk: localhost:2181(CONNECTED) 10] ls2 /hellozk
[a, c0000000003, b0000000001, c0000000002]
cZxid = 0xd00000562
ctime = Sun Mar 31 22:35:14 CST 2019
mZxid = 0xd00000578
mtime = Sun Mar 31 23:03:44 CST 2019
pZxid = 0xd00000575
cversion = 4
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 4
Stat exists = zk.exists("/hellozk", true);
System.out.println("/hellozk:exites:");
发现确实有这个节点
/hellozk:exites:55834576226,55834576248,1554042914373,1554044624298,1,4,0,0,3,4,55834576245