Zookeeper简介以及部署

  • 推荐一个IDEA方便查看Zookeeper里面数据的工具:zoolyic

1. Zookeeper介绍

  • 官网
  • Zookeeper是一个开源的分布式协调框架,每个框架的出现都是为了解决某些或者某类问题,而Zookeeper的出现是为了解决某些复杂且容易出错的问题,Zookeeper把这些问题全部都封装起来,并提供一些简单的接口便于用户去使用。
  • Zookeeper是一个典型的解决数据一致性的分布式框架,其他的分布式应用程序可以根据Zookeeper来实现订阅/发布功能,负载均衡,命名服务,分布式协调/通知,集群管理,Master选举,分布式锁和分布式队列等功能。
  • 在使用Zookeeper的时候最好使用集群,使用的机器数量为2N+1台,是为了在选举的时候避免出现平票的情况。Zookeeper有很强大的容错性,最多可以宕机的个数为:(Zookeeper机器的个数-1)/2台,假设我们有三台Zookeeper的集群,可以宕机的个数为(3-1)/2=1台。
  • Zookeeper里面的目录结构为树状结构,就是和操作系统的目录结构是一样的。
  • Zookeeper里面又分成临时节点和永久节点,临时节点:当前Session有效,即在当前Session创建了一个临时节点,只可以在当前Session使用,并且临时节点下面不能有子节点,即临时节点就是当前目录(树状结构)的最底层。
  • 永久节点:在当前Session中创建一个数据节点:ZNode,不仅当前Session有效,而且其他Session也有效,并且永久节点下面可以创建子节点,ZNode上面可以设置权限。
  • 监听器:Watcher,在ZNode上面添加监听,但是监听是一次性的,可以监听最底层的ZNode,或者监听目录。
  • Zookeeper里面有四种形式:1. 永久/持久形式;2. 永久/持久带顺序编号形式;3. 临时形式;4. 临时带顺序编号形式。

2. Zookeeper架构

ZK架构

  • Zookeeper中分为Leader、Follower、Observer这三种角色,Zookeeper3.3.0版本之后增加Observer角色,Zookeeper集群中的所有机器通过一个Leader选举过程来选定一台机器作为Leader机器,Leader机器可以对客户端提供读、写服务,Follower只能提供读取数据的服务,Observer角色也是只有读取数据的权限,但是Observer和Follower的区别就在于,Observer不参与Leader的选举,只是增加Zookeeper集群读取数据的并行度。
  • 由于在Zookeeper集群中,只有Leader有写入数据的权限,Follower节点只有读取数据的权限,Zookeeper中当选Leader机器之后,其他Follower节点会对Leader节点增加一个监听器,当Leader节点数据变化时,Follower节点会随着Leader改变而改变。
2.1 Zookeeper–选举机制
  • Zookeeper提供了三种选举机制算法:
    • LeaderElection
    • AuthFastLeaderElection
    • FastLeaderElection(最新默认)
  • 本文重要探讨的是FastLeaderElection,假设我们Zookeeper集群有三个节点,它们的编号依次是:1、2、3,它们的选举过程如下:
  • 机器1首先启动,给自己投票,然后发起投票信息,由于其他机器还没有启动,没有收到反馈信息,机器1一直属于Looking(选举状态)。
  • 机器2启动,给自己投票,同时与之前启动的机器1交换结果,由于机器2的编号大于机器1的编号,所以机器2胜出,此时的投票数正好大于半数,所以机器2当选Leader。
  • 机器3启动,给自己投票,并同时与机器1、2交换信息,由于之前机器2已经当选成Leader,所以机器3成为Follower角色。

3. Zookeeper单节点部署

CDH5.16.2 Zookeeper下载地址

  1. 下载到software目录下面
[root@bigdatatest01 software]# wget http://archive.cloudera.com/cdh5/cdh/5/zookeeper-3.4.5-cdh5.16.2.tar.gz
  1. 解压到app路径
