欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java连接zookeeper实现zookeeper教程

程序员文章站 2022-04-13 08:33:05
目录java连接zookeeper实现zookeeperjava服务端连接zookeeper,进行节点信息的获取,管理…整理成一个基本工具添加依赖:

java连接zookeeper实现zookeeper

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

添加依赖:

<dependency>
   <groupid>org.apache.zookeeper</groupid>
   <artifactid>zookeeper</artifactid>
   <version>3.3.6</version>
</dependency>

具体代码如下:

package com;
 
import java.util.list;
import java.util.concurrent.countdownlatch;
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.keeperstate;
import org.apache.zookeeper.zoodefs.ids;
import org.apache.zookeeper.zookeeper;
import org.apache.zookeeper.data.stat;
 
 
 
public class basezookeeper implements watcher{
 
   private zookeeper zookeeper;
    /**
     * 超时时间
     */
   private static final int session_time_out = 2000;
   private countdownlatch countdownlatch = new countdownlatch(1);
   @override
   public void process(watchedevent event) {
      if (event.getstate() == keeperstate.syncconnected) {
         system.out.println("watch received event");
         countdownlatch.countdown();
      }
   }
 
 
 
  
   /**连接zookeeper
    * @param host
    * @throws exception
    */
   public void connectzookeeper(string host) throws exception{
      zookeeper = new zookeeper(host, session_time_out, this);
      countdownlatch.await();
      system.out.println("zookeeper connection success");
   }
  
   /**
    * 创建节点
    * @param path
    * @param data
    * @throws exception
    */
   public string createnode(string path,string data) throws exception{
      return this.zookeeper.create(path, data.getbytes(), ids.open_acl_unsafe, createmode.persistent);
   }
  
   /**
    * 获取路径下所有子节点
    * @param path
    * @return
    * @throws keeperexception
    * @throws interruptedexception
    */
   public list<string> getchildren(string path) throws keeperexception, interruptedexception{
      list<string> children = zookeeper.getchildren(path, false);
      return children;
   }
  
   /**
    * 获取节点上面的数据
    * @param path  路径
    * @return
    * @throws keeperexception
    * @throws interruptedexception
    */
   public string getdata(string path) throws keeperexception, interruptedexception{
      byte[] data = zookeeper.getdata(path, false, null);
      if (data == null) {
         return "";
      }
      return new string(data);
   }
  
   /**
    * 设置节点信息
    * @param path  路径
    * @param data  数据
    * @return
    * @throws keeperexception
    * @throws interruptedexception
    */
   public stat setdata(string path,string data) throws keeperexception, interruptedexception{
      stat stat = zookeeper.setdata(path, data.getbytes(), -1);
      return stat;
   }
  
   /**
    * 删除节点
    * @param path
    * @throws interruptedexception
    * @throws keeperexception
    */
   public void deletenode(string path) throws interruptedexception, keeperexception{
      zookeeper.delete(path, -1);
   }
  
   /**
    * 获取创建时间
    * @param path
    * @return
    * @throws keeperexception
    * @throws interruptedexception
    */
   public string getctime(string path) throws keeperexception, interruptedexception{
      stat stat = zookeeper.exists(path, false);
      return string.valueof(stat.getctime());
   }
  
   /**
    * 获取某个路径下孩子的数量
    * @param path
    * @return
    * @throws keeperexception
    * @throws interruptedexception
    */
   public integer getchildrennum(string path) throws keeperexception, interruptedexception{
      int childennum = zookeeper.getchildren(path, false).size();
      return childennum;
   }
   /**
    * 关闭连接
    * @throws interruptedexception
    */
   public void closeconnection() throws interruptedexception{
      if (zookeeper != null) {
         zookeeper.close();
      }
   }
  
}
 
 
 

测试:

public class demo {
 
    public static void main(string[] args) throws exception {
        basezookeeper zookeeper = new basezookeeper();
        zookeeper.connectzookeeper("192.168.0.1:2181");
 
        list<string> children = zookeeper.getchildren("/");
        system.out.println(children);
    }
 
}


相关标签: java zookeeper