1.3 在项目的 src/main/resources 目录下,新建一个文件,命名为log4j.properties,在文件中填入junit junit RELEASE org.apache.logging.log4j log4j-core 2.8.2 org.apache.zookeeper zookeeper 3.5.9
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%n2. 客户端 API 操作
package com.codecat.zk;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
public class zkClient {
private static String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
private static int sessionTimeout = 2000;
private ZooKeeper zkClient = null;
// 创建Zookeeper客户端
@Before
public void init() throws IOException {
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
// 收到事件通知后的回调函数(用户的业务逻辑)
public void process(WatchedEvent watchedEvent) {
System.out.println(watchedEvent.getType() + "----" + watchedEvent.getPath());
// 再次启动监听
List children = null;
try {
children = zkClient.getChildren("/", true);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
for (String child : children) {
System.out.println(child);
}
}
});
}
// 创建子节点
@Test
public void create() throws KeeperException, InterruptedException {
String nodeCreated = zkClient.create("/codecat", "love game".getBytes(StandardCharsets.UTF_8), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
// 获取子节点并监听节点变化
@Test
public void getChildren() throws KeeperException, InterruptedException {
List children = zkClient.getChildren("/", true);
for (String child : children) {
System.out.println(child);
}
// 延时阻塞
Thread.sleep(Long.MAX_VALUE);
}
// 判断Znode是否存在
@Test
public void exists() throws KeeperException, InterruptedException {
Stat stat = zkClient.exists("/codecat", true);
System.out.println(stat == null ? "not exist" : "exist");
}
}
2.1 创建子节点
运行create()
运行getChildren()
在 hadoop102 的客户端上创建再创建一个节点/test
[zk: localhost:2181(CONNECTED) 4] create /test "test" Created /test2.3 判断 Znode 是否存在
运行exist()



