3.3 客户端 API 操作
前提:保证 hadoop102、hadoop103、hadoop104 服务器上 Zookeeper 集群服务端启动。
3.3.1 IDEA 环境搭建
1)创建一个工程:zookeeper
2)添加pom文件
xml
<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>
<dependency>
<groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId>
<version>3.5.7</version>
</dependency>
</dependencies>
3)拷贝log4j.properties文件到项目根目录
需要在项目的 src/main/resources 目录下,新建一个文件,命名为“log4j.properties”,在文件中填入。
bash
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)创建包名com.atguigu.zk
5)创建类名称zkClient
3.3.2 创建 ZooKeeper 客户端
java
public class zkClient {
// 注意:逗号左右不能有空格
private String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
private int sessionTimeout = 2000;
private ZooKeeper zkClient;
@Before
public void init() throws IOException {
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {}
});
}
}
3.3.3 创建子节点
java
public class zkClient {
@Test
public void create() throws KeeperException, InterruptedException {
// 参数 1:要创建的节点的路径;
// 参数 2:节点数据 ;
// 参数 3:节点权限 ;
// 参数 4:节点的类型
String nodeCreated = zkClient.create("/atguigu", "ss.avi".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
}
测试:在 hadoop102 的 zk 客户端上查看创建节点情况
bash
[zk: localhost:2181(CONNECTED) 16] get -s /atguigu shuaige
3.3.4 获取子节点并监听节点变化
java
public class zkClient {
@Test
public void getChildren() throws KeeperException, InterruptedException {
List<String> children = zkClient.getChildren("/", true);
for (String child : children) {
System.out.println(child);
}
// 延时
Thread.sleep(Long.MAX_VALUE);
}
}
(1)在 IDEA 控制台上看到如下节点:
zookeeper
sanguo
atguigu
(2)在 hadoop102 的客户端上创建再创建一个节点/atguigu1,观察 IDEA 控制台
bash
[zk: localhost:2181(CONNECTED) 3] create /atguigu1 "atguigu1"
(3)在 hadoop102 的客户端上删除节点/atguigu1,观察 IDEA 控制台
bash
[zk: localhost:2181(CONNECTED) 4] delete /atguigu1
3.3.5 判断 Znode 是否存在
java
public class zkClient {
@Test
public void exist() throws KeeperException, InterruptedException {
Stat stat = zkClient.exists("/atguigu", false);
System.out.println(stat==null ? "not exist " : "exist");
}
}
完整代码
java
package com.atguigu.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.util.List;
public class zkClient {
// 注意:逗号左右不能有空格
private String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
private int sessionTimeout = 2000;
private ZooKeeper zkClient;
@Before
public void init() throws IOException {
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
System.out.println("-------------------------------");
List<String> children = null;
try {
children = zkClient.getChildren("/", true);
for (String child : children) {
System.out.println(child);
}
System.out.println("-------------------------------");
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
@Test
public void create() throws KeeperException, InterruptedException {
String nodeCreated = zkClient.create("/atguigu", "ss.avi".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
@Test
public void getChildren() throws KeeperException, InterruptedException {
List<String> children = zkClient.getChildren("/", true);
for (String child : children) {
System.out.println(child);
}
// 延时
Thread.sleep(Long.MAX_VALUE);
}
@Test
public void exist() throws KeeperException, InterruptedException {
Stat stat = zkClient.exists("/atguigu", false);
System.out.println(stat==null? "not exist " : "exist");
}
}
3.4 客户端向服务端写数据流程
写流程之写入请求直接发送给Leader节点

写流程之写入请求发送给follower节点

1100

被折叠的 条评论
为什么被折叠?



