zookeeper java的API 入门使用
作者:互联网
一.创建简单的maven项目导包
org.apache.zookeeper
zookeeper
3.6.3
二。编辑 代码
package com.wqj;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import java.util.concurrent.CountDownLatch;
/**
* Hello world!
*
*/
public class App {
//连接字符串。zk server zkConnectString地址和端口
private static String zkConnectString ="192.168.0.133:2181,192.168.0.134:2181,192.168.0.135:2181";
// 会话超时时间 单位ms
private static int sessionTimeout =3000;
//监视器对象
public static void main( String[] args ) throws Exception {
System.out.println( "Hello World!" );
//1.创建zk对象
/* //zk有session的概念,没有连接池的概念
//watch: 观察回调,回调。
//watch的注册值发生在 读类型调用
//第一类:new zk 时候,传入的watch,这个watch,session级别的,跟path 、node没有关系
ZooKeeper(String connectString, int sessionTimeout, Watcher watcher)
第一个参数: zookeeper服务器的地址
第二个参数: 超时时间一般为心跳时间的2倍
第三个参数: 该对象用来处理zookeeper服务器响应的事件
*/
//加锁同步执行
final CountDownLatch countDownLatch = new CountDownLatch(1);
final ZooKeeper zk = new ZooKeeper(zkConnectString, sessionTimeout, new Watcher() {
//默认watcher
/*
* 回调函数:
* 当我们需要监听的事件发生以后,那么zookeeper服务器就会调用该方法。
* 当前这个构造器中的该对象是用来响应所有的事件。
* */
@Override
public void process(WatchedEvent event) {
//获取数据KeeperStatus通知状态
Event.KeeperState state = event.getState();
//获取EventType事件类型
Event.EventType type = event.getType();
//获取地址
String path = event.getPath();
System.out.println("new zk path(创建 zk 连接获取地址:) " +path);
//判断数据类型 KeeperStatus通知状态
//switch (state) + 快捷键 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;
case Closed:
break;
}
switch (type) {
case None:
break;
case NodeCreated:
break;
case NodeDeleted:
break;
case NodeDataChanged:
break;
case NodeChildrenChanged:
break;
case DataWatchRemoved:
break;
case ChildWatchRemoved:
break;
case PersistentWatchRemoved:
break;
}
}
});
countDownLatch.await();
final ZooKeeper.States state = zk.getState();
switch (state) {
case CONNECTING:
System.out.println("CONNECTING---------------");
break;
case ASSOCIATING:
System.out.println("ASSOCIATING------------");
break;
case CONNECTED:
break;
case CONNECTEDREADONLY:
break;
case CLOSED:
break;
case AUTH_FAILED:
break;
case NOT_CONNECTED:
break;
}
/***
* final String path,
* byte[] data,
* List acl, 权限
* OPEN_ACL_UNSAFE : 完全开放的ACL,任何连接的客户端都可以操作该属性znode
* CREATOR_ALL_ACL : 只有创建者才有ACL权限
* READ_ACL_UNSAFE:只能读取ACL
* CreateMode createMode 节点类型 临时
*/
String pathName = zk.create("/wqj", "wqj_wqj".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
final Stat stat = new Stat();
byte[] node = zk.getData("/wqj", new Watcher() {
@Override
public void process(WatchedEvent event) {
System.out.println("getData watch:--------------"+event.toString());
// System.out.println("没有回调----");
try {
zk.getData("/wqj",this,stat);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},stat);
System.out.println("node---"+new String(node));
/** 触发回调
* final String path,
* byte[] data,
* int version 版本 ,每一次不同
*/
Stat stat01 = zk.setData("/wqj", "wqj_0112".getBytes(), 0);
//还会回调吗? 还会的
Stat stat02 = zk.setData("/wqj", "wqj_011".getBytes(), stat01.getVersion());
System.out.println("-------async start----------");
zk.getData("/wqj", false, new AsyncCallback.DataCallback() {
@Override
public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
System.out.println("-------async call back----------");
System.out.println(ctx.toString());
System.out.println(new String(data));
}
},"wqjj");
System.out.println("-------async over----------");
Thread.sleep(2222);
}
}
标签:case,java,zk,zookeeper,System,break,API,println,out 来源: https://www.cnblogs.com/wqj-grh/p/16584091.html