原生 Java API 操作 ZooKeeper 详解
·
对于 Java 开发者而言,掌握Java 语言操作 ZooKeeper的核心能力,是理解分布式协作原理、落地分布式解决方案的必备基础。无论是基于原生 ZooKeeper 客户端实现基础功能,还是使用 Curator 等简化框架提升开发效率,都需要从核心 API、连接机制、数据节点操作、监听机制、异常处理等维度系统学习。
第一步:导入Curator工具包
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>com.google.collections</groupId>
<artifactId>google-collections</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
第二步:测试连接
package com.qyh;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.TreeCache;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import org.apache.curator.framework.recipes.cache.TreeCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
/**
* 创建人: @author ZZY
* 创建时间: 2026-04-07 15:55
*/
public class TestZK {
CuratorFramework curator = null;
@Before
public void init(){
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 2);
curator = CuratorFrameworkFactory.newClient("hadoop11:2181", retryPolicy);
curator.start();
}
@After
public void destroy(){
curator.close();
}
@Test
public void testCreate() throws Exception {
curator.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/bigdata01/aaa");
}
@Test
public void testLs() throws Exception {
List<String> list = curator.getChildren().forPath("/");
System.out.println(list);
}
@Test
public void testSet() throws Exception {
curator.setData().forPath("/bigdata01", "12345,上山打老虎".getBytes());
}
@Test
public void testGet() throws Exception {
byte[] bytes = curator.getData().forPath("/bigdata01");
System.out.println(new String(bytes));
}
@Test
public void testDel() throws Exception {
curator.delete().forPath("/bigdata01/aaa");
}
@Test
public void testWatch() throws Exception {
TreeCache treeCache = new TreeCache(curator, "/bigdata01");
treeCache.getListenable().addListener(new TreeCacheListener() {
@Override
public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) throws Exception {
TreeCacheEvent.Type type = treeCacheEvent.getType();
ChildData data = treeCacheEvent.getData();
if (type==TreeCacheEvent.Type.NODE_ADDED){
System.out.println("创建了一个新的节点"+data.getPath());
}
if (type==TreeCacheEvent.Type.NODE_REMOVED){
System.out.println("移除了一个节点"+data.getPath());
}
if (type==TreeCacheEvent.Type.NODE_UPDATED){
System.out.println("修改了一个节点"+data.getPath());
}
}
});
treeCache.start();
Thread.sleep(1000*1000);
}
}
更多推荐



所有评论(0)