1.API jar包
在Zookeeper根目录,zookeeper-3.4.10.jar
将lib里的jar包也考过来,因为需要依赖;
2.Eclipse
1.创建一个Maven工程
2.添加pom文件
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.8.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.5.7</version> </dependency> </dependencies> |
3.拷贝log4j.properties文件到项目根目录
需要在项目的src/main/resources目录下,新建一个文件,命名为“log4j.properties”,在文件中填入。
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
4.创建Zookeeper客户端
private static String connectString =
"hadoop102:2181,hadoop103:2181,hadoop104:2181";
private static int sessionTimeout = 2000;
private ZooKeeper zkClient = null;
@Before
public void init() throws Exception {
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() { //匿名内部类
@Override
public void process(WatchedEvent event) {
// 收到事件通知后的回调函数(用户的业务逻辑)
System.out.println(event.getType() + "--" + event.getPath());
// 再次启动监听
try {
zkClient.getChildren("/", true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
5.查看子节点
public void ls() throws Exception{
Stat stat = new Stat();
List<String> children = zkClient.getChildren("/", null, stat);
System.out.println(children);
}
6.创建子节点
// 创建子节点
@Test
public void create() throws Exception {
// 参数1:要创建的节点的路径; 参数2:节点数据 ; 参数3:节点权限 ;参数4:节点的类型
String nodeCreated = zkClient.create("/atguigu", "jinlian".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
7.获取子节点并监听状态
// 获取子节点
@Test
public void getChildren() throws Exception {
List<String> children = zkClient.getChildren("/", true);
for (String child : children) {
System.out.println(child);
}
// 延时阻塞
Thread.sleep(Long.MAX_VALUE);
}
8.判断ZNode是否存在:参数是路径,
@Test
public void exist() throws Exception {
Stat stat = zkClient.exists("/eclipse", false);
System.out.println(stat == null ? "not exist" : "exist");
}
9.get读取数据: 参数path
public void get() throws Exception{
byte[] data = zkClient.getData("/eclipse", null, null);
System.out.println(new String(data));
}
10.修改数据 : set path data
public void set() throws Exception {
zooKeeper.setData("/eclipse", "hi".getBytes(), -1);
}
-1为忽略版本号;
11.删除:参数路径,版本号
public void delete() throws Exception {
zooKeeper.delete("/eclipse", -1);
}
-1为忽略版本号;
12.递归删除
public void rmr() throws Exception {
String path="/data";
//先获取当前路径中所有的子node
List<String> children = zooKeeper.getChildren(path, false);
//删除所有的子节点
for (String child : children) {
//拼接父路径+"/"+子节点; -1 不检查版本号
zooKeeper.delete(path+"/"+child, -1);
}
zooKeeper.delete(path, -1);
}
3.设置观察者
// zkCli.sh -server xxx:2181
@Before
public void init() throws Exception {
// 创建一个zk的客户端对象
zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
//回调方法,一旦watcher观察的path触发了指定的事件,服务端会通知客户端,客户端收到通知后
// 会自动调用process()
@Override
public void process(WatchedEvent event) {
// TODO Auto-generated method stub
}
});
System.out.println(zooKeeper);
}
Stat结构体
1)czxid-创建节点的事务zxid
每次修改ZooKeeper状态都会收到一个zxid形式的时间戳,也就是ZooKeeper事务ID。
事务ID是ZooKeeper中所有修改总的次序。每个修改都有唯一的zxid,如果zxid1小于zxid2,那么zxid1在zxid2之前发生。
2)ctime - znode被创建的毫秒数(从1970年开始)
3)mzxid - znode最后更新的事务zxid
4)mtime - znode最后修改的毫秒数(从1970年开始)
5)pZxid-znode最后更新的子节点zxid
6)cversion - znode子节点变化号,znode子节点修改次数
7)dataversion - znode数据变化号
8)aclVersion - znode访问控制列表的变化号
9)ephemeralOwner- 如果是临时节点,这个是znode拥有者的session id。如果不是临时节点则是0。
10)dataLength- znode的数据长度
11)numChildren - znode子节点数量