Curator 是 Apache ZooKeeper 的Java客户端库。
Zookeeper现有常见的Java API如:原生JavaAPI、Curator、ZkClient等。
pom.xml
XML
<?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>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.9</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.2.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</project>
application.properties
XML
server.port= 8085
zookeeper.host= localhost:2181
一、建立连接
四个关键参数:
1、connectString 连接字符串 zkServer 地址和端口:"192.168.253.128:2181"
2、sessionTimeoutMs 会话超时时间,单位ms
3、connectionTimeoutMs 连接超时时间,单位ms
4、retryPolicy 重试策略
ZookeeperConfig
java
package com.example.demo.zk.config;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author yanqiuxiang
* @version 1.0
*/
@Configuration
public class ZookeeperConfig {
@Value("${zookeeper.host}")
private String host;
@Bean
public CuratorFramework curatorFramework() {
CuratorFramework curatorFramework = CuratorFrameworkFactory.builder()
.connectString(host)
.sessionTimeoutMs(5000)
.retryPolicy(new ExponentialBackoffRetry(500, 5))
.build();
curatorFramework.start();
return curatorFramework;
}
}
ZookeeperController
二、创建节点:create 持久 临时 顺序 数据
-
基本创建 :create().forPath("")
-
创建节点 带有数据:create().forPath("",data)
-
设置节点的类型:create().withMode().forPath("",data)
-
创建多级节点 /app1/p1 :create().creatingParentsIfNeeded().forPath("",data)
java
package com.example.demo.zk.controller;
import com.example.demo.zk.JsonResult;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.nio.charset.StandardCharsets;
import java.util.List;
/**
* @author yanqiuxiang
* @version 1.0
*/
@RestController
@RequestMapping(path = "/zookeeper", produces = "application/json;charset=utf-8")
public class ZookeeperController {
private final CuratorFramework curatorFramework;
@Autowired
public ZookeeperController(CuratorFramework curatorFramework) {
this.curatorFramework = curatorFramework;
}
/**
* 判断znode是否存在
* @param node 节点名称
*/
@RequestMapping(value = "/exist", method = RequestMethod.GET)
public JsonResult<Stat> exist(String node) throws Exception {
Stat stat = curatorFramework.checkExists().forPath(node);
return new JsonResult(200, stat);
}
/**
* 创建一个znode
* @param node 节点名称
*/
@RequestMapping(value = "/create", method = RequestMethod.GET)
public JsonResult<Void> create(String node) throws Exception {
curatorFramework.create()
.creatingParentContainersIfNeeded()
/*
创建模式:常用的有
PERSISTENT:持久化节点,客户端与zookeeper断开连接后,该节点依旧存在,只要不手动删除,该节点就会永远存在。
PERSISTENT_SEQUENTIAL:持久化顺序编号目录节点,客户端与zookeeper断开连接后,该节点依旧存在,只是zookeeper给该节点名称进行顺序编号。
EPHEMERAL:临时目录节点,客户端与zookeeper断开连接后,该节点被删除。
EPHEMERAL_SEQUENTIAL:临时顺序编号目录节点,客户端与zookeeper断开连接后,该节点被删除,只是zookeeper给该节点名称进行顺序编号。
*/
.withMode(CreateMode.EPHEMERAL)
.forPath(node);
return new JsonResult("创建成功");
}
/**
* 设置znode节点的数据
* @param node 节点名称
* @param data 节点的数据
*/
@RequestMapping(value = "/setData", method = RequestMethod.GET)
public JsonResult<Void> setData(String node, String data) throws Exception {
curatorFramework.setData().forPath(node, data.getBytes(StandardCharsets.UTF_8));
return new JsonResult("设置成功");
}
/**
* 删除节点
* @param node 节点名称
*/
@RequestMapping(value = "/delete", method = RequestMethod.GET)
public JsonResult<Void> delete(String node) throws Exception {
curatorFramework.delete().forPath(node);
return new JsonResult("删除成功");
}
/**
* 删除节点及其子节点的数据
* @param node 节点名称
*/
@RequestMapping(value = "/deleteDeeply", method = RequestMethod.GET)
public JsonResult<Void> deleteDeeply(String node) throws Exception {
curatorFramework.delete().deletingChildrenIfNeeded().forPath(node);
return new JsonResult("删除成功");
}
/**
* 获取节点的数据
* @param node 节点名称
*/
@RequestMapping(value = "/getData", method = RequestMethod.GET)
public JsonResult<String> getData(String node) throws Exception {
byte[] bytes = curatorFramework.getData().forPath(node);
return new JsonResult(200, new String(bytes));
}
/**
* 获取当前节点的子节点数据
* @param node 节点名称
*/
@RequestMapping(value = "/getChildren", method = RequestMethod.GET)
public JsonResult<List<String>> getChildren(String node) throws Exception {
List<String> list = curatorFramework.getChildren().forPath(node);
return new JsonResult(200, list);
}
}
JsonResult
java
package com.example.demo.zk;
import java.io.Serializable;
/**
* 用于封装服务器到客户端的Json返回值
* @author yanqiuxiang
*
*/
public class JsonResult<T> implements Serializable {
//Serializable将对象的状态保存在存储媒体中以便可以在以后重新创建出完全相同的副本
public static final int SUCCESS=0;
public static final int ERROR=1;
public static final int OTHER=2;
private int state;
private String message = "";
private T data;
private String pass="";
public JsonResult(){
state = SUCCESS;
}
//为了方便,重载n个构造器
public JsonResult(int state, String message, T data) {
super();
this.state = state;
this.message = message;
this.data = data;
}
public JsonResult(int state,String error){
this(state,error,null);
}
public JsonResult(int state,T data){
this(state,"",data);
}
public JsonResult(String error){
this(ERROR,error,null);
}
public JsonResult(T data){
this(SUCCESS,"",data);
}
public JsonResult(int state){
this(state,"",null);
}
public JsonResult(Throwable e){
this(ERROR,e.getMessage(),null);
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public static int getSuccess() {
return SUCCESS;
}
public static int getError() {
return ERROR;
}
@Override
public String toString() {
return "JsonResult [state=" + state + ", message=" + message + ", pass=" + pass + ", data=" + data + "]";
}
}