8.1 连接方法

package com.zookeeper.day01;

import org.apache.zookeeper.*;

import java.io.IOException;

public class ZookeeperWatcher implements Watcher {
    //连接地址
    private static final String ZK_ADDRESS="0.0.0.0:2181";
    //超时时间
    private static final Integer ZK_TIMEOUT=2000;

    private ZooKeeper zooKeeper;

    public ZookeeperWatcher() {
        openConnection(ZK_ADDRESS,ZK_TIMEOUT);

    }

    /**
     * 回调方法,监听连接,监听增删改节点
     * @param watchedEvent
     */
    @Override
    public void process(WatchedEvent watchedEvent) {
        //获取当前的状态
        Event.KeeperState keeperState = watchedEvent.getState();
        //获取通知类型
        Event.EventType eventType=watchedEvent.getType();
        //获取操作节点的路径
        String path=watchedEvent.getPath();

        System.out.println("当前状态为:"+keeperState+"\t通知类型为:"+eventType+"\t操作的节点路径:"+path);

        //已经成功连接
        if (Event.KeeperState.SyncConnected==keeperState){
            //连接状态
            if (Event.EventType.None==eventType){
                System.out.println("--------连接事件回调--------");
            }
        }
    }

    /**
     * 连接方法
     */
    public void openConnection(String zk_address,Integer zk_timeout){
        try {
            zooKeeper = new ZooKeeper(zk_address, zk_timeout, this);
            System.out.println("Zookeeper连接成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭连接
     */
    public void closeConnection(){
        if (zooKeeper!=null){
            try {
                zooKeeper.close();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }



    public static void main(String[] args) {
        //创建实体类对象
        ZookeeperWatcher zookeeperWatcher=new ZookeeperWatcher();
        //关闭连接
        zookeeperWatcher.closeConnection();
    }
}
View Code 

控制台效果:

  

8.2 添加节点

package com.zookeeper.day01;

import org.apache.zookeeper.*;

import java.io.IOException;

public class ZookeeperWatcher implements Watcher {
    //连接地址
    private static final String ZK_ADDRESS="0.0.0.0:2181";
    //超时时间
    private static final Integer ZK_TIMEOUT=2000;

    private ZooKeeper zooKeeper;

    public ZookeeperWatcher() {
        openConnection(ZK_ADDRESS,ZK_TIMEOUT);

    }

    /**
     * 回调方法,监听连接,监听增删改节点
     * @param watchedEvent
     */
    @Override
    public void process(WatchedEvent watchedEvent) {
        //获取当前的状态
        Event.KeeperState keeperState = watchedEvent.getState();
        //获取通知类型
        Event.EventType eventType=watchedEvent.getType();
        //获取操作节点的路径
        String path=watchedEvent.getPath();

        System.out.println("当前状态为:"+keeperState+"\t通知类型为:"+eventType+"\t操作的节点路径:"+path);

        //已经成功连接
        if (Event.KeeperState.SyncConnected==keeperState){
            //连接状态
            if (Event.EventType.None==eventType){
                System.out.println("--------连接事件回调--------");
            }
            //创建节点
            if (Event.EventType.NodeCreated==eventType){
                System.out.println("------创建节点事件回调------");
            }
        }
    }

    /**
     * 连接方法
     */
    public void openConnection(String zk_address,Integer zk_timeout){
        try {
            zooKeeper = new ZooKeeper(zk_address, zk_timeout, this);
            System.out.println("Zookeeper连接成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭连接
     */
    public void closeConnection(){
        if (zooKeeper!=null){
            try {
                zooKeeper.close();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 添加节点
     */
    public void createNode(String path,String data){
        try {
            //启动监听
            zooKeeper.exists(path,true);
            String result = zooKeeper.create(path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            System.out.println("创建节点成功:"+result);
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }



    public static void main(String[] args) {
        //创建实体类对象
        ZookeeperWatcher zookeeperWatcher=new ZookeeperWatcher();
        //调用创建节点方法
        zookeeperWatcher.createNode("/zkNode","zkValue");
        //关闭连接
        zookeeperWatcher.closeConnection();
    }
}
View Code

控制台效果:

   

8.3 修改节点

package com.zookeeper.day01;

import org.apache.zookeeper.*;

import java.io.IOException;

public class ZookeeperWatcher implements Watcher {
    //连接地址
    private static final String ZK_ADDRESS="0.0.0.0:2181";
    //超时时间
    private static final Integer ZK_TIMEOUT=2000;

    private ZooKeeper zooKeeper;

    public ZookeeperWatcher() {
        openConnection(ZK_ADDRESS,ZK_TIMEOUT);

    }

    /**
     * 回调方法,监听连接,监听增删改节点
     * @param watchedEvent
     */
    @Override
    public void process(WatchedEvent watchedEvent) {
        //获取当前的状态
        Event.KeeperState keeperState = watchedEvent.getState();
        //获取通知类型
        Event.EventType eventType=watchedEvent.getType();
        //获取操作节点的路径
        String path=watchedEvent.getPath();

        System.out.println("当前状态为:"+keeperState+"\t通知类型为:"+eventType+"\t操作的节点路径:"+path);

        //已经成功连接
        if (Event.KeeperState.SyncConnected==keeperState){
            //连接状态
            if (Event.EventType.None==eventType){
                System.out.println("--------连接事件回调--------");
            }
            //修改节点
            if(Event.EventType.NodeDataChanged==eventType){
                System.out.println("------修改节点事件回调------");
            }

    /**
     * 连接方法
     */
    public void openConnection(String zk_address,Integer zk_timeout){
        try {
            zooKeeper = new ZooKeeper(zk_address, zk_timeout, this);
            System.out.println("Zookeeper连接成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭连接
     */
    public void closeConnection(){
        if (zooKeeper!=null){
            try {
                zooKeeper.close();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 修改节点
     */
    public void setNode(String path,String data){
        try {
            //启动监听
            zooKeeper.exists(path,true);
            zooKeeper.setData(path, data.getBytes(), -1);
            System.out.println("修改节点成功");
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //创建实体类对象
        ZookeeperWatcher zookeeperWatcher=new ZookeeperWatcher();
        //调用修改节点方法
        zookeeperWatcher.setNode("/zkNode","zkValueSet");
        //关闭连接
        zookeeperWatcher.closeConnection();
    }
}
View Code 

控制台效果:

   

8.4 删除节点

package com.zookeeper.day01;

import org.apache.zookeeper.*;

import java.io.IOException;

public class ZookeeperWatcher implements Watcher {
    //连接地址
    private static final String ZK_ADDRESS="0.0.0.0:2181";
    //超时时间
    private static final Integer ZK_TIMEOUT=2000;

    private ZooKeeper zooKeeper;

    public ZookeeperWatcher() {
        openConnection(ZK_ADDRESS,ZK_TIMEOUT);

    }

    /**
     * 回调方法,监听连接,监听增删改节点
     * @param watchedEvent
     */
    @Override
    public void process(WatchedEvent watchedEvent) {
        //获取当前的状态
        Event.KeeperState keeperState = watchedEvent.getState();
        //获取通知类型
        Event.EventType eventType=watchedEvent.getType();
        //获取操作节点的路径
        String path=watchedEvent.getPath();

        System.out.println("当前状态为:"+keeperState+"\t通知类型为:"+eventType+"\t操作的节点路径:"+path);

        //已经成功连接
        if (Event.KeeperState.SyncConnected==keeperState){
            //连接状态
            if (Event.EventType.None==eventType){
                System.out.println("--------连接事件回调--------");
            }
            //删除节点
            if(Event.EventType.NodeDeleted==eventType){
                System.out.println("------删除节点事件回调------");
            }
        }
    }

    /**
     * 连接方法
     */
    public void openConnection(String zk_address,Integer zk_timeout){
        try {
            zooKeeper = new ZooKeeper(zk_address, zk_timeout, this);
            System.out.println("Zookeeper连接成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭连接
     */
    public void closeConnection(){
        if (zooKeeper!=null){
            try {
                zooKeeper.close();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 删除节点
     */
    public void deleteNode(String path){
        try {
            //启动监听
            zooKeeper.exists(path,true);
            zooKeeper.delete(path, -1);
            System.out.println("删除节点成功");
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //创建实体类对象
        ZookeeperWatcher zookeeperWatcher=new ZookeeperWatcher();
        //调用删除节点方法
        zookeeperWatcher.deleteNode("/zkNode");
        //关闭连接
        zookeeperWatcher.closeConnection();
    }
}
View Code

控制台效果:

  

01-05 20:33