java连接zookeeper实现zookeeper的基本操作

Java服务端连接Zookeeper,进行节点信息的获取,管理…,整理成一个基本工具,

 

添加依赖:

1 <dependency>
2    <groupId>org.apache.zookeeper</groupId>
3    <artifactId>zookeeper</artifactId>
4    <version>3.3.6</version>
5 </dependency>
  1 package com;
  2 
  3 import java.util.List;
  4 import java.util.concurrent.CountDownLatch;
  5 import org.apache.zookeeper.CreateMode;
  6 import org.apache.zookeeper.KeeperException;
  7 import org.apache.zookeeper.WatchedEvent;
  8 import org.apache.zookeeper.Watcher;
  9 import org.apache.zookeeper.Watcher.Event.KeeperState;
 10 import org.apache.zookeeper.ZooDefs.Ids;
 11 import org.apache.zookeeper.ZooKeeper;
 12 import org.apache.zookeeper.data.Stat;
 13 
 14 
 15 
 16 public class BaseZookeeper implements Watcher{
 17  
 18    private ZooKeeper zookeeper;
 19     /**
 20      * 超时时间
 21      */
 22    private static final int SESSION_TIME_OUT = 2000;
 23    private CountDownLatch countDownLatch = new CountDownLatch(1);
 24    @Override
 25    public void process(WatchedEvent event) {
 26       if (event.getState() == KeeperState.SyncConnected) {
 27          System.out.println("Watch received event");
 28          countDownLatch.countDown();
 29       }
 30    }
 31 
 32 
 33 
 34   
 35    /**连接zookeeper
 36     * @param host
 37     * @throws Exception
 38     */
 39    public void connectZookeeper(String host) throws Exception{
 40       zookeeper = new ZooKeeper(host, SESSION_TIME_OUT, this);
 41       countDownLatch.await();
 42       System.out.println("zookeeper connection success");
 43    }
 44   
 45    /**
 46     * 创建节点
 47     * @param path
 48     * @param data
 49     * @throws Exception
 50     */
 51    public String createNode(String path,String data) throws Exception{
 52       return this.zookeeper.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
 53    }
 54   
 55    /**
 56     * 获取路径下所有子节点
 57     * @param path
 58     * @return
 59     * @throws KeeperException
 60     * @throws InterruptedException
 61     */
 62    public List<String> getChildren(String path) throws KeeperException, InterruptedException{
 63       List<String> children = zookeeper.getChildren(path, false);
 64       return children;
 65    }
 66   
 67    /**
 68     * 获取节点上面的数据
 69     * @param path  路径
 70     * @return
 71     * @throws KeeperException
 72     * @throws InterruptedException
 73     */
 74    public String getData(String path) throws KeeperException, InterruptedException{
 75       byte[] data = zookeeper.getData(path, false, null);
 76       if (data == null) {
 77          return "";
 78       }
 79       return new String(data);
 80    }
 81   
 82    /**
 83     * 设置节点信息
 84     * @param path  路径
 85     * @param data  数据
 86     * @return
 87     * @throws KeeperException
 88     * @throws InterruptedException
 89     */
 90    public Stat setData(String path,String data) throws KeeperException, InterruptedException{
 91       Stat stat = zookeeper.setData(path, data.getBytes(), -1);
 92       return stat;
 93    }
 94   
 95    /**
 96     * 删除节点
 97     * @param path
 98     * @throws InterruptedException
 99     * @throws KeeperException
100     */
101    public void deleteNode(String path) throws InterruptedException, KeeperException{
102       zookeeper.delete(path, -1);
103    }
104   
105    /**
106     * 获取创建时间
107     * @param path
108     * @return
109     * @throws KeeperException
110     * @throws InterruptedException
111     */
112    public String getCTime(String path) throws KeeperException, InterruptedException{
113       Stat stat = zookeeper.exists(path, false);
114       return String.valueOf(stat.getCtime());
115    }
116   
117    /**
118     * 获取某个路径下孩子的数量
119     * @param path
120     * @return
121     * @throws KeeperException
122     * @throws InterruptedException
123     */
124    public Integer getChildrenNum(String path) throws KeeperException, InterruptedException{
125       int childenNum = zookeeper.getChildren(path, false).size();
126       return childenNum;
127    }
128    /**
129     * 关闭连接
130     * @throws InterruptedException
131     */
132    public void closeConnection() throws InterruptedException{
133       if (zookeeper != null) {
134          zookeeper.close();
135       }
136    }
137   
138 }

 

测试:

 1 public class Demo {
 2 
 3     public static void main(String[] args) throws Exception {
 4         BaseZookeeper zookeeper = new BaseZookeeper();
 5         zookeeper.connectZookeeper("192.168.0.1:2181");
 6 
 7         List<String> children = zookeeper.getChildren("/");
 8         System.out.println(children);
 9     }
10 
11 }

 

转载于:https://www.cnblogs.com/weihuang6620/p/10821670.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值