• 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 }
    ========================================================================================== 我希望每一篇文章的背后,都能看到自己对于技术、对于生活的态度。 我相信乔布斯说的,只有那些疯狂到认为自己可以改变世界的人才能真正地改变世界。面对压力,我可以挑灯夜战、不眠不休;面对困难,我愿意迎难而上、永不退缩。 其实我想说的是,我只是一个程序员,这就是我现在纯粹人生的全部。 ==========================================================================================
  • 相关阅读:
    关于数据库 长度和小数点的关系和坑
    温故而知新,jquery选择器$=
    局域网访问本地localhost页面
    解决谷歌浏览器和360浏览器 input 自动填充淡黄色背景色的问题
    抢月饼 浏览器插件开发
    css 多行溢出
    ss服务待研究
    NUnit笔记
    5分钟实现VS2010整合NUnit进行单元测试
    vs2010中使用Nunit测试c#代码结果的正确性
  • 原文地址:https://www.cnblogs.com/weihuang6620/p/10821670.html
Copyright © 2020-2023  润新知