1. Api
坐标
<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>
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
test
package com.atguigu.zkclient;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
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 ZooKeeper zooKeeper = null;
@Before
public void before() throws IOException {
String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
int sessionTimeout = 2000;
//创建zk对象
zooKeeper = new ZooKeeper(connectString, //连接地址
sessionTimeout, //超时时间
new Watcher() {
//Zookeeper监听的回调函数
@Override
public void process(WatchedEvent watchedEvent) {
System.out.println("回调函数");
}
}
);
}
@After
public void after() throws InterruptedException {
zooKeeper.close();
}
//创建新节点
@Test
public void create() throws IOException, InterruptedException, KeeperException {
zooKeeper.create(
"/testApi", //节点名
"haha".getBytes(), //值
ZooDefs.Ids.OPEN_ACL_UNSAFE, //访问控制列表 相对应权限
CreateMode.PERSISTENT //永久节点
);
}
//查询子节点
@Test
public void ls() throws InterruptedException, KeeperException {
List<String> children = zooKeeper.getChildren(
"/",
new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
System.out.println("自定义回调函数");
}
}
);
for (String child : children) {
System.out.println(child);
}
Thread.sleep(Long.MAX_VALUE);
}
//查询节点的值
@Test
public void get() throws InterruptedException, KeeperException, IOException {
Stat stat = new Stat(); //节点状态(Stat结构体)
byte[] data = zooKeeper.getData("/testApi", false, stat);
System.out.write(data);
System.out.println();
System.out.println(stat.getMzxid());
}
//查询一个节点的状态
@Test
public void stat() throws InterruptedException, KeeperException {
Stat stat = zooKeeper.exists("/testApi", false);
if (stat == null) {
System.out.println("节点不存在");
} else {
System.out.println(stat);
}
}
//修改节点内容
@Test
public void set() throws InterruptedException, KeeperException {
String node = "/testApi";
int version = 0;
Stat stat = zooKeeper.exists(node, false);
if (stat == null) {
System.out.println("节点不存在");
} else {
version = stat.getVersion();
}
//乐观锁 修改前先比较版本号 不一致则报错
zooKeeper.setData(node,
"123".getBytes(StandardCharsets.UTF_8),
version
);
}
//删除节点
@Test
public void delete() throws InterruptedException, KeeperException {
zooKeeper.delete("/testApi",1);
}
}