Zookeeper JavaAPI操作

使用原生的ZookeeperAPI操作ZooKeeper很不方便,下面将使用Curator操作Zookeeper,实现zookeeper节点的基本操作(增删改查+注册监听)。

  • 创建一个maven工程,引入相关依赖
<dependencies>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.30</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.30</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.2.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.2.0</version>
        </dependency>
  • 创建一个测试类
  1. 获取链接
private CuratorFramework client;

    /*
     * 建立连接
     * */

    @Before
    public void testConnect() {

        String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);
        client = CuratorFrameworkFactory.builder()
                .connectString(connectString)
//                名称空间,以此节点为查询起点
//                .namespace("app")
                .retryPolicy(retryPolicy).build();
        client.start();
    }
  1. 创建节点
 /*
     * 创建节点: 持久/临时 带序号/不带序号
     * */
    @Test
    public void testCreate() throws Exception {
        client.create()
                .creatingParentsIfNeeded() //如果父节点不存在就自动创建
                .withMode(CreateMode.PERSISTENT_SEQUENTIAL) //设置节点类型
                .forPath("/app2/app22", "app2".getBytes()); //设置节点path和数据
    }
  1. 查询数据
 /*
     * 查询数据
     * */
    @Test
    public void testGetData() throws Exception {
        byte[] bytes = client.getData().forPath("/app");
        System.out.println(new String(bytes));
    }

    /*
     * 查询子节点
     * */
    @Test
    public void testGetChildren() throws Exception {
        List<String> path = client.getChildren().forPath("/");
        System.out.println(path);
    }

    /*
     * 查询状态信息
     * */
    @Test
    public void testGetState() throws Exception {
        Stat status = new Stat();
        client.getData().storingStatIn(status).forPath("/app");
        System.out.println(status);
    }
  1. 修改数据
/*
    * 修改节点(根据版本号修改)
    * */
    @Test
    public void testSetData() throws Exception {
        //根据版本号修改,避免多个用户同时操作数据出现错误。
        Stat status = new Stat();
        client.getData().storingStatIn(status).forPath("/app");
        int version=status.getVersion();
        client.setData().withVersion(version).forPath("/app","app".getBytes());
    }
  1. 删除节点
/*
    * 删除节点
    * */
    @Test
    public void testDelete() throws Exception {
        client.delete()
                .guaranteed() //使删除必须成功(防止网络抖动,重试几次)
                .deletingChildrenIfNeeded() //相当于deleteall,连同子节点一起删除
                .forPath("/app");
    }

    /*
     * 删除后回调
     * */
    @Test
    public void testDeleteINBackground() throws Exception {
        client.delete()
                .guaranteed()
                .inBackground((client, event) -> {
                    System.out.println("我被删除了");
                    System.out.println(event);
                })
                .forPath("/app");
    }
  1. 三种注注册监听
/*
     * NodeCache:给指定的一个节点注册监听器
     * */
    @Test
    public void testNodeCache() throws Exception {
        //1.创建NodeCache对象
        NodeCache nodeCache = new NodeCache(client, "/app1");
        //2.注册监听
        nodeCache.getListenable().addListener(() -> {
            System.out.println("节点变化了...");
            //获取变化的信息
            byte[] data = nodeCache.getCurrentData().getData();
            System.out.println("data变为了:"+new String(data));
        });
        //3.开启监听
        nodeCache.start(true); //如果设置为TRUE,在开启监听时加载缓存数据

        //小技巧,让客户端会话不关闭
        while (true){

        }
    }

    /*
     * PathChildrenCache:监听某个节点的所有子节点(不包括自己)
     * */
    @Test
    public void testPathChildrenCache() throws Exception {
        PathChildrenCache pathChildrenCache = new PathChildrenCache(client, "/app1", true);
        pathChildrenCache.getListenable().addListener((client,event)->{
            System.out.println("子节点变化了...");
            System.out.println(event);
        });
        pathChildrenCache.start();
        //小技巧,让客户端会话不关闭
        while (true){

        }
    }

    /*
     * TreeCache:监听某个节点的及所有子节点(包括自己)
     * */
    @Test
    public void testTreeCache() throws Exception {
        TreeCache treeCache = new TreeCache(client, "/");
        treeCache.getListenable().addListener((client, event) -> {
            System.out.println("子节点变化了...");
            System.out.println(event);
        });
        treeCache.start();
        //小技巧,让客户端会话不关闭
        while (true){

        }
    }
  1. 关闭连接
/*
     * 关闭连接
     * */
    @After
    public void close() {
        if (client != null) {
            client.close();
        }
    }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值