概述
Zookeeper是Apache提供的一套用于进行分布式架构的协调和管理的框架
- create /news ‘news server’ 在根节点下创建一个子节点news
- ls / 查看/所有的子节点
- delete /music 删除根节点下的music节点。要求被删除的节点必须没有子节点
- rmr /news 递归删除news节点及其子节点
- get /news 查看news的数据以及节点信息
- set /news ‘news server’ 更改news节点的信息
- create -e /video ‘’ 创建一个临时节点video
- create -s /tmp ‘’ 创建一个顺序节点tmp
节点信息
- cZxid 这个节点的创建事务id
- ctime 这个节点的创建时间
- mZxid 这个节点的修改数据的事务id
- mtime 这个节点的修改数据的时间
- pZxid 记录子节点个数变化的事务id
- cversion 子节点个数变化次数
- dataVersion 数据版本。记录当前节点被修改的次数
- aclVersion 记录节点权限的变化次数
- ephemeralOwner 标记当前节点是否是一个临时节点
- 只要这个节点是持久节点,那么此项为0
- 如果这个节点是临时节点,那么此项为sessionid
- dataLength 数据的字节个数
- numChildren 子节点个数
节点类型
持久节点 临时节点
顺序节点 Persistant_Sequential Ephemeral_Sequential
非顺序节点 Persistant Ephemeral
相关代码
package cn.tedu.ZK;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;
public class ZookeeperDemo {
ZooKeeper zk;
// 连接Zookeeper
@Before
public void connect() throws Exception {
CountDownLatch cdl = new CountDownLatch(1);
// connectString - 连接地址
// sessionTimeout - 会话超时时间,默认单位是毫秒
// watcher - 监控者
// Zookeeper的连接是利用NIO的非阻塞方式连接
// 就意味着可能没有连上就继续往下的操作
// 因此需要监控这个连接是否建立
zk = new ZooKeeper("192.168.195.132:2181", // 连接地址
5000, // 现阶段,这个值必须在4000~40000之间
new Watcher() {
// 需要利用这个方法来监控连接是否建立
@Override
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected)
System.out.println("连接成功~~~");
cdl.countDown();
}
});
cdl.await();
System.out.println("Test线程执行完成~~~");
}
// 创建节点
@Test
public void createNode() throws KeeperException, InterruptedException {
// path - 路径
// data - 数据
// acl - acl策略
// createMode - 创建模式 - 节点类型
// 返回值表示节点的路径
String path = zk.create("/news", "news server".getBytes(), Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT_SEQUENTIAL);
System.out.println(path);
}
// 修改节点的数据
@Test
public void setData() throws KeeperException, InterruptedException {
// path, data
// version - 数据版本
// 在修改节点数据的时候,会自动校验version是否一致
// 如果一致,才允许修改
// 如果需要强制修改,那么数据版本的值给定为-1
Stat s = zk.setData("/log", "log~~~".getBytes(), 0);
System.out.println(s);
}
// 获取节点数据
@Test
public void getData() throws KeeperException, InterruptedException {
// path, watch,
// stat - 节点信息
// Stat stat = new Stat();
byte[] data = zk.getData("/log", null, null);
System.out.println(new String(data));
}
// 删除节点
@Test
public void deleteNode() throws InterruptedException, KeeperException{
zk.delete("/news0000000011", -1);
}
// 获取子节点
@Test
public void getChildren() throws KeeperException, InterruptedException{
// 返回的是子节点的路径
List<String> paths = zk.getChildren("/", null);
for (String path : paths) {
System.out.println(path);
}
}
// 判断节点是否存在
@Test
public void exists() throws KeeperException, InterruptedException{
// 如果这个节点存在,则返回节点信息
// 如果节点不存在,则返回null
Stat s = zk.exists("/txt", null);
System.out.println(s == null);
}
}
package cn.tedu.ZK;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.junit.Before;
import org.junit.Test;
public class ZookeeperDemo2 {
ZooKeeper zk;
@Before
public void connect() throws IOException, InterruptedException {
CountDownLatch cdl = new CountDownLatch(1);
zk = new ZooKeeper("192.168.195.132:2181", 5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected)
System.out.println("连接成功~~~");
cdl.countDown();
}
});
cdl.await();
}
// 监控节点的数据是否被改动
@Test
public void dataChanged() throws KeeperException, InterruptedException {
CountDownLatch cdl = new CountDownLatch(1);
zk.getData("/log", new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getType() == EventType.NodeDataChanged)
System.out.println("节点数据被改变了~~~");
cdl.countDown();
}
}, null);
cdl.await();
}
// 监控子节点个数是否发生改变
@Test
public void childrenChanged() throws KeeperException, InterruptedException {
CountDownLatch cdl = new CountDownLatch(1);
zk.getChildren("/log", new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getType() == EventType.NodeChildrenChanged)
System.out.println("子节点个数发生了变化~~~");
cdl.countDown();
}
});
cdl.await();
}
// 监控节点的增删变化
@Test
public void nodeChanged() throws KeeperException, InterruptedException {
CountDownLatch cdl = new CountDownLatch(1);
zk.exists("/music", new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getType() == EventType.NodeCreated)
System.out.println("新建这个节点~~~");
else if (event.getType() == EventType.NodeDeleted)
System.out.println("这个节点被删除~~~");
cdl.countDown();
}
});
cdl.await();
}
}