Springboot测试ZookeeperClient
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Springboot测试ZookeeperClient,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5176字,纯文字阅读大概需要8分钟。
内容图文
引入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测试ZookeeperClient全部内容,希望文章能够帮你解决Springboot测试ZookeeperClient所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。