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

    .

  • 相关阅读:
    网络时钟同步SOCKET代码
    NTP时间同步之同步时钟要领
    3分钟 Markdown 快速入门(超详细)(Day_33)
    如何在idea中将项目生成API文档(超详细)(Day_32)
    多条件分页 (Day_31)
    常见判断错误 (Day_30)
    HTML 标签隐藏占用空间与不占用空间(Day_29)
    EasyUI_使用datagrid分页 (Day_28)
    (xxx) is not defined at HTMLInputElement.onblur(Day_27)
    EasyUI系列—点击按钮加载tabs_day26
  • 原文地址:https://www.cnblogs.com/jonban/p/10801807.html
Copyright © 2020-2023  润新知