1.导包
pom.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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.7.RELEASE</version>
</parent>
<groupId>com.cgx</groupId>
<artifactId>zk-curator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>zk-curator</name>
<description>zk-curator</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.配置类
application.properties
curator.retryCount=5
curator.elapsedTimeMs=5000
curator.connectString=192.168.81.130
curator.sessionTimeoutMs=60000
curator.connectionTimeoutMs=5000
3.读取配置文件的实体类WrapperZK
package com.cgx.zkcurator.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component //交给spring托管
@ConfigurationProperties(prefix = "curator")
public class WrapperZK {
private int retryCount;
private int elapsedTimeMs;
private String connectString;
private int sessionTimeoutMs;
private int connectionTimeoutMs;
}
4.使用配置类向Spring添加CuratorFramework
package com.cgx.zkcurator.config;
import org.apache.curator.RetryPolicy;
import org.apache.curator.RetrySleeper;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* curator配置类
*/
@Configuration
public class CuratorConfig {
@Autowired
private WrapperZK wrapperZK;
@Bean(initMethod = "start")
public CuratorFramework curatorFramework() {
return CuratorFrameworkFactory.newClient(
wrapperZK.getConnectString(),
wrapperZK.getSessionTimeoutMs(),
wrapperZK.getConnectionTimeoutMs(),
new RetryNTimes(wrapperZK.getRetryCount(), wrapperZK.getElapsedTimeMs()));
}
}
5.进行测试
package com.cgx.zkcurator;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
class ZkCuratorApplicationTests {
@Autowired
CuratorFramework curatorFramework;
@Test
void contextLoads() throws Exception {
//添加持久节点
String path = curatorFramework.create().forPath("/curator-cgx","hello".getBytes());
//添加临时节点
//String path = curatorFramework.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/myCurator", "hello".getBytes());
System.out.println(String.format("节点%s成功创建",path));
//System.in.read();
}
/**
* 测试删除节点数据
* @throws Exception
*/
@Test
public void getZKData() throws Exception {
byte[] bytes = curatorFramework.getData().forPath("/curator-cgx");
String data = new String(bytes);
System.out.println(data);
}
/**
* 测试给节点添加数据
* @throws Exception
*/
@Test
public void setZKData() throws Exception {
curatorFramework.setData().forPath("/curator-cgx","cgx666".getBytes());
byte[] bytes = curatorFramework.getData().forPath("/curator-cgx");
System.out.println(new String(bytes));
}
/**
* 测试创建时带有父节点的,带父节点一起创建
* @throws Exception
*/
@Test
public void testCreateWithParent() throws Exception {
String path = "/cgx/sub1";
//父节点没创建就先创建父节点再创建子节点
String res = curatorFramework.create().creatingParentsIfNeeded().forPath(path);
System.out.println(String.format("节点%s成功创建",res));
}
/**
* 测试删除父节点时有子节点并一起删除
* @throws Exception
*/
@Test
public void testDeleteWithChild() throws Exception {
String path = "/cgx";
curatorFramework.delete().guaranteed().deletingChildrenIfNeeded().forPath(path);
}
}