[root@bigdatatest01 software]# tar -xzvf zookeeper-3.4.5-cdh5.16.2.tar.gz -C ~/app/
[root@bigdatatest01 software]# cd ~/app/
[root@bigdatatest01 app]# cd zookeeper-3.4.5-cdh5.16.2/
  1. 修改配置文件
[root@bigdatatest01 zookeeper-3.4.5-cdh5.16.2]# cd conf/
[root@bigdatatest01 conf]# ll
total 12
-rw-rw-r-- 1 root root  535 Jun  3  2019 configuration.xsl
-rw-rw-r-- 1 root root 2693 Jun  3  2019 log4j.properties
-rw-rw-r-- 1 root root  922 Jun  3  2019 zoo_sample.cfg
[root@bigdatatest01 conf]# cp zoo_sample.cfg zoo.cfg
[root@bigdatatest01 conf]# vim zoo.cfg 
dataDir=/root/tmp/zookeeper

### 创建数据目录
[root@bigdatatest01 ~]# cd tmp/
[root@bigdatatest01 tmp]# mkdir zookeeper
[root@bigdatatest01 tmp]# cd zookeeper/
[root@bigdatatest01 zookeeper]# pwd
/root/tmp/zookeeper
  1. 启动Zookeeper服务
[root@bigdatatest01 conf]# cd ../
[root@bigdatatest01 zookeeper-3.4.5-cdh5.16.2]# cd bin/
[root@bigdatatest01 bin]# ll
total 44
-rwxr-xr-x 1 root root  238 Jun  3  2019 README.txt
-rwxr-xr-x 1 root root 1909 Jun  3  2019 zkCleanup.sh
-rwxr-xr-x 1 root root 1049 Jun  3  2019 zkCli.cmd
-rwxr-xr-x 1 root root 1512 Jun  3  2019 zkCli.sh
-rwxr-xr-x 1 root root 1333 Jun  3  2019 zkEnv.cmd
-rwxr-xr-x 1 root root 2599 Jun  3  2019 zkEnv.sh
-rwxr-xr-x 1 root root 1084 Jun  3  2019 zkServer.cmd
-rwxr-xr-x 1 root root 4559 Jun  3  2019 zkServer-initialize.sh
-rwxr-xr-x 1 root root 6246 Jun  3  2019 zkServer.sh
[root@bigdatatest01 bin]# ./zkServer.sh start
JMX enabled by default
Using config: /root/app/zookeeper-3.4.5-cdh5.16.2/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@bigdatatest01 bin]# ./zkServer.sh status
JMX enabled by default
Using config: /root/app/zookeeper-3.4.5-cdh5.16.2/bin/../conf/zoo.cfg
Mode: standalone
  1. 进入Zookeeper客户端
[root@bigdatatest01 bin]# ./zkCli.sh
[zk: localhost:2181(CONNECTING) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1] get /zookeeper
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x0
cversion = -1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1
[zk: localhost:2181(CONNECTED) 2] quit
Quitting...
2021-01-18 11:17:35,101 [myid:] - INFO  [main:ZooKeeper@684] - Session: 0x177137daa0c0000 closed
2021-01-18 11:17:35,101 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@524] - EventThread shut down

4. Zookeeper分布式部署

  • 这里分布式部署是部署在同一个机器的不同进程上面的
  1. 拷贝多个Zookeeper文件
[root@bigdatatest01 app]# pwd
/root/app
[root@bigdatatest01 app]# cp -r zookeeper-3.4.5-cdh5.16.2 zk1
[root@bigdatatest01 app]# cp -r zookeeper-3.4.5-cdh5.16.2 zk2
[root@bigdatatest01 app]# cp -r zookeeper-3.4.5-cdh5.16.2 zk3
  1. 修改配置文件
