建立java工程,使用zookeeper的api 对其进行简单操作。
了解接口的分类和情况
在虚拟机192.168.136.144上部署了zookeeper,伪分布式方式。
在本机192.168.136.1 eclipse环境下建立 java工程
1)依赖库
2) 代码
package com.solostudy.zookeeper.api; import java.io.IOException; 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.Watcher.Event.KeeperState; import org.apache.zookeeper.ZooDefs.Ids; import org.apache.zookeeper.ZooKeeper; public class CliApiDemo implements Watcher { public final String CLIENT_PORT = "2181"; public final String ZK_IP = "192.168.136.144"; public final Integer CONNECTION_TIMEOUT = 1000; public ZooKeeper zk = null; private CountDownLatch connectedSemaphore = new CountDownLatch(1); /** * 继承了watcher,需要实现process接口,这个接口类似一个回调函数 收到来自Server的Watcher通知后的处理。 */ @Override public void process(WatchedEvent event) { //System.out.println("收到事件通知:" + event.getState() + "\n"); if (KeeperState.SyncConnected == event.getState()) { connectedSemaphore.countDown(); } } /** * zookeeper连接方法,成功返回true,否则返回false * */ public boolean ZK_con() { String con_string = ZK_IP + ":" + CLIENT_PORT; try { zk = new ZooKeeper(con_string, CONNECTION_TIMEOUT, this); // connectedSemaphore.await(); } catch (InterruptedException e) { System.out.println("连接创建失败,发生 InterruptedException"); e.printStackTrace(); return false; } catch (IOException e) { System.out.println("连接创建失败,发生 IOException"); e.printStackTrace(); return false; } return true; } /** * zookeeper 创建目录或者子目录,成功返回true,否则返回false * */ public boolean ZK_create(String strPath, String strDir) { try { zk.create(strPath, strDir.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } catch (KeeperException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } return true; } /** * zookeeper连接方法,成功返回true,否则返回false * */ public String ZK_getNode(String strPath) { String result = ""; try { result = new String(zk.getData(strPath, false, null)); } catch (KeeperException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } public List ZK_getChild(String strPath) { List<String> resultList = null; try { resultList = zk.getChildren(strPath, true); } catch (KeeperException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return resultList; } // 设置节点数据 public boolean ZK_setNodeDate(String strPath, String strNewData) { try { zk.setData(strPath, strNewData.getBytes(), -1); } catch (KeeperException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } return true; } // 判断节点是否存在 public boolean ZK_exists(String strPath) { try { zk.exists(strPath, true); } catch (KeeperException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } return true; } // 删除节点 public boolean ZK_delete(String strPath) { try { zk.delete(strPath, -1); } catch (KeeperException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } return true; } // 删除节点 public boolean ZK_close() { try { zk.close(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } return true; } public static void main(String[] args) { // 创建一个CliApiDemo对象 CliApiDemo myCliApiDemo = new CliApiDemo(); // 创建一个与服务器的连接 if (myCliApiDemo.ZK_con()) { System.out.println("ZK connect succesfully!"); } else { System.out.println("ZK connect failed!"); System.exit(0); } // 创建一个目录节点 if (myCliApiDemo.ZK_create("/MyPath", "MyPath")) System.out.println(myCliApiDemo.ZK_getNode("/MyPath")); else System.out.println("Create Node failed!!!"); // 创建一个子目录节点 if (myCliApiDemo.ZK_create("/MyPath/MyChildPath1", "child-1")) System.out.println(myCliApiDemo.ZK_getChild("/MyPath")); else System.out.println("Create children Node failed!!!"); // 修改子目录节点数据 if (myCliApiDemo.ZK_setNodeDate("/MyPath/MyChildPath1", "child-1")) System.out.println("目录节点状态:[" + myCliApiDemo.ZK_exists("/MyPath") + "]"); else System.out.println("Set New Data failed!!!"); // 创建另外一个子目录节点 if (myCliApiDemo.ZK_create("/MyPath/MyChildPath2", "child-2")) System.out.println(myCliApiDemo .ZK_getChild("/MyPath")); else System.out.println("Create children Node failed!!!"); // 删除子目录节点 myCliApiDemo.ZK_delete("/MyPath/MyChildPath2"); myCliApiDemo.ZK_delete("/MyPath/MyChildPath1"); // 删除父目录节点 myCliApiDemo.ZK_delete("/MyPath"); // 关闭连接 myCliApiDemo.ZK_close(); } } 执行结果如下图: