ZookeeperAPI

本文档介绍了如何使用Apache ZooKeeper进行包依赖管理,通过示例展示了如何连接Zookeeper服务器、创建持久和临时节点,以及非阻塞监听节点变化的过程。最后演示了ZooKeeper连接的关闭操作。
摘要由CSDN通过智能技术生成

1.添加包依赖

<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
            <type>pom</type>
        </dependency>

2.连接zookeeper

@Before
    public void conn() throws IOException {
        ZooKeeper zk = new ZooKeeper("master:2181,node1:2181,node2:2181",30000,null);

    }

3.在zk上创建节点

 @Test
    /**
     * 在zk上创建节点  并保存数据
     * ZooDefs.Ids.OPEN_ACL_UNSAFE 不做任何权限认证
     * CreateMode.PERSISTENT  创建永久节点
     * CreateMode.EPHEMERAL 创建临时节点,连接断开自动删除
     *
     */
    public void createZNode() throws KeeperException, InterruptedException {
        zk.create("/test1",
                "data1".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT
        );
    }

 @Test
    public void createTempZNode() throws KeeperException, InterruptedException {
        zk.create("/test2",
                "data1".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.EPHEMERAL
        );
    }

4.监听创建的节点是否有变化

 @Test
    /**
     * 非阻塞监听 /test1是否有变化
     */
    public void watchEvent(){

        try {
            zk.exists("/test1", new Watcher() {
                public void process(WatchedEvent event) {
                    System.out.println(event);
                    System.out.println("/test1有变化");
                }
            });
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        while (true){

        }
    }

5.关闭

 @After
    public void close() {
        try {
            zk.close();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

完整代码

public class ZKAPI {
    ZooKeeper zk=null;

    @Before
    public void conn() throws IOException {
         zk = new ZooKeeper("master:2181,node1:2181,node2:2181",30000,null);

    }
    @Test
    /**
     * 在zk上创建节点  并保存数据
     * ZooDefs.Ids.OPEN_ACL_UNSAFE 不做任何权限认证
     * CreateMode.PERSISTENT  创建永久节点
     * CreateMode.EPHEMERAL 创建临时节点,连接断开自动删除
     *
     */
    public void createZNode() throws KeeperException, InterruptedException {
        zk.create("/test1",
                "data1".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT
        );
    }
    @Test
    public void createTempZNode() throws KeeperException, InterruptedException {
        zk.create("/test2",
                "data1".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.EPHEMERAL
        );
    }

    @Test
    /**
     * 非阻塞监听 /test1是否有变化
     */
    public void watchEvent(){

        try {
            zk.exists("/test1", new Watcher() {
                public void process(WatchedEvent event) {
                    System.out.println(event);
                    System.out.println("/test1有变化");
                }
            });
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        while (true){

        }
    }

    @After
    public void close() {
        try {
            zk.close();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值