[root@bigdatatest01 app]# cd zk1/
[root@bigdatatest01 zk1]# vim conf/zoo.cfg
dataDir=/root/tmp/zk1
clientPort=2181
server.1=localhost:2888:3888
server.2=localhost:2889:3889
server.3=localhost:2890:3890
[root@bigdatatest01 zk1]# cd ../zk2/
[root@bigdatatest01 zk2]# vim conf/zoo.cfg
dataDir=/root/tmp/zk2
clientPort=2182
server.1=localhost:2888:3888
server.2=localhost:2889:3889
server.3=localhost:2890:3890
[root@bigdatatest01 zk2]# cd ../zk3
[root@bigdatatest01 zk3]# vim conf/zoo.cfg
dataDir=/root/tmp/zk3
clientPort=2183
server.1=localhost:2888:3888
server.2=localhost:2889:3889
server.3=localhost:2890:3890
  1. 修改数据文件
[root@bigdatatest01 tmp]# mkdir zk1
[root@bigdatatest01 tmp]# mkdir zk2
[root@bigdatatest01 tmp]# mkdir zk3
[root@bigdatatest01 tmp]# pwd
/root/tmp
[root@bigdatatest01 tmp]# cd zk1/
[root@bigdatatest01 zk1]# vim myid
1
[root@bigdatatest01 zk1]# cd ../zk2/
[root@bigdatatest01 zk2]# vim myid
2
[root@bigdatatest01 zk2]# cd ../zk3/
[root@bigdatatest01 zk3]# vim myid
3
  1. 启动ZK集群服务
[root@bigdatatest01 zk3]# cd ../zk1/
[root@bigdatatest01 zk1]# pwd
/root/app/zk1
[root@bigdatatest01 zk1]# bin/zkServer.sh start
JMX enabled by default
Using config: /root/app/zk1/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@bigdatatest01 zk1]# bin/zkServer.sh status
JMX enabled by default
Using config: /root/app/zk1/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
[root@bigdatatest01 zk1]# cd ../zk2/
[root@bigdatatest01 zk2]# bin/zkServer.sh start
JMX enabled by default
Using config: /root/app/zk2/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@bigdatatest01 zk2]# bin/zkServer.sh status
JMX enabled by default
Using config: /root/app/zk2/bin/../conf/zoo.cfg
Mode: leader
[root@bigdatatest01 zk1]# cd ../zk3
[root@bigdatatest01 zk3]# bin/zkServer.sh start
JMX enabled by default
Using config: /root/app/zk3/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@bigdatatest01 zk3]# bin/zkServer.sh status
JMX enabled by default
Using config: /root/app/zk3/bin/../conf/zoo.cfg
Mode: follower

3. Zookeeper Shell常用命令

  • 进入到客户端
[root@bigdatatest01 bin]# pwd
/root/app/zookeeper-3.4.5-cdh5.16.2/bin
[root@bigdatatest01 bin]# ./zkCli.sh 
[zk: localhost:2181(CONNECTED) 0] help
ZooKeeper -server host:port cmd args
	stat path [watch]
	set path data [version]
	ls path [watch]
	delquota [-n|-b] path
	ls2 path [watch]
	setAcl path acl
	setquota -n|-b val path
	history 
	redo cmdno
	printwatches on|off
	delete path [version]
	sync path
	listquota path
	rmr path
	get path [watch]
	create [-s] [-e] path data acl
	addauth scheme auth
	quit 
	getAcl path
	close 
	connect host:port
3.1 创建节点
  • 创建永久节点
[zk: localhost:2181(CONNECTED) 7] create /hadoop 11
Created /hadoop
  • 创建永久顺序节点
[zk: localhost:2181(CONNECTED) 9] create -s /hadoop/spark 11
Created /hadoop/spark0000000000
[zk: localhost:2181(CONNECTED) 10] ls /hadoop
[spark0000000000]
[zk: localhost:2181(CONNECTED) 11] create -s /hadoop/spark 22
Created /hadoop/spark0000000001
[zk: localhost:2181(CONNECTED) 12] ls /hadoop
[spark0000000000, spark0000000001]
  • 创建临时节点
