一、创建会话

1. 创建一个基本的ZooKeeper会话实例

package com.huey.dream.demo;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.KeeperState; /**
* 创建一个基本的ZooKeeper会话实例
* @author huey
* @version 1.0
* @created 2015-3-1
*/
public class ZKDemo { public static void main(String[] args) throws Exception {
final CountDownLatch connectedSignal = new CountDownLatch(1); /**
* ZooKeeper客户端和服务器会话的建立是一个异步的过程
* 构造函数在处理完客户端的初始化工作后立即返回,在大多数情况下,并没有真正地建立好会话
* 当会话真正创建完毕后,Zookeeper服务器会向客户端发送一个事件通知
*/
ZooKeeper zk = new ZooKeeper("192.168.1.109:2181", 5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
connectedSignal.countDown();
}
}
});
System.out.println("State1: " + zk.getState()); // CONNECTING connectedSignal.await();
System.out.println("State2: " + zk.getState()); // CONNECTED zk.close();
System.out.println("State3: " + zk.getState()); // CLOSED
} }

2. 创建一个复用sessionId和sessionPasswd的Zookeeper对象示例

package com.huey.dream.demo;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.KeeperState; /**
* 创建一个复用sessionId和sessionPasswd的ZooKeeper对象示例
* @author huey
* @version 1.0
* @created 2015-3-1
*/
public class ZKDemo { public static void main(String[] args) throws Exception { final CountDownLatch connectedSignal = new CountDownLatch(1);
ZooKeeper zk = new ZooKeeper("192.168.1.109:2181", 5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
connectedSignal.countDown();
}
}
}); connectedSignal.await();
long sessionId = zk.getSessionId();
byte[] passwd = zk.getSessionPasswd();
zk.close(); final CountDownLatch anotherConnectedSignal = new CountDownLatch(1);
ZooKeeper newZk = new ZooKeeper(
"192.168.1.109:2181",
5000,
new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
anotherConnectedSignal.countDown();
}
}
},
sessionId,
passwd); anotherConnectedSignal.await();
newZk.close();
} }

二、创建节点

1. 使用同步API创建一个ZNode节点

package com.huey.dream.demo;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids; /**
* 使用同步API创建一个ZNode节点
* @author huey
* @version 1.0
* @created 2015-3-1
*/
public class ZKDemo { public static void main(String[] args) throws Exception { final CountDownLatch connectedSignal = new CountDownLatch(1);
ZooKeeper zk = new ZooKeeper("192.168.1.109:2181", 5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
connectedSignal.countDown();
}
}
});
connectedSignal.await(); String path = zk.create(
"/zk-huey",
"hello".getBytes(),
Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT
);
System.out.println(path + " is created."); zk.close();
} }

2. 使用异步API创建一个ZNode节点

package com.huey.dream.demo;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids; /**
* 使用异步API创建一个ZNode节点
* @author huey
* @version 1.0
* @created 2015-3-1
*/
public class ZKDemo { public static void main(String[] args) throws Exception { final CountDownLatch connectedSignal = new CountDownLatch(1);
ZooKeeper zk = new ZooKeeper("192.168.1.109:2181", 5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
connectedSignal.countDown();
}
}
});
connectedSignal.await(); zk.create(
"/zk-huey",
"hello".getBytes(),
Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT_SEQUENTIAL,
new AsyncCallback.StringCallback() {
@Override
public void processResult(int rc, String path, Object ctx, String name) {
// 服务器响应吗
System.out.println("ResultCode: " + rc);
// 接口调用时传入API的数据节点的路径参数值
System.out.println("Znode: " + path);
// 接口调用时传入API的ctx参数值
System.out.println("Context: " + (String)ctx);
// 实际在服务端创建的节点名
System.out.println("Real Path: " + name);
}
},
"The Context"
); zk.close();
} }

三、删除节点

1. 使用同API删除一个ZNode节点

package com.huey.dream.demo;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.KeeperState; /**
* 使用同步API删除一个ZNode节点
* @author huey
* @version 1.0
* @created 2015-3-1
*/
public class ZKDemo { public static void main(String[] args) throws Exception { final CountDownLatch connectedSignal = new CountDownLatch(1);
ZooKeeper zk = new ZooKeeper("192.168.1.109:2181", 5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
connectedSignal.countDown();
}
}
});
connectedSignal.await(); zk.delete("/zk-huey", -1); zk.close();
} }

