ZooKeeper 客户端编程

博客介绍了ZooKeeper相关操作,包括参考特定链接搭建ZooKeeper集群,新建Maven项目zookeeper - client,给出pom.xml和ZookeeperClientTest.java文件,还提到可按顺序进行测试,涉及ZooKeeper客户端编程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

1、准备,搭建ZooKeeper 集群

参考 https://www.cnblogs.com/jonban/p/zookeeper.html

 

2、新建 Maven 项目 zookeeper-client

 

3、pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
        http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.java.zookeeper</groupId>
    <artifactId>zookeeper-client</artifactId>
    <version>1.0.0</version>


    <dependencies>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.14</version>
        </dependency>


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 

4、ZookeeperClientTest.java

package com.java.zookeeper;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Test;

/**
 * zookeeper 客户端测试
 * 
 * @author Logan
 * @createDate 2019-05-02
 * @version 1.0.0
 *
 */
public class ZookeeperClientTest {

    private static final String connectString = "s1:2181,s2181,s3:2181";

    @Test
    public void create() {
        try {
            ZooKeeper zk = new ZooKeeper(connectString, 2000, null);
            String result = zk.create("/test", "zookeeper client test root path".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            System.out.println(result);

            result = zk.create("/test/node", "Hello zookeeper".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            System.out.println(result);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void get() {
        try {
            ZooKeeper zk = new ZooKeeper(connectString, 2000, null);

            // Stat stat = new Stat();
            // stat.setVersion(0);

            /* 下面是Stat 属性说明,<a href='https://zookeeper.apache.org/doc/r3.4.14/zookeeperProgrammers.html'>官网参考地址 </a> */
            // ZooKeeper Stat Structure
            // The Stat structure for each znode in ZooKeeper is made up of the following fields:

            // czxid The zxid of the change that caused this znode to be created.
            // mzxid The zxid of the change that last modified this znode.
            // pzxid The zxid of the change that last modified children of this znode.
            // ctime The time in milliseconds from epoch when this znode was created.
            // mtime The time in milliseconds from epoch when this znode was last modified.
            // version The number of changes to the data of this znode.
            // cversion The number of changes to the children of this znode.
            // aversion The number of changes to the ACL of this znode.
            // ephemeralOwner The session id of the owner of this znode if the znode is an ephemeral node. If it is not an ephemeral node, it will be zero.
            // dataLength The length of the data field of this znode.
            // numChildren The number of children of this znode.
            byte[] data = zk.getData("/test", null, null);
            System.out.println(new String(data));

            data = zk.getData("/test/node", null, null);
            System.out.println(new String(data));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void deletePath() {
        try {
            ZooKeeper zk = new ZooKeeper(connectString, 2000, null);

            // 节点实际版本号,如果没有对应版本,删除会抛出异常
            int dataVersion = 0;
            zk.delete("/test/node", dataVersion);
            zk.delete("/test", dataVersion);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

 

可按顺序进行测试

 

 

ZooKeeper 客户端编程

.

转载于:https://www.cnblogs.com/jonban/p/10801807.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值