Zookeeper集群Java操作(Zookeeper3.4.5)

datouniao 2015-04-14

1.zk集群java简单操作

1.0 包目录结构

lib取自zookpeeper官方压缩包(基于zk3.4.5测试通过)

Zookeeper集群Java操作(Zookeeper3.4.5)

1.1 AbstractZooKeeper.java

package com.ccy.zk;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import org.apache.log4j.Logger;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;

/**
 *
 * <p>
 * Title: AbstractZooKeeper.java
 * Package com.ccy.zk
 * </p>
 * <p>
 * Description: TODO
 * <p>
 * @author Tom.Cai
 * @created 2015-4-13 下午9:48:50
 * @version V1.0
 *
 */
public class AbstractZooKeeper implements Watcher { 
 protected Logger logger = Logger.getLogger(AbstractZooKeeper.class);
 
    //缓存时间 
    private static final int SESSION_TIME  = 2000;   
    protected ZooKeeper zooKeeper; 
    protected CountDownLatch countDownLatch=new CountDownLatch(1); 
 
    //连接zk集群
    public void connect(String hosts) throws IOException, InterruptedException{   
            zooKeeper = new ZooKeeper(hosts,SESSION_TIME,this);   
            countDownLatch.await();   
      }   
    //zk处理
    @Override 
    public void process(WatchedEvent event) { 
        if(event.getState()==KeeperState.SyncConnected){ 
            countDownLatch.countDown(); 
        } 
    } 
   
    //关闭集群
    public void close() throws InterruptedException{   
        zooKeeper.close();   
    }   
}

1.2 ZooKeeperOperator.java(测试类)

package com.ccy.zk;


import java.util.List;

import org.apache.log4j.Logger;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.data.Stat;

 

/**
 *
 * <p>
 * Title: ZooKeeperOperator.java
 * Package com.ccy.zk
 * </p>
 * <p>
 * Description: TODO
 * <p>
 * @author Tom.Cai
 * @created 2015-4-13 下午9:49:00
 * @version V1.0
 *
 */
public class ZooKeeperOperator extends AbstractZooKeeper {
 
 private static Logger logger = Logger.getLogger(ZooKeeperOperator.class);

 /**
  *
  *创建znode
  *
  */
 public void create(String path,byte[] data)throws KeeperException, InterruptedException{
  /**
  * 此处采用的是创建的是持久化节点:PERSISTENT表示不会因连接的断裂而删除节点
  * EPHEMERAL 表示The znode will be deleted upon the client's disconnect.
  */
  this.zooKeeper.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
 }
 /**
  *
  *获取节点信息
  *
  */
 public void getChild(String path) throws KeeperException, InterruptedException{ 
  try{
   List<String> list=this.zooKeeper.getChildren(path, false);
   if(list.isEmpty()){
    logger.info(path+"中没有节点");
    System.out.println(path+"中没有节点");
   }else{
    for(String child:list){
     System.out.println("节点:"+child);
    }
   }
  }catch (KeeperException.NoNodeException e) {
   // TODO: handle exception
    throw e; 
  }
 }
 /**
  *
  *获取节点数据
  *
  */
 public byte[] getData(String path) throws KeeperException, InterruptedException { 
        return  this.zooKeeper.getData(path, false,null); 
    } 
 
 /**
  * 修改节点数据
  */
 public Stat setDate(String path,byte[] data,int version) throws KeeperException, InterruptedException{
  return this.zooKeeper.setData(path, data, version);
 }
 
 /**
  * 删除节点
  */
 public void deleteNode(final String path,int version) throws InterruptedException, KeeperException{
  this.zooKeeper.delete(path, version);
 }
 
  public static void main(String[] args) {
  try { 
            ZooKeeperOperator zkoperator = new ZooKeeperOperator(); 
            zkoperator.connect("192.168.80.100");
           
            //操作1:创建一个持久化节点
            //zkoperator.create("/test1", "zk".getBytes());
            //zkoperator.create("/test1/test11", "zk".getBytes());
            //zkoperator.create("/test1/test12", "zk".getBytes());
           
            //操作2:取出原有的节点
            //byte[] b = zkoperator.getData("/test1");
            //System.out.println(new String(b));
           
            //操作3:设置节点数据
            //zkoperator.setDate("/test1", "zk1".getBytes(), 1);
           
           
            //操作4:删除节点操作
            zkoperator.deleteNode("/test1/test12", 0);
           
            //获取子节点
            // zkoperator.getChild("/test1");
       

               
            zkoperator.close(); 
           
           
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 

 }
}

--------------------------------------分割线 --------------------------------------

相关推荐