2. 使用异步API删除一个ZNode节点

package com.huey.dream.demo;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.KeeperState; /**
* 使用异步API删除一个ZNode节点
* @author huey
* @version 1.0
* @created 2015-3-1
*/
public class ZKDemo { public static void main(String[] args) throws Exception { final CountDownLatch connectedSignal = new CountDownLatch(1);
ZooKeeper zk = new ZooKeeper("192.168.1.109:2181", 5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
connectedSignal.countDown();
}
}
});
connectedSignal.await(); zk.delete(
"/zk-huey",
-1,
new AsyncCallback.VoidCallback() {
@Override
public void processResult(int rc, String path, Object ctx) {
System.out.println("ResultCode: " + rc);
System.out.println("Znode: " + path);
System.out.println("Context: " + (String)ctx);
}
},
"The Context"
); zk.close();
} }

四、获取子节点列表

1. 使用同步API获取子节点列表

package com.huey.dream.demo;

import java.util.List;
import java.util.concurrent.CountDownLatch; import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.data.Stat; /**
* 使用同步API获取子节点列表
* @author huey
* @version 1.0
* @created 2015-3-1
*/
public class ZKDemo { public static void main(String[] args) throws Exception { final CountDownLatch connectedSignal = new CountDownLatch(1);
final ZooKeeper zk = new ZooKeeper("192.168.1.109:2182", 5000, null);
zk.register(new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
if (event.getType() == EventType.None
&& event.getPath() == null) {
connectedSignal.countDown();
} else if (event.getType() == EventType.NodeChildrenChanged) {
try {
System.out.println("NodeChildrenChanged.");
List<String> children = zk.getChildren(event.getPath(), true);
System.out.println("Children: " + children);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
connectedSignal.await(); zk.create("/zk-huey", "root".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create("/zk-huey/node1", "node1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create("/zk-huey/node2", "node2".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); Stat stat = new Stat();
List<String> children = zk.getChildren("/zk-huey",
true, // 注册默认的Watcher,当子节点类别发送变更的话,向客户端发送通知
stat // 用于描述节点状态信息
);
System.out.println("Stat: " + stat);
System.out.println("Children: " + children); zk.create("/zk-huey/node3", "node3".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); Thread.sleep(10 * 1000);
zk.close();
} }

结果输出:

Stat: 4294967436,4294967436,1425201236879,1425201236879,0,2,0,0,4,2,4294967438

Children: [node2, node1]
NodeChildrenChanged.
State: CONNECTED
Children: [node2, node3, node1]

2. 使用异步API获取子节点列表

package com.huey.dream.demo;

import java.util.List;
import java.util.concurrent.CountDownLatch; import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.data.Stat; /**
* 使用异步API获取子节点列表
* @author huey
* @version 1.0
* @created 2015-3-1
*/
public class ZKDemo { public static void main(String[] args) throws Exception { final CountDownLatch connectedSignal = new CountDownLatch(1);
final ZooKeeper zk = new ZooKeeper("192.168.1.109:2182", 5000, null);
zk.register(new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
if (event.getType() == EventType.None
&& event.getPath() == null) {
connectedSignal.countDown();
} else if (event.getType() == EventType.NodeChildrenChanged) {
try {
System.out.println("NodeChildrenChanged.");
List<String> children = zk.getChildren(event.getPath(), true);
System.out.println("Children: " + children);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
connectedSignal.await(); zk.create("/zk-huey", "root".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create("/zk-huey/node1", "node1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create("/zk-huey/node2", "node2".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.getChildren("/zk-huey",
true,
new AsyncCallback.Children2Callback() {
@Override
public void processResult(int rc, String path, Object ctx,
List<String> children, Stat stat) {
System.out.println("ResultCode: " + rc);
System.out.println("ZNode: " + path);
System.out.println("Context: " + ctx);
System.out.println("Children: " + children);
System.out.println("Stat: " + stat);
}
},
"The Context"
); zk.create("/zk-huey/node3", "node3".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); Thread.sleep(10 * 1000);
zk.close();
} }

五、获取节点数据内容

1. 使用同步API获取节点数据内容

package com.huey.dream.demo;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids; /**
* 使用同步API获取节点数据内容
* @author huey
* @version 1.0
* @created 2015-3-1
*/
public class ZKDemo { public static void main(String[] args) throws Exception { final CountDownLatch connectedSignal = new CountDownLatch(1);
final ZooKeeper zk = new ZooKeeper("192.168.1.109:2182", 5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
if (event.getType() == EventType.None
&& event.getPath() == null) {
connectedSignal.countDown();
}
}
}
});
connectedSignal.await(); zk.create("/zk-huey", "hello".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
byte[] nodeData = zk.getData("/zk-huey", false, null);
System.out.println("NodeData: " + new String(nodeData)); zk.close();
} }

2. 使用异步API获取节点数据内容

package com.huey.dream.demo;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.data.Stat; /**
* 使用异步API获取节点数据内容
* @author huey
* @version 1.0
* @created 2015-3-1
*/
public class ZKDemo { public static void main(String[] args) throws Exception { final CountDownLatch connectedSignal = new CountDownLatch(1);
final ZooKeeper zk = new ZooKeeper("192.168.1.109:2182", 5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
if (event.getType() == EventType.None
&& event.getPath() == null) {
connectedSignal.countDown();
}
}
}
});
connectedSignal.await(); zk.create("/zk-huey", "hello".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.getData(
"/zk-huey",
false,
new AsyncCallback.DataCallback() {
@Override
public void processResult(int rc, String path, Object ctx, byte[] data,
Stat stat) {
System.out.println("ResultCode: " + rc);
System.out.println("ZNode: " + path);
System.out.println("Context: " + ctx);
System.out.println("NodeData: " + new String(data));
System.out.println("Stat: " + stat);
}
},
"The Context"
); zk.close();
} }

六、更新数据

1. 使用同步API更新节点数据内容

package com.huey.dream.demo;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids; /**
* 使用同步API更新节点数据内容
* @author huey
* @version 1.0
* @created 2015-3-1
*/
public class ZKDemo { public static void main(String[] args) throws Exception { final CountDownLatch connectedSignal = new CountDownLatch(1);
final ZooKeeper zk = new ZooKeeper("192.168.1.109:2182", 5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
if (event.getType() == EventType.None
&& event.getPath() == null) {
connectedSignal.countDown();
}
}
}
});
connectedSignal.await(); zk.create("/zk-huey", "hello".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
byte[] nodeData = zk.getData("/zk-huey", false, null);
System.out.println("NodeData: " + nodeData); zk.setData("/zk-huey", "world".getBytes(), -1);
nodeData = zk.getData("/zk-huey", false, null);
System.out.println("NodeData: " + nodeData); zk.close();
} }

2. 使用异步API更新节点数据内容

package com.huey.dream.demo;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.data.Stat; /**
* 使用异步API更新节点数据内容
* @author huey
* @version 1.0
* @created 2015-3-1
*/
public class ZKDemo { public static void main(String[] args) throws Exception { final CountDownLatch connectedSignal = new CountDownLatch(1);
final ZooKeeper zk = new ZooKeeper("192.168.1.109:2182", 5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
if (event.getType() == EventType.None
&& event.getPath() == null) {
connectedSignal.countDown();
}
}
}
});
connectedSignal.await(); zk.create("/zk-huey", "hello".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
byte[] nodeData = zk.getData("/zk-huey", false, null);
System.out.println("NodeData: " + nodeData); zk.setData(
"/zk-huey",
"world".getBytes(),
-1,
new AsyncCallback.StatCallback() {
@Override
public void processResult(int rc, String path, Object ctx, Stat stat) {
System.out.println("ResultCode: " + rc);
System.out.println("ZNode: " + path);
System.out.println("Context: " + ctx);
System.out.println("Stat: " + stat);
}
},
"The Context"
); zk.close();
} }

七、判断节点是否存在

1. 使用同步API判断节点是否存在

package com.huey.dream.demo;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.data.Stat; /**
* 使用同步API判断节点是否存在
* @author huey
* @version 1.0
* @created 2015-3-1
*/
public class ZKDemo { public static void main(String[] args) throws Exception { final CountDownLatch connectedSignal = new CountDownLatch(1);
final ZooKeeper zk = new ZooKeeper("192.168.1.109:2182", 5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
if (event.getType() == EventType.None
&& event.getPath() == null) {
connectedSignal.countDown();
}
}
}
});
connectedSignal.await(); Stat stat = zk.exists("/zk-huey", false);
if (stat == null) {
zk.create("/zk-huey", "hello".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} else {
System.out.println("Stat: " + stat);
} zk.close();
} }

