Springboot测试ZookeeperClient
作者:互联网
引入jar包:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cc</groupId>
<artifactId>springboot-zookeeper-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-zookeeper-client</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.13</version>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
测试单机client:
package com.cc.springbootzookeeperclient;
import lombok.extern.slf4j.Slf4j;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;
import org.apache.zookeeper.CreateMode;
import java.util.List;
/**
* Created by CarlosXiao on 2018/7/28.
*/
@Slf4j
public class ZkClientTest {
public static final String ZK_HOST = "192.168.13.51:2182,192.168.13.51:2183,192.168.13.51:2184,192.168.13.51:2185";
public static void main(String [] args) throws InterruptedException {
// 1、创建链接
ZkClient zkClient = new ZkClient(ZK_HOST);
// ZkClient zkClient = new ZkClient(new ZkConnection(ZK_HOST), 5000);
// 2、创建节点
//zkClient.create("/zk", "test", CreateMode.PERSISTENT);
//zkClient.createPersistent("/zkclient/test", true);
// 修改节点内容
// zkClient.writeData("/zkclient/test", "modify data");
// 3、删除节点
//zkClient.delete("/zk");
// 递归删除
//zkClient.deleteRecursive("/zkclient/test");
//List<String> childrens = zkClient.getChildren("/test");
//log.info("childrens: {}", childrens);
zkClient.subscribeChildChanges("/test", new IZkChildListener() {
@Override
public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
log.info("parent node: {}", parentPath);
log.info("current childs: {}", currentChilds);
}
});
Thread.sleep(3000);
zkClient.createPersistent("/test/a");
Thread.sleep(1000);
zkClient.deleteRecursive("/test");
Thread.sleep(Integer.MAX_VALUE);
}
}
实现通知:
package com.cc.springbootzookeeperclient;
import lombok.extern.slf4j.Slf4j;
import org.apache.zookeeper.*;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;
/**
* Created by CarlosXiao on 2018/7/28.
*/
@Slf4j
public class ZookeeperTest implements Watcher{
public static final String ZK_HOST = "192.168.13.51:2182,192.168.13.51:2183,192.168.13.51:2184,192.168.13.51:2185";
private static CountDownLatch countDownLatch = new CountDownLatch(1);
public static void main(String [] args) throws IOException, InterruptedException, KeeperException {
// 1、 创建会话,
/**
* 1、 链接字符串 192.168.13.51:2182,192.168.13.51:2183
*/
ZooKeeper zooKeeper = new ZooKeeper(ZK_HOST, 5000, new ZookeeperTest());
countDownLatch.await();
// 2. 创建节点
zooKeeper.create("/test", "aaa".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
// 3. 设置节点内容
zooKeeper.setData("/test", "bbb".getBytes(), -1);
zooKeeper.create("/test/1", "aaa".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zooKeeper.create("/test/2", "aaa".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zooKeeper.create("/test/3", "aaa".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zooKeeper.create("/test/4", "aaa".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
// 5. 获取子节点
List<String> childrens = zooKeeper.getChildren("/test", false);
log.info("childrens: {}", childrens);
// 4. 节点删除
zooKeeper.delete("/test", -1);
Thread.sleep(Integer.MAX_VALUE);
}
@Override
public void process(WatchedEvent event) {
if (event.getState() == Event.KeeperState.SyncConnected) {
log.info("--------------------------> 已连接");
countDownLatch.countDown();
}
}
}
标签:Springboot,测试,13.51,192.168,test,zkClient,import,ZookeeperClient,org 来源: https://blog.csdn.net/qq_39246466/article/details/115659730