ZooKeeper 客戶端編程

 

一、準備,搭建ZooKeeper 集羣html

參考 https://www.cnblogs.com/jonban/p/zookeeper.htmljava

 

二、新建 Maven 項目 zookeeper-clientnode

 

三、pom.xmlapache

<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>

 

四、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();
        }
    }

}

 

可按順序進行測試session

 

 

ZooKeeper 客戶端編程maven

.測試

相關文章
相關標籤/搜索