2. 使用异步API判断节点是否存在

package com.huey.dream.demo;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.data.Stat; /**
* 使用异步API判断节点是否存在
* @author huey
* @version 1.0
* @created 2015-3-1
*/
public class ZKDemo { public static void main(String[] args) throws Exception { final CountDownLatch connectedSignal = new CountDownLatch(1);
final ZooKeeper zk = new ZooKeeper("192.168.1.109:2182", 5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
if (event.getType() == EventType.None
&& event.getPath() == null) {
connectedSignal.countDown();
}
}
}
});
connectedSignal.await(); zk.exists(
"/zk-huey",
false,
new AsyncCallback.StatCallback() {
@Override
public void processResult(int rc, String path, Object ctx, Stat stat) {
System.out.println("ResultCode: " + rc);
System.out.println("ZNode: " + path);
System.out.println("Context: " + ctx);
System.out.println("Stat: " + stat);
}
},
"The Context"
); zk.close();
} }

八、权限控制

package com.huey.dream.demo;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids; /**
* ZooKeeper权限控制
* @author huey
* @version 1.0
* @created 2015-3-1
*/
public class ZKDemo { public static void main(String[] args) throws Exception { /**
* 使用含有权限信息的zookeeper会话创建数据节点
*/
final CountDownLatch connectedSignal = new CountDownLatch(1);
ZooKeeper zk = new ZooKeeper("192.168.1.109:2182", 5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
connectedSignal.countDown();
}
});
connectedSignal.await();
zk.addAuthInfo("digest", "huey:123".getBytes());
zk.create("/zk-huey", "hello".getBytes(), Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);
zk.close(); /**
* 使用无权限信息的zookeeper会话访问含有权限信息的数据节点
*/
try {
final CountDownLatch signal = new CountDownLatch(1);
ZooKeeper zk1 = new ZooKeeper("192.168.1.109:2182", 5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
signal.countDown();
}
});
signal.await();
zk1.getData("/zk-huey", false, null);
System.out.println("NodeData: " + new String(zk1.getData("/zk-huey", false, null)));
zk1.close();
} catch (Exception e) {
System.out.println("Failed to delete Znode: " + e.getMessage());
} /**
* 使用错误权限信息的zookeeper会话访问含有权限信息的数据节点
*/
try {
final CountDownLatch signal = new CountDownLatch(1);
ZooKeeper zk2 = new ZooKeeper("192.168.1.109:2182", 5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
signal.countDown();
}
});
signal.await();
zk2.addAuthInfo("digest", "huey:abc".getBytes());
System.out.println("NodeData: " + new String(zk2.getData("/zk-huey", false, null)));
zk2.close();
} catch (Exception e) {
System.out.println("Failed to delete Znode: " + e.getMessage());
} /**
* 使用正确权限信息的zookeeper会话访问含有权限信息的数据节点
*/
try {
final CountDownLatch signal = new CountDownLatch(1);
ZooKeeper zk3 = new ZooKeeper("192.168.1.109:2182", 5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
signal.countDown();
}
});
signal.await();
zk3.addAuthInfo("digest", "huey:123".getBytes());
System.out.println("NodeData: " + new String(zk3.getData("/zk-huey", false, null)));
zk3.close();
} catch (Exception e) {
System.out.println("Failed to delete Znode: " + e.getMessage());
}
} }

结果输出:

Failed to delete Znode: KeeperErrorCode = NoAuth for /zk-huey
Failed to delete Znode: KeeperErrorCode = NoAuth for /zk-huey
NodeData: hello

ZooKeeper(3.4.5) - 原生 API 的简单示例的更多相关文章

  1. demo工程的清单文件及activity中api代码简单示例

    第一步注册一个账户,并创建一个应用.获取app ID与 app Key. 第二步下载sdk 第三步新建工程,修改清单文件,导入相关的sdk文件及调用相应的api搞定. 3.1 修改清单文件,主要是加入 ...

  2. 玩转nodeJS系列:使用原生API实现简单灵活高效的路由功能(支持nodeJs单机集群),nodeJS本就应该这样轻快

    前言: 使用nodeJS原生API实现快速灵活路由,方便与其他库/框架进行整合: 1.原生API,简洁高效的轻度封装,加速路由解析,nodeJS本就应该这样轻快 2.不包含任何第三方库/框架,可以灵活 ...

  3. Google字体API使用简单示例

    一.前面的话 Google总会做些造福大众的事情,例如提供了web在线字体的API,这玩意其实去年就有了,但是字体种类手指头+脚趾头就可以数出来.but 最近,貌似Google对字体API进行了升级, ...

  4. ASP.NET Web API 的简单示例

    Demo1: HTML: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...

  5. 【Redis】三、Redis安装及简单示例

    (四)Redis安装及使用   Redis的安装比较简单,仍然和大多数的Apache开源软件一样,只需要下载,解压,配置环境变量即可.具体安装过程参考:菜鸟教程Redis安装.   安装完成后,通过r ...

  6. curator框架的使用以及实现分布式锁等应用与zkclient操作zookeeper,简化复杂原生API

    打开zookeeper集群 先体会一下原生API有多麻烦(可略过): //地址 static final String ADDR = "192.168.171.128:2181,192.16 ...

  7. (原) 2.1 Zookeeper原生API使用

    本文为原创文章,转载请注明出处,谢谢 Zookeeper原生API使用 1.jar包引入,演示版本为3.4.6,非maven项目,可以下载jar包导入到项目中 <dependency> & ...

  8. Zookeeper系列2 原生API 以及核心特性watcher

    原生API 增删改查询 public class ZkBaseTest { static final String CONNECT_ADDR = "192.168.0.120"; ...

  9. Zookeeper系列三:Zookeeper客户端的使用(Zookeeper原生API如何进行调用、ZKClient、Curator)和Zookeeper会话

    一.Zookeeper原生API如何进行调用 准备工作: 首先在新建一个maven项目ZK-Demo,然后在pom.xml里面引入zk的依赖 <dependency> <groupI ...

随机推荐

  1. cocos2dx搭建开发环境

    windows7 64位 搭建cocos2dx 版本开发环境 目前cocos2dx分为2.x版本和3.x版本,搭建环境稍有不同 先搭建3.1版本win32开发环境 相关准备: 注意:安装路径尽可能不要 ...

  2. 【转】Android 4.4前后版本读取图库图片和拍照完美解决方案

    http://blog.csdn.net/zbjdsbj/article/details/42387551 4.3或以下,选了图片之后,根据Uri来做处理,很多帖子都有了,我就不详细说了.主要是4.4 ...

  3. SRV记录说明

      SRV记录是DNS服务器的数据库中支持的一种资源记录的类型,它记录了哪台计算机提供了哪个服务这么一个简单的信息 SRV 记录:一般是为Microsoft的活动目录设置时的应用.DNS可以独立于活动 ...

  4. Codeforces Round #327 (Div. 2) E. Three States BFS

    E. Three States Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/probl ...

  5. 三大文本处理工具grep、sed及awk的简单介绍

    grep.sed和awk都是文本处理工具,虽然都是文本处理工具单却都有各自的优缺点,一种文本处理命令是不能被另一个完全替换的,否则也不会出现三个文本处理命令了.只不过,相比较而言,sed和awk功能更 ...

  6. jquery.validate.js使用id验证控件

    jquery.validate.js默认的是元素的name. 例如:<input name="username" id="username" size=& ...

  7. 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token

    GCC编译C源程序时出现:错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token,通常是因为在函数声明(包括包含的头文 ...

  8. Lua调用自定义C++类

    弄了一天终于会Lua调用自定义C++类.不容易啊. 我的电脑是64位的,装了64的Python不行,装了32位的就可以了,靠!下面是报错信息 python pyyaml Cheetah全都是装32位的 ...

  9. Python_爬虫2

    URLError异常处理 大家好,本节在这里主要说的是URLError还有HTTPError,以及对它们的一些处理. 1.URLError 首先解释下URLError可能产生的原因: 网络无连接,即本 ...

  10. Internet连接共享访问,依赖服务或组无法启动

    问题与现象 在启用本地Internet连接共享给VMware的虚拟网卡时,出现了如下错误提示,导致无法启用(如图一示): Internet连接共享访问被启用时,出现了一个错误.依赖服务或组无法启动 图 ...