ZooKeeper 介绍
创始人
2025-06-01 21:05:39
0

一、数据模型

ZooKeeper 是一个树形目录服务,每个节点(ZNode)上可以保存自己的数据和节点信息。

节点可以拥有子节点,同时也允许少量(1MB)数据存储在该节点之下。

节点可以分为四大类:

  • PERSISTENT 持久化节点 
  • EPHEMERAL 临时节点 :-e
  • PERSISTENT_SEQUENTIAL 持久化顺序节点 :-s
  • EPHEMERAL_SEQUENTIAL 临时顺序节点  :-es

 二、服务端命令

启动 ZooKeeper 服务: ./zkServer.sh start

查看 ZooKeeper 服务状态: ./zkServer.sh status

停止 ZooKeeper 服务: ./zkServer.sh stop 

重启 ZooKeeper 服务: ./zkServer.sh restart 

三、客户端命令

./zkCli.sh –server ip:port 连接服务器

quit 断开连接

help 查看帮助命令

ls /目录 查看目录下的节点

create /节点path value 创建节点

get /节点path 获取节点值

set /节点path value 设置节点值

delete /节点path 删除单个节点

deleteall /节点path 删除带有子节点的节点

create -e /节点path value 创建临时节点

create -s /节点path value 创建顺序节点

 ls –s /节点path  查询节点详细信息

详细信息:

四、使用curator JavaAPI操作zookeeper

1、使用curator连接zookeeper

public void testConnect() {//重试策略RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10);//2.第二种方式//CuratorFrameworkFactory.builder();client = CuratorFrameworkFactory.builder().connectString("192.168.200.130:2181").sessionTimeoutMs(60 * 1000).connectionTimeoutMs(15 * 1000).retryPolicy(retryPolicy).namespace("itheima").build();//开启连接client.start();
}

2、创建节点


//创建基本节点,没有指定数据,则默认将当前客户端的ip作为数据存储
public void testCreate2() throws Exception {String path = client.create().forPath("/app1");System.out.println(path);
}//创建带有数据的基本节点
public void testCreate() throws Exception {String path = client.create().forPath("/app2", "hehe".getBytes());System.out.println(path);
}//创建节点并指定节点类型
public void testCreate3() throws Exception {//3. 设置节点的类型//默认类型:持久化String path = client.create().withMode(CreateMode.EPHEMERAL).forPath("/app3");System.out.println(path);
}
//创建多级节点节点
public void testCreate4() throws Exception {//4. 创建多级节点  /app1/p1//creatingParentsIfNeeded():如果父节点不存在,则创建父节点String path = client.create().creatingParentsIfNeeded().forPath("/app4/p1");System.out.println(path);
}

3、查询节点

public void testGet1() throws Exception {//1. 查询数据:getbyte[] data = client.getData().forPath("/app1");System.out.println(new String(data));
}public void testGet2() throws Exception {// 2. 查询子节点: lsList path = client.getChildren().forPath("/");System.out.println(path);
}public void testGet3() throws Exception {Stat status = new Stat();System.out.println(status);//3. 查询节点状态信息:ls -sclient.getData().storingStatIn(status).forPath("/app1");System.out.println(status);
}

4、修改节点

//基本修改
public void testSet() throws Exception {client.setData().forPath("/app1", "itcast".getBytes());
}//根据版本号修改
public void testSetForVersion() throws Exception {Stat status = new Stat();//查询节点状态信息:ls -sclient.getData().storingStatIn(status).forPath("/app1");int version = status.getVersion();//查询出来的System.out.println(version);client.setData().withVersion(version).forPath("/app1", "hehe".getBytes());
}

5、删除节点

public void testDelete() throws Exception {// 1. 删除单个节点client.delete().forPath("/app1");
}public void testDelete2() throws Exception {//2. 删除带有子节点的节点client.delete().deletingChildrenIfNeeded().forPath("/app4");
}public void testDelete3() throws Exception {//3. 必须成功的删除(例如发生网络抖动,其实就是不停的重试)client.delete().guaranteed().forPath("/app2");
}public void testDelete4() throws Exception {//4. 回调client.delete().guaranteed().inBackground(new BackgroundCallback(){@Overridepublic void processResult(CuratorFramework client, CuratorEvent event) throws Exception {System.out.println("我被删除了~");System.out.println(event);}}).forPath("/app1");
}

五、watch监听详解

ZooKeeper提供了三种Watcher:

NodeCache : 只是监听某一个特定的节点

PathChildrenCache : 监控一个ZNode的子节点. 

TreeCache : 可以监控整个树上的所有节点,类似于PathChildrenCache和NodeCache的组合

NodeCache监听 

/**
* 演示 NodeCache:给指定一个节点注册监听器
*/
public void testNodeCache() throws Exception {//1. 创建NodeCache对象final NodeCache nodeCache = new NodeCache(client,"/app1");//2. 注册监听nodeCache.getListenable().addListener(new NodeCacheListener() {@Overridepublic void nodeChanged() throws Exception {System.out.println("节点变化了~");//获取修改节点后的数据byte[] data = nodeCache.getCurrentData().getData();System.out.println(new String(data));}});//3. 开启监听.如果设置为true,则开启监听时,加载缓存数据nodeCache.start(true);while (true){}
}

PathChildrenCache监听

public void testPathChildrenCache() throws Exception {//1.创建监听对象PathChildrenCache pathChildrenCache = new PathChildrenCache(client,"/app2",true);//2. 绑定监听器pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {    			    @Overridepublic void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {System.out.println("子节点变化了~");System.out.println(event);//监听子节点的数据变更,并且拿到变更后的数据//1.获取类型PathChildrenCacheEvent.Type type = event.getType();//2.判断类型是否是updateif(type.equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)){System.out.println("数据变了!!!");byte[] data = event.getData().getData();System.out.println(new String(data));}}});//3. 开启pathChildrenCache.start();while (true){}
}

TreeCache监听

/**
* 演示 TreeCache:监听某个节点自己和所有子节点们
*/
public void testTreeCache() throws Exception {//1. 创建监听器TreeCache treeCache = new TreeCache(client,"/app2");//2. 注册监听treeCache.getListenable().addListener(new TreeCacheListener() {@Overridepublic void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {System.out.println("节点变化了");System.out.println(event);}});//3. 开启treeCache.start();while (true){}
}

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
【Ctfer训练计划】——(三... 作者名:Demo不是emo  主页面链接:主页传送门 创作初心ÿ...