手机版
你好,游客 登录 注册
背景:
阅读新闻

Zookeeper API JAVA 解析

[日期:2014-04-09] 来源:Linux社区  作者:Linux [字体:大 中 小]

常用方法:

  • new ZooKeeper()构造方法
  • create
  • exists
  • getChildren
  • getData

注意点:

watcher 的触发,为一次性。

测试程序

package zookeeper;

import Java.io.IOException;
import java.util.List;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

public class ZookeeperTest {

 public static final int Zookeeper_SESSION_TIMEOUT = 20000;

 static ZooKeeper zk;

 static class TestWatcher implements Watcher {

  @Override
  public void process(WatchedEvent event) {
   // TODO Auto-generated method stub
   System.out.println("come from TestWatcher-> process");
  }

 }

 static TestWatcher watcher = new TestWatcher();

 /**
  * new ZooKeeper
  * 初始化 ZooKeeper 实例
  */
 public static void createZKInstance() throws IOException, KeeperException,
   InterruptedException {
  zk = new ZooKeeper("ip:port,ip:port", Zookeeper_SESSION_TIMEOUT,watcher);
 }
 
 /**
  * create
  * 创建节点
  */
 public static void createNode(String node) throws IOException, InterruptedException,KeeperException
  {
  if (zk.exists(node, null) == null) {
    zk.create(node,node.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
  }
  }
 
 /**
  * exists
  * 判断节点是否存在
  */
 public  static boolean isExists(String node) throws InterruptedException,
    KeeperException {
  if (zk.exists(node, null) == null) {
    return false;
  }
  return true;
  }
 
 /**
  * getChildren
  * 获取一个节点的子节点列表
  */
 public static List<String> getChildren(String node) throws KeeperException, InterruptedException{
  return zk.getChildren(node, null);
 }
 
 /**
  * 返回节点的数据
  */
 public static byte[] getDate(String path ) throws KeeperException, InterruptedException{
  return zk.getData(path, null, null);
 }
 
 public static void main(String[] args) {
  try {
   createZKInstance();
   
   createNode("/testnodes");
   
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (KeeperException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 
 }

}

ZooKeeper 的详细介绍:请点这里
ZooKeeper 的下载地址:请点这里

相关阅读:

ZooKeeper集群配置 http://www.linuxidc.com/Linux/2013-06/86348.htm

使用ZooKeeper实现分布式共享锁 http://www.linuxidc.com/Linux/2013-06/85550.htm

分布式服务框架 ZooKeeper -- 管理分布式环境中的数据 http://www.linuxidc.com/Linux/2013-06/85549.htm

ZooKeeper集群环境搭建实践 http://www.linuxidc.com/Linux/2013-04/83562.htm

ZooKeeper服务器集群环境配置实测 http://www.linuxidc.com/Linux/2013-04/83559.htm

ZooKeeper集群安装 http://www.linuxidc.com/Linux/2012-10/72906.htm

linux
相关资讯       Zookeeper API  Zookeeper JAVA 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款