[zk: localhost:2181(CONNECTED) 0]  create -e /cdh 222
Created /cdh
  • 创建临时顺序节点
[zk: localhost:2181(CONNECTED) 2] create -s -e /hadoop/cdh 222
Created /hadoop/cdh0000000002
[zk: localhost:2181(CONNECTED) 3] create -s -e /hadoop/cdh 333
Created /hadoop/cdh0000000003 
[zk: localhost:2181(CONNECTED) 4] ls /hadoop
[cdh0000000002, cdh0000000003, spark0000000000, spark0000000001]
3.2 查看子节点
[zk: localhost:2181(CONNECTED) 5] ls /
[hadoop, zookeeper]
3.3 得到节点数据
[zk: localhost:2181(CONNECTED) 9] get /hadoop
11
cZxid = 0x8
ctime = Mon Jan 18 13:55:19 CST 2021
mZxid = 0x8
mtime = Mon Jan 18 13:55:19 CST 2021
pZxid = 0x12
cversion = 4
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 2
numChildren = 4
  • 第一行是这个节点数据
  • ctime:创建时间;mtime:更新时间
  • dataVersion:数据版本号,默认是0,每次新增1
  • ephemeralOwner是否是临时节点;0x0:是永久节点;0x177139c35140004:这样的是临时节点
  • dataLength:数据的长度
  • numChildren:下面的子节点个数
3.4 删除节点
[zk: localhost:2181(CONNECTED) 14] rmr /hadoop
[zk: localhost:2181(CONNECTED) 15] ls /
[zookeeper, spark]
3.5 添加监听器
  • 节点监听器
[zk: localhost:2181(CONNECTED) 6] stat /hadoop/spark watch
cZxid = 0x1c
ctime = Mon Jan 18 14:11:24 CST 2021
mZxid = 0x1d
mtime = Mon Jan 18 14:12:19 CST 2021
pZxid = 0x1c
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 0
[zk: localhost:2181(CONNECTED) 7] set /hadoop/spark 222

WATCHER::

WatchedEvent state:SyncConnected type:NodeDataChanged path:/hadoop/spark
cZxid = 0x1c
ctime = Mon Jan 18 14:11:24 CST 2021
mZxid = 0x1e
mtime = Mon Jan 18 14:13:03 CST 2021
pZxid = 0x1c
cversion = 0
dataVersion = 2
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
  • 目录监听器
[zk: localhost:2181(CONNECTED) 3] ls /hadoop watch
[]
[zk: localhost:2181(CONNECTED) 4] create /hadoop/spark 22

WATCHER::
Created /hadoop/spark

WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/hadoo

4. Zookeeper 代码

4.1 增加POM文件
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.5-cdh5.16.2</version>
</dependency>
4.2 ZookeeperUtils
package com.xk.bigdata.zookeeper.utils;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.util.List;

public class ZookeeperUtils {

    public static ZooKeeper client = null;

    /**
     * 得到Zookeeper客户端
     *
     * @param connectString
     * @param sessionTimeout
     * @param watcher
     * @throws Exception
     */
    public static void getZookeeperClient(String connectString, int sessionTimeout, Watcher watcher) throws Exception {
        client = new ZooKeeper(connectString, sessionTimeout, watcher);
    }

    /**
     * 关闭Zookeeper客户端
     *
     * @throws Exception
     */
    public static void close() throws Exception {
        if (null != client) {
            client.close();
        }
    }

