<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency>
package com.itman; import java.io.IOException; 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.Watcher.Event.EventType; import org.apache.zookeeper.Watcher.Event.KeeperState; import org.apache.zookeeper.ZooDefs.Ids; import org.apache.zookeeper.ZooKeeper; public class ZookeeperHelloworld { /** * 集羣地址 */ private static final String CONNECT_ADDRES = "169.254.197.135:2181"; /** * 超時時間 */ private static final int SESSIONTIME = 2000; private static final CountDownLatch countDownLatch = new CountDownLatch(1); public static void main(String[] args) throws IOException, InterruptedException, KeeperException { ZooKeeper zk = new ZooKeeper(CONNECT_ADDRES, SESSIONTIME, new Watcher() { public void process(WatchedEvent event) { // 獲取時間的狀態 KeeperState keeperState = event.getState(); EventType tventType = event.getType(); // 若是是創建鏈接 if (KeeperState.SyncConnected == keeperState) { if (EventType.None == tventType) { // 若是創建鏈接成功,則發送信號量,讓後阻塞程序向下執行 countDownLatch.countDown(); System.out.println("zk 創建鏈接"); } } } }); // 進行阻塞 countDownLatch.await(); // 建立父節點 // String result = zk.create("/testRott", "12245465".getBytes(), // Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); // System.out.println("result:" + result); // 建立子節點 String result = zk.create("/testRott/children", "children 12245465".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); System.out.println("result:" + result); zk.close(); } }
package com.itman; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.Watcher.Event.EventType; import org.apache.zookeeper.Watcher.Event.KeeperState; import org.apache.zookeeper.ZooDefs.Ids; import org.apache.zookeeper.ZooKeeper; public class ZkWatcher implements Watcher { /** * 集羣地址 */ private static final String CONNECT_ADDRES = "169.254.197.135:2181"; private static ZooKeeper zooKeeper; /** * 超時時間 */ private static final int SESSIONTIME = 2000; public ZkWatcher() { createConnection(CONNECT_ADDRES, SESSIONTIME); } // zk節點、發生變動、刪除、修改 、 新增 事件通知 public void process(WatchedEvent event) { KeeperState keeperState = event.getState(); // 事件類型 EventType eventType = event.getType(); // 節點名稱 String path = event.getPath(); System.out.println( "#####process()####調用####keeperState:" + keeperState + ",eventType:" + eventType + ",path:" + path); if (KeeperState.SyncConnected == keeperState) { // 鏈接類型 if (EventType.None == eventType) { // 創建zk鏈接 System.out.println("創建zk鏈接成功!"); } // 建立類型 if (EventType.NodeCreated == eventType) { System.out.println("####事件通知,當前建立一個新的節點####路徑:" + path); } // 修改類型 if (EventType.NodeDataChanged == eventType) { System.out.println("####事件通知,當前建立一個修改節點####路徑:" + path); } // 刪除類型 if (EventType.NodeDeleted == eventType) { System.out.println("####事件通知,當前建立一個刪除節點####路徑:" + path); } } System.out.println("####################################################"); System.out.println(); } // 建立zk鏈接 private void createConnection(String connectAddres, int sessiontime) { try { zooKeeper = new ZooKeeper(connectAddres, sessiontime, this); } catch (Exception e) { // TODO: handle exception } } // 建立節點 public void createNode(String path, String data) { try { String result = zooKeeper.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); System.out.println("建立節點成功....result:" + result); } catch (Exception e) { e.printStackTrace(); } } // 修改節點 public void updateNode(String path, String data) { try { zooKeeper.setData(path, data.getBytes(), -1); } catch (Exception e) { // TODO: handle exception } } // 刪除節點 public void deleNode(String path) { try { zooKeeper.delete(path, -1); } catch (Exception e) { // TODO: handle exception } } public void close() { try { if (zooKeeper != null) zooKeeper.close(); } catch (Exception e) { // TODO: handle exception } } public static void main(String[] args) throws KeeperException, InterruptedException { ZkWatcher zkWatcher = new ZkWatcher(); // zkWatcher.createNode("/parent1", "6452852"); String path = "/parent1"; zooKeeper.exists(path, true); // zkWatcher.updateNode(path, "88888"); zkWatcher.deleNode(path); zkWatcher.close(); } }