/**
*
*/
package com.ginger.zookeeper;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @Description: 获取节点数据
* @author 姜锋
* @date 2018年8月20日 下午5:04:28
* @version V1.0
*/
public class ZKGetNodeData implements Watcher {
final static Logger log = LoggerFactory.getLogger(ZKGetNodeData.class);
private ZooKeeper zooKeeper = null;
// 服务端地址
public static final String connectionString = "www.justginger.top:2181,www.justginger.top:2182,www.justginger.top:2183";
// session过期时间
public static final Integer sessionTimeout = 5000;
private static Stat stat = new Stat();
public ZKGetNodeData() {
}
/**
* @Description: TODO(这里用一句话描述这个方法的作用) @author 姜锋 @date 2018年8月20日 下午5:46:30 @param @param
* i @param @return @return CountDownLatch @throws
*/
public ZKGetNodeData(String connectionString) {
try {
zooKeeper = new ZooKeeper(connectionString, sessionTimeout, new ZKNodeOpteator());
} catch (Exception e) {
e.printStackTrace();
if (zooKeeper != null) {
try {
zooKeeper.close();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
private static CountDownLatch countDown = new CountDownLatch(1);
public static void main(String[] args) throws Exception {
ZKGetNodeData zkServer = new ZKGetNodeData(connectionString);
/**
* 参数: path:节点路径 watch:true或者false, 注册一个watch事件 stat : 状态
*/
byte[] resByte = zkServer.getZooKeeper().getData("/ginger", true, stat);
String result = new String(resByte);
System.out.println("此次查询结果" + result);
countDown.await();
}
@Override
public void process(WatchedEvent event) {
try {
if (event.getType() == EventType.NodeDataChanged) {
ZKGetNodeData zkServer = new ZKGetNodeData(connectionString);
byte[] resByte = zkServer.getZooKeeper().getData("/ginger", false, stat);
String result = new String(resByte);
System.out.println("更改后的值" + result);
System.out.println("更改后的版本号New Version: " + stat.getVersion());
countDown.countDown();
} else if (event.getType() == EventType.NodeCreated) {
} else if (event.getType() == EventType.NodeChildrenChanged) {
} else if (event.getType() == EventType.NodeDeleted) {
}
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* @return zooKeeper
*/
public ZooKeeper getZooKeeper() {
return zooKeeper;
}
/**
* @param zooKeeper 要设置的 zooKeeper
*/
public void setZooKeeper(ZooKeeper zooKeeper) {
this.zooKeeper = zooKeeper;
}
}控制台日志
18:31:43.208 [main] DEBUG org.apache.zookeeper.ClientCnxn - zookeeper.disableAutoWatchReset is false
18:31:43.607 [main-SendThread(www.justginger.top:2181)] INFO org.apache.zookeeper.ClientCnxn - Opening socket connection to server www.justginger.top/139.199.36.32:2181. Will not attempt to authenticate using SASL (unknown error)
18:31:43.618 [main-SendThread(www.justginger.top:2181)] INFO org.apache.zookeeper.ClientCnxn - Socket connection established to www.justginger.top/139.199.36.32:2181, initiating session
18:31:43.619 [main-SendThread(www.justginger.top:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Session establishment request sent on www.justginger.top/139.199.36.32:2181
18:31:43.640 [main-SendThread(www.justginger.top:2181)] INFO org.apache.zookeeper.ClientCnxn - Session establishment complete on server www.justginger.top/139.199.36.32:2181, sessionid = 0x100000ced1f000e, negotiated timeout = 5000
18:31:43.656 [main-SendThread(www.justginger.top:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x100000ced1f000e, packet:: clientPath:null serverPath:null finished:false header:: 1,4 replyHeader:: 1,4294967371,0 request:: '/ginger,T response:: #617364617364617364,s{4294967304,4294967369,1534647571665,1534761083862,6,2,0,0,9,0,4294967312}
此次查询结果asdasdasd
18:31:45.322 [main-SendThread(www.justginger.top:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x100000ced1f000e after 10ms
18:31:46.986 [main-SendThread(www.justginger.top:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x100000ced1f000e after 7ms
18:31:48.594 [main-SendThread(www.justginger.top:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got notification sessionid:0x100000ced1f000e
18:31:48.595 [main-SendThread(www.justginger.top:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got WatchedEvent state:SyncConnected type:NodeDataChanged path:/ginger for sessionid 0x100000ced1f000e
18:31:48.602 [main-SendThread(www.justginger.top:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x100000ced1f000e after 8ms
18:31:50.268 [main-SendThread(www.justginger.top:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x100000ced1f000e after 8ms
18:31:51.935 [main-SendThread(www.justginger.top:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x100000ced1f000e after 8ms