package com.fsun.framework.zookeeper; import java.io.IOException; import java.nio.charset.Charset; import java.util.List; import org.apache.commons.lang3.exception.ExceptionUtils; 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.ZooDefs.Ids; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.data.ACL; import org.apache.zookeeper.data.Stat; public class MainTest { private static ZooKeeper zk; static{ try { zk=new ZooKeeper("192.168.1.11:4180", 5000, null); } catch (IOException e) { System.out.println("创建zookeeper客户端失败,异常信息"+ExceptionUtils.getMessage(e)); } } public static void main(String[] args) { try { /*String result=createOpenAclPersistentNode("/services", "publicservice".getBytes()); System.out.println("创建开放访问权限的持久节点成功,返回值="+result);*/ /*Stat stat=setData("/services", "publicservice2.0".getBytes(), 0); printAllStateInfo(stat);*/ /*String result=getData("/services"); System.out.println("获取到节点数据:"+result);*/ /*String result=createOpenAclPersistentChildNode("/services", "/publicservice", "publicservice1.0".getBytes()); System.out.println("添加子节点成功,返回的子节点完整路径:"+result);*/ //boolean exists=exists("/service"); } catch (Exception e) { System.out.println("新增持久节点失败,失败信息:"+ExceptionUtils.getStackTrace(e)); } finally{ if(zk!=null){ try { zk.close(); } catch (InterruptedException e) { zk=null; } } } } /** * 打印出所有节点的状态信息 * @param stat 节点状态信息 */ private static void printAllStateInfo(Stat stat) { System.out.println("权限控制列表修改次数="+ stat.getAversion()); System.out.println("节点创建时间="+stat.getCtime()); System.out.println("子节点修改次数"+stat.getCversion()); System.out.println(""+stat.getCzxid()); System.out.println("节点数据长度="+stat.getDataLength()); System.out.println("临时节点所有者的会话id(不是临时节点则为0)"+stat.getEphemeralOwner()); //如果znode是临时节点,则指示节点所有者的会话ID;如果不是临时节点,则为零。 System.out.println("节点最近修改时间="+stat.getMtime()); System.out.println(""+stat.getMzxid()); System.out.println("子节点个数="+stat.getNumChildren()); System.out.println(""+stat.getPzxid()); System.out.println("节点版本(即节点修改次数)="+stat.getVersion()); } private static void test() throws Exception{ throw new Exception("你红啊"); } /** * 新建节点 * @param nodeName 节点路径 * @param nodeData 节点数据 * @param aclList 权限控制列表 * @param createMode 节点类型 * @return String 创建的节点路径名称 * @throws InterruptedException * @throws KeeperException */ private static String create(String nodePath,byte[] nodeData,List<ACL> aclList,CreateMode createMode) throws KeeperException, InterruptedException{ String result= zk.create(nodePath, nodeData, aclList, createMode); return result; } /** * 创建临时节点(在客户端与服务器断开连接后会被删除) * @param nodePath 节点路径 * @param nodeData 节点数据 * @param aList 权限控制列表 * @return String 新创建的节点路径 * @throws InterruptedException * @throws KeeperException */ private static String createTemporyNode(String nodePath,byte[] nodeData,List<ACL> aList) throws KeeperException, InterruptedException{ return create(nodePath, nodeData, aList, CreateMode.EPHEMERAL); } /** * 创建持久节点(客户端与服务区断开连接后不会被删除) * @param nodePath 节点路径 * @param nodeData 节点数据 * @param acList 权限控制列表 * @return * @throws KeeperException * @throws InterruptedException */ private static String createPersistentNode(String nodePath,byte[] nodeData,List<ACL> acList) throws KeeperException, InterruptedException{ return create(nodePath, nodeData, acList, CreateMode.PERSISTENT); } /** * 创建访问权限为开放的持久节点 * @param nodePath 节点路径 * @param nodeData 节点数据 * @return String 新建的节点路径 * @throws KeeperException * @throws InterruptedException */ private static String createOpenAclPersistentNode(String nodePath,byte[] nodeData) throws KeeperException, InterruptedException{ return create(nodePath, nodeData, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } /** * 删除节点 * @param path 节点路径 * @param version 节点版本(-1表示所有版本) * @throws InterruptedException * @throws KeeperException */ private static void deleteZNode(String path,int version) throws InterruptedException, KeeperException{ zk.delete(path, version); } /** * 获取节点数据 * @param path 节点路径 * @param data 要设置的节点数据 * @param version 节点版本 * @return Stat 设置后的节点状态 * @throws KeeperException * @throws InterruptedException */ private static Stat setData(String path,byte[] data,int version) throws KeeperException, InterruptedException{ return zk.setData(path, data, version); } /** * 获取节点数据(从zookeeper获取到的字节数组用默认字符集转换成字符串对象) * @param path 节点路径 * @return * @throws KeeperException * @throws InterruptedException */ private static String getData(String path) throws KeeperException, InterruptedException{ Stat stat=new Stat(); return new String( zk.getData(path, false, stat),Charset.defaultCharset()); } /** * 创建一个开放权限访问的持久子节点 * @param parentNodePath 父节点路径 * @param nodePath 子节点当前路径 * @param nodeData 子节点数据 * @return String 子节点完整路径 * @throws KeeperException * @throws InterruptedException */ private static String createOpenAclPersistentChildNode(String parentNodePath,String nodePath,byte[] nodeData) throws KeeperException, InterruptedException{ return create(parentNodePath+nodePath, nodeData, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } /** * 判断节点是否存在 * @param nodePath 节点路径 * @return Stat 节点状态信息 * @throws KeeperException * @throws InterruptedException */ private static boolean exists(String nodePath) throws KeeperException, InterruptedException{ return zk.exists(nodePath, false)==null?false:true; } /** * Zookeeper节点变化监听类 * @author Administrator * */ private static class ZkWatcher implements Watcher{ @Override public void process(WatchedEvent event) { /**********************一共下面四种节点变化的类型********************/ if(event.getType()==EventType.NodeDeleted){ //节点被删除 } if(event.getType()==EventType.NodeChildrenChanged){ //节点的子节点被修改 } if(event.getType()==EventType.NodeCreated){ //创建节点 } if(event.getType()==EventType.NodeDataChanged){ //节点数据被修改 } } } }