zookeeper 之 java API 使用,zookeeper 学习
private static ZooKeeper zooKeeper;
public static void main(String[] args) {
try {
zooKeeper = new ZooKeeper("192.168.40.135:2181", 5000, new Watcher() {
public void process(WatchedEvent watchedEvent) {
if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
doSomeThing();
}
}
});
System.out.println("state:{}" + zooKeeper.getState());
Thread.sleep(Integer.MAX_VALUE);
}
catch (IOException e) {
System.out.println("connect zk failed");
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
private static void doSomeThing() {
String path = null;
try {
path = zooKeeper.create("/node_4", "123".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
catch (KeeperException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("path:" + path);
}
}
同步异步创建:
package com.zookeeper;
import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import java.io.IOException;
public class CreateNodeASync {
private static ZooKeeper zooKeeper;
public static void main(String[] args) {
try {
zooKeeper = new ZooKeeper("192.168.40.135:2181", 5000, new Watcher() {
public void process(WatchedEvent watchedEvent) {
if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
doSomeThing();
}
}
});
System.out.println("state:{}" + zooKeeper.getState());
Thread.sleep(Integer.MAX_VALUE);
}
catch (IOException e) {
System.out.println("connect zk failed");
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
private static void doSomeThing() {
zooKeeper.create("/node_5", "123".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new IStringClassback(), "异步创建");
}
//异步调用,需要 一个回调函数
static class IStringClassback implements AsyncCallback.StringCallback {
/**
@param rc 返回码,如果异步创建成功,返回 0
@param path 创建节点的完整路径
@param ctx 异步调用的上下文,。。也就是上面传下来的“创建”字符串
@param name 服务端返回的已经创建节点的真实路径,如果是顺序节点,那么和 path 不一致
*/
public void processResult(int rc, String path, Object ctx, String name) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("rc=" + rc).append("\n");
stringBuilder.append("path=" + path).append("\n");
stringBuilder.append("ctx=" + ctx).append("\n");
stringBuilder.append("name=" + name).append("\n");
System.out.println(stringBuilder.toString());
}
}
}
运行结果:
同步获取:
package com.zookeeper;
im
port org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import java.io.IOException;
import java.util.List;
public class GetChildSync {
private static ZooKeeper zooKeeper;
public static void main(String[] args) {
try {
zooKeeper = new ZooKeeper("192.168.40.135:2181", 5000, new Watcher() {
public void process(WatchedEvent watchedEvent) {
if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
doSomeThing();
}
}
});
System.out.println("state:{}" + zooKeeper.getState());
Thread.sleep(Integer.MAX_VALUE);
}
catch (IOException e) {
System.out.println("connect zk failed");
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
private static void doSomeThing() {
List<String> children = null;
try {
//false 表示不需要需要关注子节点的变化
children = zooKeeper.getChildren("/", false);
}
catch (KeeperException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("children:" + children);
}
}
运行结果:
如果需要关注子节点的变化:
package com.zookeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import java.io.IOException;
import java.util.List;
public class GetChildSync {
private static ZooKeeper zooKeeper;
public static void main(String[] args) {
try {
zooKeeper = new ZooKeeper("192.168.40.135:2181", 5000, new Watcher() {
public void process(WatchedEvent watchedEvent) {
if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
if (watchedEvent.getType() == Event.EventType.None && null == watchedEvent.getPath()) {
doSomeThing();
} else {
if (watchedEvent.getType() == Event.EventType.NodeChildrenChanged) {
try {
System.out.println(zooKeeper.getChildren(watchedEvent.getPath(), true));
}
catch (KeeperException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
});
System.out.println("state:{}" + zooKeeper.getState());
Thread.sleep(Integer.MAX_VALUE);
}
catch (IOException e) {
System.out.println("connect zk failed");
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
private static void doSomeThing() {
List<String> children = null;
try {
//false 表示不需要需要关注子节点的变化
children = zooKeeper.getChildren("/", true);
}
catch (KeeperException e) {
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("children:" + children);
}
}
运行结果:
通过服务端给根节点添加一个子节点,就会打印出来:
异步获取:
package com.zookeeper;
import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
import java.util.List;
public class GetChildASync {
private static ZooKeeper zooKeeper;
public static void main(String[] args) {
try {
zooKeeper = new ZooKeeper("192.168.40.135:2181", 5000, new Watcher() {
public void process(WatchedEvent watchedEvent) {
if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
if (watchedEvent.getType() == Event.EventType.None && null == watchedEvent.getPath()) {
doSomeThing();
} else {
if (watchedEvent.getType() == Event.EventType.NodeChildrenChanged) {
zooKeeper.getChildren(watchedEvent.getPath(), true, new ChildrenClallBack(), "异步获取");
}
}
}
}
});
System.out.println("state:{}" + zooKeeper.getState());
Thread.sleep(Integer.MAX_VALUE);
}
评论