    /**
     * 创建节点
     *
     * @param path  路径
     * @param value 数据
     * @return
     * @throws Exception
     */
    public static String create(String path, String value) throws Exception {
        return client.create(path, value.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }

    /**
     * 判断节点是否存在
     *
     * @param path 路径
     * @return
     * @throws Exception
     */
    public static Boolean exists(String path) throws Exception {
        return client.exists(path, false) == null ? false : true;
    }

    /**
     * 得到该目录下面的数据
     *
     * @param path 路径
     * @param stat 监听
     * @return
     * @throws Exception
     */
    public static String getData(String path, Stat stat) throws Exception {
        return new String(client.getData(path, false, stat));
    }

    /**
     * 得到所有的子目录
     *
     * @param path
     * @return
     * @throws Exception
     */
    public static List<String> getChildren(String path) throws Exception {
        return client.getChildren(path, false);
    }

    /**
     * 修改路径数据
     *
     * @param path  路径
     * @param value 数据
     * @return
     * @throws Exception
     */
    public static Boolean setData(String path, String value) throws Exception {
        client.setData(path, value.getBytes(), -1);
        Stat stat = new Stat();
        String data = getData(path, stat);
        if (value.equals(data)) {
            return true;
        }
        return false;
    }

    /**
     * 删除节点
     *
     * @param path      路径
     * @param recursive 是否删除下面子节点
     * @throws Exception
     */
    public static void delete(String path, Boolean recursive) throws Exception {
        if (recursive) {
            deleteChildren(path);
            client.delete(path, -1);
        } else {
            client.delete(path, -1);
        }
    }

    /**
     * 删除子节点
     *
     * @param path 路径
     * @throws Exception
     */
    public static void deleteChildren(String path) throws Exception {
        List<String> childrens = getChildren(path);
        if (childrens.isEmpty()) {
            client.delete(path, -1);
        } else {
            for (String children : childrens) {
                deleteChildren(path + "/" + children);
            }
        }
    }

}

ZookeeperUtils Code

4.3 ZookeeperUtilsTest
package com.xk.bigdata.zookeeper.utils;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

public class ZookeeperUtilsTest {

    public String connectString = "bigdatatest01:2181";

    public int sessionTimeout = 5000;

    public String path = "/hadoop";

    public String value = "111";

