1.3 Zookeeper的Java API操作

1.新建Maven项目

2.导入依赖

    <dependencies>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.5.9</version>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

3.API的使用

3.1 连接Zookeeper服务端

package com.biao.test;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Test;

import java.io.IOException;

public class Test1 {

    private String connectString="192.168.134.129:2181,192.168.134.131:2181,192.168.134.132:2181";

    private int sessionTimeout=5000;

    /**
     * 连接Zookeeper服务端
     * @throws IOException
     */
    @Test
    public void test1() throws IOException {

        ZooKeeper zooKeeper=new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            /**
             * 触发监听事件的回调方法
             * @param watchedEvent
             */
            public void process(WatchedEvent watchedEvent) {
                System.out.println("触发了....");
            }
        });
        System.out.println("-->"+zooKeeper);
    }
}

结果
在这里插入图片描述

3.2 创建节点

3.2.1 创建节点前将连接操作前置

    private String connectString="192.168.134.129:2181,192.168.134.131:2181,192.168.134.132:2181";

    private int sessionTimeout=100000;

    ZooKeeper zooKeeper=null;

    /**
     * 连接Zookeeper服务端
     * @throws IOException
     */
    @Before
    public void test1() throws IOException {

        zooKeeper=new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            /**
             * 触发监听事件的回调方法
             * @param watchedEvent
             */
            public void process(WatchedEvent watchedEvent) {
                System.out.println("触发了....");
            }
        });
    }

在这里插入图片描述

3.2.2 创建节点

    /**
     * 创建节点
     * @throws Exception
     */
    @Test
    public void test2() throws Exception{
        String path = zooKeeper.create("/apptest",//节点路径
                "HelloZookeeper".getBytes(), //节点的数据
                ZooDefs.Ids.OPEN_ACL_UNSAFE, //权限
                CreateMode.PERSISTENT);//节点类型
        System.out.println(path);

    }

结果
在这里插入图片描述
在这里插入图片描述

3.3 判断节点是否存在

    /**
     * 判断节点是否存在
     * @throws Exception
     */
    @Test
    public void exist() throws Exception{
        //true表示的是使用Zookeeper中的watch
        Stat exists = zooKeeper.exists("/apptest", true);
        if (exists != null){
            System.out.println("节点存在"+exists.getNumChildren());
        }else {
            System.out.println("节点不存在");
        }
    }

结果
在这里插入图片描述

3.4 获取某个节点下面的所有的子节点

    /**
     * 获取某个节点下面的所有的节点
     * @throws Exception
     */
    @Test
    public void getChildrens() throws Exception{
        List<String> childrens = zooKeeper.getChildren("/apptest", true);
        for (String children:childrens
             ) {
            //获取子节点中数据
            byte[] data = zooKeeper.getData("/apptest/" + children, false, null);
            System.out.println(children+":"+new String(data));
        }
    }

结果
在这里插入图片描述

3.5 修改节点的内容

    /**
     * 修改节点的内容
     * @throws Exception
     */
    @Test
    public void setData() throws Exception{
        //-1 不指定版本 自动维护
        Stat stat = zooKeeper.setData("/apptest/a1", "setaaa".getBytes(), -1);
        System.out.println(stat);
    }

结果
在这里插入图片描述
在这里插入图片描述

3.6 删除节点(不支持删除非空)

    /**
     * 删除节点
     * @throws Exception
     */
    @Test
    public void deleteNode() throws Exception{
        zooKeeper.delete("/app3",-1);
    }

结果

在这里插入图片描述
在这里插入图片描述

3.7 监听节点下的子节点的变更

    /**
     * 监听节点下的子节点变更
     * @throws Exception
     */
    @Test
    public void nodeChildrenChange() throws Exception{
        List<String> list = zooKeeper.getChildren("/apptest", new Watcher() {
            /**
             *             None(-1),
             *             NodeCreated(1),
             *             NodeDeleted(2),
             *             NodeDataChanged(3),
             *             NodeChildrenChanged(4),
             *             DataWatchRemoved(5),
             *             ChildWatchRemoved(6);
             * @param watchedEvent
             */
            public void process(WatchedEvent watchedEvent) {
                System.out.println("--->"+ watchedEvent.getType());
            }
        });
        for (String s :
                list) {
            System.out.println(s);
        }
        Thread.sleep(Integer.MAX_VALUE);
    }

3.8 监听节点内容变更

    /*** 监听节点内容变更 */
    @Test
    public void nodeDataChanged() throws Exception{
        byte[] data = zooKeeper.getData("/apptest/a1", new Watcher() {

            public void process(WatchedEvent watchedEvent) {
                System.out.println("--->" + watchedEvent.getType());
            }
            }, null);
        System.out.println("--->" + new String(data));
        Thread.sleep(Integer.MAX_VALUE); 
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值