zookeeper原生api调用

操作步骤:

  一、引入zookeeper的jar包(maven方式)

  <dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.8</version>
  </dependency>

  二、调用相关代码  

package com.zookeeper.api.javaApi;

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

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;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

public class NativeApi implements Watcher{

    private final static String connectString = "192.168.194.128:2181,192.168.194.129:2181,192.168.194.130:2181";
    
    private static CountDownLatch countDownLatch = new CountDownLatch(1);
    private static ZooKeeper zookeeper;
    private static Stat stat = new Stat();
    
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        zookeeper = new ZooKeeper(connectString,5000,new NativeApi());
        countDownLatch.await();
        System.out.println(zookeeper.getState()+"\n");
        //测试临时节点
        testEphemeralNode();
        //测试持久化节点
        testPersistentNode();
        
    }
    
    /**
     * 测试持久化节点
     * @throws KeeperException
     * @throws InterruptedException
     */
    private static void testPersistentNode() throws KeeperException,
        InterruptedException {
        String node = "/test1";
        String createRst = zookeeper.create(node, "value1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println("创建持久节点"+node+"成功:"+createRst+"\n");
        TimeUnit.SECONDS.sleep(3);
        
        byte[] bData = zookeeper.getData(node, true, stat);
        System.out.println("得到"+node+"持久节点创建时的值:"+new String(bData)+"\n");
        TimeUnit.SECONDS.sleep(1);
        
        zookeeper.setData(node, "value2".getBytes(), -1);//-1表示强制执行
        bData = zookeeper.getData(node, true, stat);
        System.out.println("得到"+node+"持久节点修改后的值:"+new String(bData)+"\n");
        TimeUnit.SECONDS.sleep(1);
        
        zookeeper.delete(node, -1);
        System.out.println("删除持久节点"+node+"成功"+"\n");
        TimeUnit.SECONDS.sleep(1);
        
        String node1 = "/pNode1";
        createRst = zookeeper.create(node1, "pValue1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println("创建持久节点"+node1+"成功:"+createRst+"\n");
        TimeUnit.SECONDS.sleep(1);
        
        Stat pstat = zookeeper.exists(node1+"/cpNode1", true);
        if(pstat==null){
            createRst = zookeeper.create(node1+"/cpNode1", "cpValue1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            TimeUnit.SECONDS.sleep(2);
            System.out.println("创建持久节点"+node+"/cpNode1成功:"+createRst+"\n");
            zookeeper.setData(node1+"/cpNode1", "cpValue2".getBytes(), -1);
        }
        
        List<String> childrens = zookeeper.getChildren(node1, true);
        System.out.println(childrens+"\n");
        
        zookeeper.delete(node1+"/cpNode1", -1);
        TimeUnit.SECONDS.sleep(3);
        System.out.println("删除持久节点"+node1+"/cpNode1成功"+"\n");
        
        zookeeper.delete(node1, -1);
        System.out.println("删除持久节点"+node1+"成功"+"\n");
        TimeUnit.SECONDS.sleep(1);
    }
    

    /**
     * 临时节点测试
     * @throws KeeperException
     * @throws InterruptedException
     */
    private static void testEphemeralNode() throws KeeperException,
            InterruptedException {
        String node = "/test1";
        String createRst = zookeeper.create(node, "value1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println("创建临时节点"+node+"成功:"+createRst+"\n");
        TimeUnit.SECONDS.sleep(1);
        
        byte[] bData = zookeeper.getData(node, true, stat);
        System.out.println("得到"+node+"临时节点创建时的值:"+new String(bData)+"\n");
        TimeUnit.SECONDS.sleep(1);
        
        zookeeper.setData(node, "value2".getBytes(), -1);
        bData = zookeeper.getData(node, true, stat);
        System.out.println("得到"+node+"临时节点修改后的值:"+new String(bData)+"\n");
        TimeUnit.SECONDS.sleep(1);
        
        zookeeper.delete(node, -1);
        System.out.println("删除临时节点"+node+"成功");
        TimeUnit.SECONDS.sleep(1);
        
        //临时节点下不可以再创建节点
    }

    public void process(WatchedEvent event) {
        if(event.getState()==Event.KeeperState.SyncConnected){
            System.out.println("call process**************事件类型:"+event.getType());
            if(event.getType()==Event.EventType.None && null==event.getPath()){
                countDownLatch.countDown();
                System.out.println(event.getState()+"-->"+event.getType());
            }else if(event.getType()==Event.EventType.NodeDataChanged){
                try {
                    String data = new String(zookeeper.getData(event.getPath(), true, stat));
                    System.out.println("call process**************数据变更触发路径:"+event.getPath()+"-->节点的值:"+data);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else if(event.getType()==Event.EventType.NodeChildrenChanged){
                try {
                    String data = new String(zookeeper.getData(event.getPath(), true, stat));
                    System.out.println("call process**************子节点数据变更触发路径:"+event.getPath()+"-->节点的值:"+data);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else if(event.getType()==Event.EventType.NodeCreated){
                try {
                    String data = new String(zookeeper.getData(event.getPath(), true, stat));
                    System.out.println("call process**************节点创建触发路径:"+event.getPath()+"-->节点的值:"+data);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else if(event.getType()==Event.EventType.NodeDeleted){
                System.out.println("call process**************数据删除触发路径:"+event.getPath());
            }
        }
    }
    
}

  三、显示测试结果

    1、测试临时节点结果    

call process**************事件类型:None
SyncConnected-->None
CONNECTED

创建临时节点/test1成功:/test1

得到/test1临时节点创建时的值:value1

call process**************事件类型:NodeDataChanged
call process**************数据变更触发路径:/test1-->节点的值:value2
得到/test1临时节点修改后的值:value2

call process**************事件类型:NodeDeleted
call process**************数据删除触发路径:/test1
删除临时节点/test1成功

 

    2、测试持久化节点结果

call process**************事件类型:None
SyncConnected-->None
CONNECTED

创建持久节点/test1成功:/test1

得到/test1持久节点创建时的值:value1

call process**************事件类型:NodeDataChanged
call process**************数据变更触发路径:/test1-->节点的值:value2
得到/test1持久节点修改后的值:value2

call process**************事件类型:NodeDeleted
call process**************数据删除触发路径:/test1
删除持久节点/test1成功

创建持久节点/pNode1成功:/pNode1

call process**************事件类型:NodeCreated
call process**************节点创建触发路径:/pNode1/cpNode1-->节点的值:cpValue1
创建持久节点/test1/cpNode1成功:/pNode1/cpNode1

call process**************事件类型:NodeDataChanged
call process**************数据变更触发路径:/pNode1/cpNode1-->节点的值:cpValue2
[cpNode1]

call process**************事件类型:NodeDeleted
call process**************数据删除触发路径:/pNode1/cpNode1
call process**************事件类型:NodeChildrenChanged
call process**************子节点数据变更触发路径:/pNode1-->节点的值:pValue1
删除持久节点/pNode1/cpNode1成功

call process**************事件类型:NodeDeleted
call process**************数据删除触发路径:/pNode1
删除持久节点/pNode1成功

 

posted on 2017-10-18 14:49  ☆雪无痕☆  阅读(188)  评论(0编辑  收藏  举报

导航