zookeeper之簡單API使用

一、簡單客戶端API

package com.spring.test;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

public class Zkclient {
    private String servers="192.168.1.14:2181,192.168.1.14:2182,192.168.1.14:2183";
    private int sessionTime=6000;
    ZooKeeper zkCli=null;
    @Before
    public void init() throws IOException {
        zkCli=new ZooKeeper(servers, sessionTime, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                try {
                    List<String> list=zkCli.getChildren("/",true);
                    for (String v:list){
                        System.out.println(v);
                    }
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    //創建節點
    @Test
    public void test1(){
        System.out.println("===");
        String path = null;
        try {
            path = zkCli.create("/test2", "world".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(path);
    }

    //判斷節點是否存在
    @Test
    public void test2(){
        try {
            Stat exists=zkCli.exists("/hello",false);
            System.out.println("========"+exists==null?"節點不存在":"節點存在");
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //修改節點內容
    @Test
    public void test3(){
        try {
            zkCli.setData("/hello","updateData".getBytes(),-1);
            System.out.println("====修改節點===");
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

二、

package com.spring.test;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

/**
 * 監聽單節點內容
 * @date 2020-06-19 11:36
 */
public class ZKListener {
    private  static String servers="192.168.1.14:2181,192.168.1.14:2182,192.168.1.14:2183";
    private static int sessionTime=6000;

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        ZooKeeper zkCli=new ZooKeeper(servers, sessionTime, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {

            }
        });


        byte[] data=zkCli.getData("/hello", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("監聽路徑爲:"+watchedEvent.getPath());
                System.out.println("監聽類型爲:"+watchedEvent.getType());
                System.out.println("被監聽內容修改了!!!");
            }
        },null);

        System.out.println("被監聽內容爲:"+data.toString());
        Thread.sleep(3000);
        //修改節點內容
        zkCli.setData("/hello","17".getBytes(),-1);
        Thread.sleep(Long.MAX_VALUE);

    }
}

三、監控目錄
監控根節點demo:

package com.spring.test;

import org.apache.zookeeper.*;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

/**
 * 監聽根節點目錄
 */
public class ZKListener {
    private  static String servers="192.168.1.14:2181,192.168.1.14:2182,192.168.1.14:2183";
    private static int sessionTime=6000;
     List<String> children ;
    ZooKeeper zkCli=null;
     @Test
     public void test1() throws IOException, KeeperException, InterruptedException{
//    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
         zkCli=new ZooKeeper(servers, sessionTime, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                    System.out.println("==監控中。。。。。");
            }
        });
        //監聽目錄
        children =zkCli.getChildren("/", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("監聽路徑爲:"+watchedEvent.getPath());
                System.out.println("監聽類型爲:"+watchedEvent.getType());
                System.out.println("被監聽目錄被修改了!!!");
                System.out.println("目錄被修改==start");
                //重新獲取監聽目錄信息
                children=getList();
                for (String child:children){

                    System.out.println(child);
                }
                System.out.println("目錄被修改==end");
            }
        });
        System.out.println("目錄初始化==start");
        for (String child:children){
            System.out.println(child);
        }
        System.out.println("目錄初始化==end");

        Thread.sleep(3000);
        //創建新目錄節點
        String path = null;
        try {
            path = zkCli.create("/test7", "test7Value".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        zkCli.delete("/test5",-1);
         Thread.sleep(Long.MAX_VALUE);

    }

    //獲取節點目錄列表
    List<String> getList(){
        try {
            List<String> list=zkCli.getChildren("/",true);
            return list;
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章