public class App {
private static final int SESSION_TIME = 2000;
protected ZooKeeper zk;
protected String host = "";
protected void connectToZK(String newHost) throws InterruptedException, IOException {
if (zk != null && zk.getState().isAlive()) {
zk.close();
}
host = newHost;
zk = new ZooKeeper(host, SESSION_TIME, new MyWatcher());
}
public static void printMessage(String msg) {
System.out.println(msg);
}
private class MyWatcher implements Watcher {
public void process(WatchedEvent event) {
App.printMessage(event.toString());
}
}
}
public class AppTest {
public static void main(String[] args) throws Exception {
App app = new App();
app.connectToZK("192.168.1.92:2181");
//添加一个scheme为ip的权限控制
ArrayList<ACL> acl = new ArrayList<ACL>(Collections.singletonList(new ACL(Perms.ALL, new Id("ip","192.168.1.92"))));
app.zk.create("/test", "test".getBytes(), acl, CreateMode.PERSISTENT);
}
}