    @Before
    public void setUp() {
        try {
            ZookeeperUtils.getZookeeperClient(connectString, sessionTimeout, new Watcher() {
                @Override
                public void process(WatchedEvent watchedEvent) {

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

    @After
    public void cleanUp() {
        try {
            ZookeeperUtils.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testCreate() {
        try {
            String result = ZookeeperUtils.create(path, value);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testExists() {
        try {
            Boolean result = ZookeeperUtils.exists(path);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testGetData() {
        Stat stat = new Stat();
        try {
            String result = ZookeeperUtils.getData(path, stat);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testCreateChildren() {
        for (int i = 0; i < 10; i++) {
            try {
                ZookeeperUtils.create(path + "/" + i, i + "");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void testGetChildren() {
        try {
            List<String> childrens = ZookeeperUtils.getChildren(path);
            for (String children : childrens) {
                System.out.println(children);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testSetData() {
        try {
            Boolean result = ZookeeperUtils.setData(path, "2222");
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testDelete() {
        try {
            ZookeeperUtils.delete(path, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

ZookeeperUtilsTest Code

5 Curator 代码

  • Curator是Apache一个可以操作Zookeeper的顶级项目,主要是对Zookeeper的代码封装了一下,更加方便我们开发
5.1 增加POM文件
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>4.2.0</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </exclusion>
    </exclusions>
</dependency>
5.2 CuratorUtils
package com.xk.bigdata.curator.utils;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;

import java.util.List;

public class CuratorUtils {

    public static CuratorFramework client = null;

    /**
     * 连接 Curator 客户端
     *
     * @param zkQuorum         Zookeeper连接字符串
     * @param baseSleepTimeMs  基本sleep时间
     * @param maxRetries       最大尝试次数
     * @param namespace        命名空间
     * @param sessionTimeoutMs Session 最大超时时间
     */
    public static void connect(String zkQuorum, int baseSleepTimeMs, int maxRetries, String namespace, int sessionTimeoutMs) {
        ExponentialBackoffRetry retry = new ExponentialBackoffRetry(baseSleepTimeMs, maxRetries);
        client = CuratorFrameworkFactory.builder()
                .connectString(zkQuorum)
                .sessionTimeoutMs(sessionTimeoutMs)
                .retryPolicy(retry)
                .namespace(namespace)
                .build();
        client.start();
    }

    /**
     * 关闭客户端
     */
    public static void close() {
        if (null != client) {
            client.close();
        }
    }

    /**
     * 创建节点
     *
     * @param path  路径
     * @param value 数据
     * @return
     * @throws Exception
     */
    public static String createNode(String path, String value) throws Exception {
        return client.create().creatingParentsIfNeeded()
                .withMode(CreateMode.PERSISTENT)
                .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
                .forPath(path, value.getBytes());
    }

    /**
     * 得到节点数据
     *
     * @param path 目录
     * @return
     * @throws Exception
     */
    public static String getData(String path) throws Exception {
        return new String(client.getData().forPath(path));
    }

    /**
     * 修改节点数据
     *
     * @param path  路径
     * @param value 数据
     * @return
     * @throws Exception
     */
    public static Boolean setData(String path, String value) throws Exception {
        client.setData()
                .forPath(path, value.getBytes());
        String data = getData(path);
        if (data.equals(value)) {
            return true;
        }
        return false;
    }

    /**
     * 检查节点是否存在
     *
     * @param path 路径
     * @return
     * @throws Exception
     */
    public static Boolean checkExists(String path) throws Exception {
        return client.checkExists()
                .forPath(path) == null ? false : true;
    }

    /**
     * 得到子节点
     *
     * @param path 路径
     * @return
     * @throws Exception
     */
    public static List<String> getChildren(String path) throws Exception {
        return client.getChildren()
                .forPath(path);
    }

    /**
     * 删除路径
     *
     * @param path 路径
     * @throws Exception
     */
    public static void delete(String path) throws Exception {
        client.delete()
                .deletingChildrenIfNeeded()
                .forPath(path);
    }

}

CuratorUtils Code

5.3 CuratorUtilsTest
package com.xk.bigdata.curator.utils;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

public class CuratorUtilsTest {

    final public String zkQuorum = "bigdatatest01:2181";
    final public int baseSleepTimeMs = 1000;
    final public int maxRetries = 5;
    final public String namespace = "spark";
    final public int sessionTimeoutMs = 1000;
    final public String path = "/hadoop";
    final public String value = "222";

    @Before
    public void setUp() {
        CuratorUtils.connect(zkQuorum, baseSleepTimeMs, maxRetries, namespace, sessionTimeoutMs);
    }

    @After
    public void cleanUp() {
        CuratorUtils.close();
    }

    /**
     * [zk: localhost:2181(CONNECTED) 6] ls /
     * [zookeeper, spark]
     * [zk: localhost:2181(CONNECTED) 7] ls /spark
     * [hadoop]
     * [zk: loca
     */
    @Test
    public void testCreateNode() {
        try {
            String result = CuratorUtils.createNode(path, value);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testGetData() {
        try {
            String result = CuratorUtils.getData(path);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testSetData() {
        try {
            Boolean result = CuratorUtils.setData(path, "111");
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testCheckExists() {
        try {
            Boolean result = CuratorUtils.checkExists(path);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testSetChildren() {
        for (int i = 0; i < 10; i++) {
            try {
                CuratorUtils.createNode(path + "/" + i, i + "");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void testGetChildren() {
        try {
            List<String> childrens = CuratorUtils.getChildren(path);
            for (String children : childrens) {
                System.out.println(children);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testDelete() {
        try {
            CuratorUtils.delete(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

CuratorUtilsTest Code

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值