Zookeeper数据模型
Zookeeper的数据模型是基于树节点(Znode)key/value形式。
节点存储类型:
1.临时
2.临时顺序
3.持久
4.持久顺序
节点操作API:
create - 在树形结构的位置中创建节点
delete - 删除一个节点
exists - 测试节点在指定位置上是否存在
get data - 从节点上读取数据
set data - 往节点写入输入
get chilren - 检索一个节点的子节点列表
sync - 等待传输数据
本文将采用curator+ zookeeper +springboot 来整合,实现zookeeper节点新增、查看例子。
curator简介
curator是Netflix公司开源的zookeeper java客户端,原生zookeeper api对代码不太友好,curator在原生zookeeper api封装,方便使用。
简单版本Java示例
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring boot 单元测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- zookeeper start -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
</dependency>
<!-- zookeeper end -->
Java版示例
@Test
public void test() throws Exception {
// 客户端连接
CuratorFramework client = CuratorFrameworkFactory.builder()
//连接地址 集群用,隔开
.connectString("192.168.126.156:2181")
.connectionTimeoutMs(50000)
//会话超时时间
.sessionTimeoutMs(50000)
//设置重试机制
.retryPolicy(new ExponentialBackoffRetry(1000, 3))
//设置命名空间 在操作节点的时候,会以这个为父节点
.namespace("terry")
.build();
// 开启连接
client.start();
// 1、创建节点:默认存储当前客户端的ip地址
String path = client.create().forPath("/user");
// 2.查询节点内容
byte[] bytes = client.getData().forPath("/user");
System.out.println("查询节点内容:" + new String(bytes));
}
客户端打印输出
查询节点内容:192.168.126.1
使用Zookeeper图像客户端可以查看到数据
与SpringBoot集成
pom.xml 使用的依赖跟java示例一样。
application.yml
zookeeper:
curator:
ip: 192.168.126.156:2181
sessionTimeOut: 50000
sleepMsBetweenRetry: 1000
maxRetries: 3
namespace: terry
connectionTimeoutMs: 50000
创建ZookeeperConfig
配置类
import lombok.Data;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "zookeeper.curator")
@Data
public class ZookeeperConfig {
/**
* 集群地址
*/
private String ip;
/**
* 连接超时时间
*/
private Integer connectionTimeoutMs;
/**
* 会话超时时间
*/
private Integer sessionTimeOut;
/**
* 重试机制时间参数
*/
private Integer sleepMsBetweenRetry;
/**
* 重试机制重试次数
*/
private Integer maxRetries;
/**
* 命名空间(父节点名称)
*/
private String namespace;
/**
* curator 客户端
* @return
* @throws Exception
*/
@Bean("curatorClient")
public CuratorFramework curatorClient() throws Exception {
// 开启客户端,并连接
CuratorFramework client = CuratorFrameworkFactory.builder()
//连接地址 集群用,隔开
.connectString(ip)
.connectionTimeoutMs(connectionTimeoutMs)
//会话超时时间
.sessionTimeoutMs(sessionTimeOut)
//设置重试机制
.retryPolicy(new ExponentialBackoffRetry(sleepMsBetweenRetry, maxRetries))
//设置命名空间 在操作节点的时候,会以这个为父节点
.namespace(namespace)
.build();
client.start();
return client;
}
}
创建启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* zookeeper 客户端
* @author terry
* @version 1.0
* @date 2022/4/18 11:38
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
创建测试客户端
import com.terry.App;
import lombok.extern.java.Log;
import org.apache.curator.framework.CuratorFramework;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* zookeeper 客户端测试
* @author terry
* @version 1.0
* @date 2022/4/25 23:01
*/
@SpringBootTest(classes = App.class)
@Log
public class Client {
@Autowired
private CuratorFramework client;
@Test
public void test() throws Exception {
// 1、创建节点:默认存储当前客户端的ip地址
String path = client.create().forPath("/user");
// 2.查询节点内容
byte[] bytes = client.getData().forPath("/user");
System.out.println("查询节点内容:" + new String(bytes));
}
}
打印输出
查询节点内容:192.168.126.1