ZooKeeper 客户端编程

 

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 客户端编程

.

posted @ 2019-05-02 10:55  诚信天下  阅读(460)  评论(0编辑  收藏  举报