# zookeeper-example **Repository Path**: jonathanzyf/zookeeper-example ## Basic Information - **Project Name**: zookeeper-example - **Description**: No description available - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-12-16 - **Last Updated**: 2021-01-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Zookeeper 示例工程 ## 介绍 本示例提供访问Zookeeper的两种客户端(Curator、ZkClient)示例程序,下表为两种客户端主要API功能清单。 | 功能 | Curator | ZkClient | |---|---|---| | 是否存在 | checkExists, 返回 **Stat** | exists, 返回boolean | | 创建节点 | create, 返回节点路径 | create, 返回节点路径 | | 获取数据 | getData, 仅读取节点数据 | readData,可以读取数据和 **Stat** | | 修改数据 | setData, 返回 **Stat** | writeData, 返回Void | | 获取子节点 | getChildren, 返回子节点List | getChildren, 返回子节点List | | 删除节点 | delete, 返回Void | delete/deleteRecursive, 返回boolean | > CreateMode 创建节点时,需要指定节点类型CreateMode: PERSISTENT,PERSISTENT_SEQUENTIAL,EPHEMERAL,EPHEMERAL_SEQUENTIAL > Stat | 属性 | 含义 | |---|---| | numChildren | 子节点个数 | | ephemeralOwner | 当ephemeralOwner值为0时,该节点为持久节点;当ephemeralOwner值大于0时,该节点为临时节点,ephemeralOwner值表示与该节点绑定的会话ID(session id) | | cversion | 子节点增删次数 | | dataVersion | 本节点数据更新次数 | | aclVersion | 节点ACL(授权信息)的更新次数 | | dataLength | 节点数据长度 | | ctime | 节点创建时间 | | mtime | 节点最近更新时间 | > Curator模版 ``` RetryPolicy retryPolicy = new RetryForever(1000);//new ExponentialBackoffRetry(1000, 3); CuratorFramework client = CuratorFrameworkFactory.newClient(server, sessionTimeout, connectionTimeout, retryPolicy); client.start(); try { client.create().withMode(CreateMode.PERSISTENT).forPath(path); } finally { if (client != null) { client.close(); } } ``` > ZkClient模版 ``` ZkClient client = new ZkClient(sever, sessionTimeout, connectionTimeout); try { client.create(path, data, CreateMode.PERSISTENT); } finally { if (client != null) { client.close(); } } ```