/**
* Hello world!
* watcher,用于监测值得变化等等
* callback,getdata 非阻塞回调 传说中的reative模式编程。使用异步回调,减少计算机阻塞空转
*/
public class App
{
public static void main( String[] args ) throws Exception
{
System.out.println( "Hello World!" );
//同步锁,解决异步的同步问题。zookeeper客户端时异步连接的,在获取连接后,countDown
CountDownLatch countDownLatch = new CountDownLatch(1);
//zk是每个链接是一个session,所以没有连接池的概念。不同节点 各自有各自的链接。
//session事件会决定,临时节点EPHEMERAL的生存事件,超过事件session消失,节点消失
ZooKeeper connected = new ZooKeeper(
"192.168.188.66:2181,192.168.188.67:2181,192.168.188.68:2181,192.168.188.69:2181", 3000, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
//watcher
Event.KeeperState state = watchedEvent.getState();
String path = watchedEvent.getPath();
Event.EventType type = watchedEvent.getType();
System.out.println("ZK default watch ························::" + watchedEvent);
//alt enter生成所有可能
switch (state) {
case Unknown:
break;
case Disconnected:
break;
case NoSyncConnected:
break;
case SyncConnected:
System.out.println("connected");
//获取到链接释放锁
countDownLatch.countDown();
break;
case AuthFailed:
break;
case ConnectedReadOnly:
break;
case SaslAuthenticated:
break;
case Expired:
break;
default:
}
switch (type) {
case None:
break;
case NodeCreated:
break;
case NodeDeleted:
break;
case NodeDataChanged:
break;
case NodeChildrenChanged:
break;
default:
}
}
});
//等待获取到链接
countDownLatch.await();
//获取zookeeper链接状态
ZooKeeper.States state = connected.getState();
switch (state) {
case CONNECTING:
System.out.println("正在链接。。");
break;
case ASSOCIATING:
break;
case CONNECTED:
System.out.println("链接完毕。。");
break;
case CONNECTEDREADONLY:
break;
case CLOSED:
break;
case AUTH_FAILED:
break;
case NOT_CONNECTED:
break;
default:
}
//创建节点
//EPHEMERAL 临时的,EPHEMERAL_SEQUENTIAL临时序列,PERSISTENT持久的,PERSISTENT_SEQUENTIAL持久序列的
//返回PathName
//create没有watch,因为create是创造事件的,watch用于读事件等
String pathName = connected.create("/tgntest", "hello my world".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
System.out.println("设值完毕:"+pathName);
//取数据,有watcher了把
Stat stat = new Stat();
byte[] data = connected.getData("/tgntest", new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
System.out.println("get data's watch----------------"+watchedEvent);
try {
//一直监控值是否被改变,true是default watch,就是zookeeper实例化时得watch
// connected.getData("/tgntest", true, stat);
//this,就是用的当前得watch
connected.getData("/tgntest", this, stat);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, stat);
System.out.println("取值完毕:"+new String(data));
//重新设值,让读操作watch回调执行:触发回调
Stat new_world = connected.setData("/tgntest", "new world".getBytes(), 0);
//看回调是否还会触发,这个version需要时新的版本号,传入new_world的版本号
Stat new_world2 = connected.setData("/tgntest", "new world2".getBytes(), new_world.getVersion());
System.out.println("重新赋值完毕!");
//非阻塞回调,取到数据
System.out.println("-------------------async start------------------------------");
connected.getData("/tgntest", false, new AsyncCallback.DataCallback() {
@Override
public void processResult(int i, String s, Object o, byte[] bytes, Stat stat) {
System.out.println("async callback");
System.out.println(new String(bytes));
}
},"context-abc");
System.out.println("-------------------async end------------------------------");
//模拟在客户端链接过程中,服务器挂掉。(这块阻塞着,然后把服务器停了)
//1、zookeeper watcher回调是session级别得,在服务器挂掉会回调
//2、如果Follower挂掉,会切换到其他得Follower上
//3、链接得session id不会变
Thread.sleep(99999);
}
}
Zookeeper简单api使用
最新推荐文章于 2022-07-03 19:52:53 发布