Zookeeper ZkClient
1. 前言
無論是選擇 Zookeeper 的單機模式還是選擇 Zookeeper 的集群模式,實際開發(fā)中我們都會使用 Java 客戶端向Zookeeper 服務端發(fā)送請求。本節(jié)我們就來學習如何使用 Zookeeper 的 Java 客戶端之一 ZkClient。
2. ZkClient 客戶端
ZkClient 是一個開源的客戶端,在 Zookeeper 原生 API 接口的基礎上進行了包裝,更便于開發(fā)人員使用。內(nèi)部實現(xiàn)了Session超時重連,Watcher反復注冊等功能。想要使用 ZkClient,我們需要搭建 Java 開發(fā)環(huán)境,這里我們使用 IntelliJ IDEA 為開發(fā)工具,JDK 我們使用了長期維護的版本 JDK-11.0.8 。接下來我們開始搭建 ZkClient 運行的環(huán)境。
2.1 ZkClient 的依賴
首先我們新建 Spring Boot 項目,在 pom.xml 文件中加入 zkclient 的依賴:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.cdd</groupId>
<artifactId>zkclient-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>zkclient-demo</name>
<description>zkclient-demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.11</version>
<!-- 排除沖突 -->
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
加入了 ZkClient的依賴,我們就可以編寫程序使用 zkclient 的 API 來向 Zookeeper 服務端發(fā)送請求了。
2.2 編寫 ZkClientServer 類
我們在 Spring Boot 主函數(shù)的同級新建目錄 service ,在 service 目錄中新建類 ZkClientServer :
package cn.cdd.zkclientdemo.service;
import org.I0Itec.zkclient.ZkClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
// 聲明 ZkClientServer 類為 Bean,交給 Spring 管理
@Component
public class ZkClientServer {
// Zookeeper 集群的地址
@Value(value = "${zookeeper.address}")
private String address;
// 獲取 ZkClient
public ZkClient getZkClient() {
return new ZkClient(address);
}
}
在 application.properties 配置文件中配置 Zookeeper 集群的地址:
zookeeper.address=192.168.0.77:2181,192.168.0.88:2181,192.168.0.99:2181
配置完成后,我們就可以去測試類中進行測試了。
2.3 測試
我們在項目中與 main 目錄同級的 test 目錄中找到測試類 ZkClientDemoApplicationTests ,在其中添加測試方法。
2.3.1 查詢測試
package cn.cdd.zkclientdemo;
import cn.cdd.zkclientdemo.service.ZkClientServer;
import org.I0Itec.zkclient.ZkClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class ZkClientDemoApplicationTests {
// 依賴注入
@Autowired
private ZkClientServer zkClientServer;
@Test
void contextLoads() {
// 獲取 ZkClient 對象
ZkClient zkClient = zkClientServer.getZkClient();
// 獲取子節(jié)點集合
List<String> children = zkClient.getChildren("/");
System.out.println(children);
// 釋放資源
zkClient.close();
}
}
執(zhí)行測試方法,控制臺輸出:
[zookeeper, imooc]
我們可以看到控制臺輸出了在上一節(jié)中我們增加的節(jié)點 imooc,說明我們的查詢成功執(zhí)行了。接下來我們測試其它的 API。
2.3.2 創(chuàng)建持久節(jié)點
創(chuàng)建持久節(jié)點測試:
@Test
void contextLoads() {
ZkClient zkClient = zkClientServer.getZkClient();
// 在 imooc 節(jié)點下創(chuàng)建持久節(jié)點 wiki
zkClient.createPersistent("/imooc/wiki");
// 獲取 imooc 節(jié)點的子節(jié)點集合
List<String> children = zkClient.getChildren("/imooc");
System.out.println(children);
// 釋放資源
zkClient.close();
}
執(zhí)行測試方法,控制臺輸出:
[wiki]
2.3.3 刪除節(jié)點
刪除節(jié)點測試:
@Test
void contextLoads() {
ZkClient zkClient = zkClientServer.getZkClient();
// 刪除 imooc 的 子節(jié)點 wiki
boolean delete = zkClient.delete("/imooc/wiki");
System.out.println(delete);
// 釋放資源
zkClient.close();
}
執(zhí)行測試方法,控制臺輸出:
true
true 表示刪除 wiki 節(jié)點成功。
2.3.4 讀寫數(shù)據(jù)
節(jié)點數(shù)據(jù)讀寫測試:
@Test
void contextLoads() {
ZkClient zkClient = zkClientServer.getZkClient();
// 給 imooc 節(jié)點寫入數(shù)據(jù) wiki
zkClient.writeData("/imooc","wiki");
// 讀取 imooc 節(jié)點的數(shù)據(jù)
Object data = zkClient.readData("/imooc");
System.out.println(data);
// 釋放資源
zkClient.close();
}
執(zhí)行測試方法,控制臺輸出:
wiki
Tips: 當我們使用 API 操作節(jié)點時,節(jié)點參數(shù)必須是全路徑。
測試完成后,我們來對 ZkClient 常用的 API 做一下介紹。
3. ZkClient API
- ZkClient 初始化 API
// serverstring:服務器地址的字符串
// 如:192.168.0.77:2181,192.168.0.88:2181,192.168.0.99:2181
public ZkClient(String serverstring);
// zkServers:服務器地址的字符串,connectionTimeout:連接超時時間,單位:ms
public ZkClient(String zkServers, int connectionTimeout);
// sessionTimeout: session 超時時間
public ZkClient(String zkServers, int sessionTimeout, int connectionTimeout);
// ZkSerializer:序列化方式,實現(xiàn)類有:SerializableSerializer、BytesPushThroughSerializer
public ZkClient(String zkServers, int sessionTimeout, int connectionTimeout, ZkSerializer zkSerializer);
- 節(jié)點創(chuàng)建 API
// path:節(jié)點全路徑
public void createPersistent(String path);
// createParents:是否創(chuàng)建父節(jié)點
public void createPersistent(String path, boolean createParents);
// acl:權(quán)限列表
public void createPersistent(String path, boolean createParents, List<ACL> acl);
// data:節(jié)點數(shù)據(jù)
public void createPersistent(String path, Object data);
public void createPersistent(String path, Object data, List<ACL> acl);
// 創(chuàng)建持久順序節(jié)點
public String createPersistentSequential(String path, Object data);
public String createPersistentSequential(String path, Object data, List<ACL> acl);
// 創(chuàng)建臨時節(jié)點
public void createEphemeral(String path);
public void createEphemeral(String path, List<ACL> acl);
// mode:節(jié)點類型
public String create(String path, Object data, CreateMode mode);
public String create(final String path, Object data, final List<ACL> acl, final CreateMode mode);
public void createEphemeral(String path, Object data);
public void createEphemeral(String path, Object data, List<ACL> acl);
// 創(chuàng)建臨時順序節(jié)點
public String createEphemeralSequential(String path, Object data);
public String createEphemeralSequential(String path, Object data, List<ACL> acl);
- 查詢節(jié)點 API
// 獲取子節(jié)點列表
public List<String> getChildren(String path);
// watch:是否開啟觀察
protected List<String> getChildren(final String path, final boolean watch);
// 獲取子節(jié)點數(shù)量
public int countChildren(String path);
// 節(jié)點是否存在
protected boolean exists(final String path, final boolean watch);
public boolean exists(String path);
- 刪除節(jié)點 API
public boolean delete(String path);
// version:節(jié)點版本
public boolean delete(final String path, final int version);
- 讀寫節(jié)點數(shù)據(jù) API
public <T> T readData(String path);
public <T> T readData(String path, boolean returnNullIfPathNotExists);
public <T> T readData(String path, Stat stat);
protected <T> T readData(final String path, final Stat stat, final boolean watch);
public void writeData(String path, Object object);
public void writeData(String path, Object datat, int expectedVersion);
- 觀察節(jié)點 API
// 觀察節(jié)點的數(shù)據(jù)變化
public void watchForData(final String path);
// 觀察子節(jié)點的變化
public List<String> watchForChilds(final String path)
4. 總結(jié)
本節(jié)我們學習了如何使用 ZkClient 的 API 對 Zookeeper 服務節(jié)點的操作,還介紹了一些常用的 API。以下是本節(jié)內(nèi)容的總結(jié):
- 使用 Spring Boot 集成 ZkClient 。
- ZkClient 的 API。