java 学习笔记(四) java连接ZooKeeper
public class Demo2 { public static void main(String[] args) { String connectString = "192.168.168.133:2181"; int sessionTimeout = 500000; // 会话时间。设置长一点,如果不够长,则会connect loss try { // 建立zookeeper链接 ZooKeeper zk = new ZooKeeper(connectString, sessionTimeout, new Watcher(){ public void process(WatchedEvent event) { // 监控所有触发的事件 System.out.println("start do something"); EventType type = event.getType(); // 事件类型。枚举 KeeperState state = event.getState(); // 状态。 枚举 // type.getIntValue(); switch (type) { case None: // int值对应 -1 System.out.println("none 事件触发"); break; case NodeCreated: // int值对应 1 System.out.println("创建节点事件发生了"); break; case NodeDeleted: // int值对应 2 System.out.println("删除节点事件发生了"); break; case NodeDataChanged: // int值对应 3 System.out.println("节点数据改变事件发生了"); break; case NodeChildrenChanged: // int值对应 4 System.out.println("子节点改变事件发生了"); break; default: System.out.println("I do not know what operate you do"); break; } // state.getIntValue(); NoSyncConnected 1和Unknown-1 switch (state) { case Disconnected: // 0 System.out.println("失去连接"); break; case SyncConnected: // 3 System.out.println("异步链接"); break; case Expired: // -112 System.out.println("超时过期"); break; default: break; } System.out.println("end"); } }); System.out.println("zk创建成功"); List<String> list = zk.getChildren("/", true); // 获取所有节点 if(list != null) { for(String s : list) System.out.println(s); } // 在/app5节点下创建child节点 zk.create("/app7", "/app8的子节点数据".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); // 会对数据持久化,服务下次启动任然存在 zk.create("/app8", "/app8的子节点数据".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new StringCallback(){ public void processResult(int rc, String path, Object ctx, String name) { System.out.println("rc: " + rc); System.out.println("path: " + path); System.out.println("Object:" + ctx); System.out.println("name: " + name); } }, "ctx object"); } catch (IOException e) { e.printStackTrace(); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
zk每一次操纵都会被监控watcher记录下来
出现这种报错是由于创建节点的时候没有创建父节点 /app1