设为首页 收藏本站
查看: 1425|回复: 0

[经验分享] Zookeeper Java API 实例

[复制链接]

尚未签到

发表于 2015-11-22 07:49:55 | 显示全部楼层 |阅读模式
Zookeeper Java API 实例
Api介绍
  
  
  表 1 org.apache.zookeeper. ZooKeeper 方法列表

DSC0000.jpg

  除了以上这些上表中列出的方法之外还有一些重载方法,如都提供了一个回调类的重载方法以及可以设置特定 Watcher 的重载方法,具体的方法可以参考 org.apache.zookeeper. ZooKeeper 类的 API 说明。
  英文版: https://zookeeper.apache.org/doc/r3.4.2/api/index.html
实例
  public class MyZk implements Watcher {
  
  public void process(WatchedEvent watchedEvent) {
  System.out.println("触发了" + watchedEvent.getType() + "事件!");
  }
  }
  
  package com.mylearn.zookeeper.test;
  
  import IceUtilInternal.StringUtil;
  import com.jd.common.util.ArrayUtils;
  import org.apache.cxf.common.util.StringUtils;
  import org.apache.zookeeper.*;
  import org.apache.zookeeper.data.ACL;
  import org.apache.zookeeper.data.Stat;
  
  import java.io.IOException;
  import java.util.List;
  
  /**
  * Created by IntelliJ IDEA.
  * User: yingkuohao
  * Date: 13-11-26
  * Time: 上午8:57
  * CopyRight:360buy
  * Descrption:
  * zk java api测试
  * To change this template use File | Settings | File Templates.
  */
  public class ZkApi {
  
  private static String server = "192.168.229.79:";
  private static String port = "2181";
  private static final int SESSION_TIMEOUT = 10000;
  private static ZooKeeper zk = null;
  
  public static void main(String args[]) {
  ZkApi testzkApi = new ZkApi();
  Watcher watcher = new MyZk();
  //        ZooKeeper zk =testzkApi.init(server + port, ClientBase.CONNECTION_TIMEOUT); //创建zk对象
  zk = testzkApi.init(server + port, SESSION_TIMEOUT, watcher); //创建zk对象
  
  String parentPath = "/testParentPath";        //节点路径 ,the path for the node
  String testParentData = "testParentData";       //  节点初始化数据,the initial data for the node
  List<ACL> aclList = ZooDefs.Ids.OPEN_ACL_UNSAFE; //节点的权限 the acl for the node
  //        CreateMode createMode = CreateMode.EPHEMERAL;  //临时节点,一旦创建这个节点的客户端与服务端端口session超时,这种节点会被自动删除。
  CreateMode createMode = CreateMode.PERSISTENT;  //持久节点,这个目录节点存储的数据不会丢失。
  try {
  createNod(parentPath, testParentData, aclList, createMode);
  String parentData = new String(zk.getData(parentPath, false, null));//读取付父节点内容,getData方法
  System.out.println(&quot;parentData=&quot; &#43; parentData);
  
  String childPath1 = parentPath &#43; &quot;/testChildPath1&quot;;
  String childData1 = &quot;testChildData1&quot;;
  
  createNod(childPath1, childData1, aclList, createMode);//创建子目录 1
  String childDataPop = new String(zk.getData(childPath1, false, null));//读取子节点内容
  System.out.println(&quot;childData1=&quot; &#43; childDataPop);
  
  
  String childPath2 = parentPath &#43; &quot;/testChildPath2&quot;;
  String childData2 = &quot;testChildData2&quot;;
  
  createNod(childPath2, childData2, aclList, createMode);//创建子目录2
  //创建另一个父节点
  String parentPath2 = &quot;/testParentPath2&quot;;
  String testParentData2 = &quot;testParentData2&quot;;
  createNod(parentPath2, testParentData2, aclList, createMode);
  
  listNode(&quot;/&quot;);
  listNode(parentPath);
  
  Stat stat = zk.exists(parentPath, false);
  List<ACL> rootAcl = zk.getACL(parentPath, stat);   //获取某个目录节点的访问权限列表。
  System.out.println(&quot;节点权限:&quot; &#43; ArrayUtils.join(rootAcl.toArray(), &quot;,&quot;)&#43;&quot;节点版本:&quot;&#43;stat.getVersion());
  zk.setACL(parentPath,ZooDefs.Ids.READ_ACL_UNSAFE,stat.getVersion());
  System.out.println(&quot;节点权限:&quot; &#43; ArrayUtils.join( zk.getACL(parentPath, stat).toArray(), &quot;,&quot;));
  
  deleteNode(&quot;/testChildPath&quot;);
  deleteNode(parentPath); //父目录下有子节点,不能直接删除,会报错: KeeperErrorCode = Directory not empty for /testParentPath
  listNode(&quot;/&quot;);
  } catch (KeeperException e) {
  e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
  } catch (InterruptedException e) {
  e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
  }
  
  }
  
  
  /**
  * 删除节点
  * @param path
  * @throws KeeperException
  * @throws InterruptedException
  */
  private static void deleteNode(String path) throws KeeperException, InterruptedException {
  Stat stat = zk.exists(path, true);
  if (stat != null) {
  zk.delete(path, -1);  //-1可以匹配任何版本,也就删除了这个目录节点所有数据,
  System.out.println(&quot;删除&quot; &#43; path &#43; &quot;节点成功&quot;);
  listNode(path);
  } else {
  System.out.println(path &#43; &quot;节点不存在&quot;);
  }
  }
  
  /**
  * 打印节点信息
  * @param path
  * @throws KeeperException
  * @throws InterruptedException
  */
  private static void listNode(String path) throws KeeperException, InterruptedException {
  List<String> nodeList = zk.getChildren(path, false); //获取孩子节点
  System.out.println(path &#43; &quot;路径下的节点&quot; &#43; ArrayUtils.join(nodeList.toArray(), &quot;,&quot;));
  }
  
  /**
  * 创建节点
  *
  * @param nodePath   节点路径
  * @param nodeData   节点初始化数据
  * @param aclList    权限列表
  * @param createMode 节点类型
  * @throws KeeperException
  * @throws InterruptedException
  */
  private static void createNod(String nodePath, String nodeData, List<ACL> aclList, CreateMode createMode) {
  try {
  Stat stat = zk.exists(nodePath, true);
  if (stat != null) {
  //如两次执行 EPHEMERAL节点,就不会执行这段代码,因为每次都会清除;而 PERSISTENT节点,创建一次后就会一直存在,除非手动删除
  System.out.println(&quot;节点&quot; &#43; nodePath &#43; &quot;已存在!&quot;);
  } else {
  zk.create(nodePath, nodeData.getBytes(), aclList, createMode);//创建节点 ,create方法
  }
  } catch (KeeperException e) {
  e.printStackTrace();
  } catch (InterruptedException e) {
  e.printStackTrace();
  }
  }
  
  
  /**
  * 初始化zk
  * @param path
  * @param timeOut
  * @param watcher
  * @return
  */
  public ZooKeeper init(String path, int timeOut, Watcher watcher) {
  if (zk == null) {
  try {
  zk = new ZooKeeper(path, timeOut, watcher);
  } catch (IOException e) {
  e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
  }
  }
  return zk;
  }
  
  
  }
  
  
  参考:
  https://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-141962-1-1.html 上篇帖子: Win7 Zookeeper的下载与安装 下篇帖子: ZooKeeper示例 实时更新server列表
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表