版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_38004638/article/details/96641877
安装好zk的机器(ip:192.168.10.201) 启动zkServer:bin/zkServer.sh localhost:2181 start
连接失败原因:
防火墙未关闭
systemctl status firewalld.service #检查防火墙状态
systemctl stop firewalld.service #关闭防火墙
systemctl disable firewalld.service #禁止开机启动防火墙
连接超时
增加连接时长 ZooKeeper zk = new ZooKeeper("10.0.0.11:2181", 15000, null);
增加maxWaitTime,maxSleepTime
<?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>top.it1002</groupId> <artifactId>ZooKeeperDemo</artifactId> <version>1.0-SNAPSHOT</version>
<dependencies> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.13</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>
package com.zk.test;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;
import java.util.ArrayList;
public class ZooKeeperCURDTest {
int version = 0;
/**
* 迭代获取父节点的子节点
*/
public ArrayList<String> iterChildNodeList(String parentNodeName, ZooKeeper zooKeeper) {
if (parentNodeName != null && !parentNodeName.equals("")) {
try {
ArrayList<String> childNodeList = (ArrayList<String>) zooKeeper.getChildren(parentNodeName, null);
if (childNodeList.size() > 0) {
System.out.println("父结点:" + parentNodeName);
for (String childNode : childNodeList) {
String childNodePath = "";
if (!parentNodeName.equals("/")) {
childNodePath = parentNodeName + "/" + childNode;
} else {
childNodePath = parentNodeName + childNode;
}
System.out.println(parentNodeName + "的子节点:" + childNodePath);
iterChildNodeList(childNodePath, zooKeeper);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return new ArrayList<String>();
}
/**
* 获取服务器节点列表
*/
@Test
public void listTest() {
try {
ZooKeeper zooKeeper = new ZooKeeper("192.168.44.128:2181", 10000, null);//192.168.10.201:2181
String root = "/";
iterChildNodeList(root, zooKeeper);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取指定节点的节点数据
*/
@Test
public void getDataTest() {
try {
ZooKeeper zooKeeper = new ZooKeeper("192.168.44.128:2181", 10000, null);
String path = "/myZnode";
String data = new String(zooKeeper.getData(path, null, new Stat()));
System.out.println(data);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 节点创建测试
*/
@Test
public void createTest() {
try {
ZooKeeper zooKeeper = new ZooKeeper("192.168.44.128:2181", 10000, null);
String path = "/my";
String dataStr = "100";
byte[] data = dataStr.getBytes();
//String res;
//CreateMode:(1)PERSISTENT:持久;(2)PERSISTENT_SEQUENTIAL:持久顺序;(3)EPHEMERAL:临时;(4)EPHEMERAL_SEQUENTIAL:临时顺序。
if (null==zooKeeper.exists(path, false)) {
//不存在节点,则新建
zooKeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
else{
//存在节点,则删除后新建
//zooKeeper.delete(path, zooKeeper.exists(path, null).getVersion());
//zooKeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
//存在节点,则更新数据
setTest();
//zooKeeper.setData(path, data, version);
}
String newData = new String(zooKeeper.getData(path, null, new Stat()));
if (newData != null && !newData.equals("")) {
System.out.println("插入节点为:" + path);
System.out.println("新插入数据为:" + newData);
} else {
System.out.println("创建失败!");
}
} catch (Exception e) {
System.out.println(e);
}
}
/**
* 节点数据更新测试
*/
@Test
public void setTest() {
try {
ZooKeeper zooKeeper = new ZooKeeper("192.168.44.128:2181", 10000, null);
// 修改的节点
String path = "/my";
// 修改的新数据
byte[] data = new String("man").getBytes();
// 未修改过版本号为0,修改后版本号自动递增1
version = zooKeeper.exists(path,true).getVersion();
System.out.println("更新前新版本号为:" + version);
// 修改之前的节点数据
String beforeData = new String(zooKeeper.getData(path, null, new Stat()));
System.out.println("更新之前数据为:" + beforeData);
//更新
Stat stat = zooKeeper.setData(path, data, version);
// 修改之后的版本号
version = stat.getVersion();
System.out.println("更新数据后新版本号为:" + version);
// 修改之后的节点数据
String afterData = new String(zooKeeper.getData(path, null, new Stat()));
System.out.println("更新之后数据为:" + afterData);
} catch (Exception e) {
System.out.println(e);
}
}
/**
* 节点删除测试
*/
@Test
public void deleteTest() {
try {
ZooKeeper zooKeeper = new ZooKeeper("192.168.44.128:2181", 10000, null);
// 删除的节点
String path = "/my";
Stat stat = zooKeeper.exists(path, null);
// 删除的节点的版本
int version = stat.getVersion();
// 执行删除
zooKeeper.delete(path, version);
//zooKeeper.delete("/my", zooKeeper.exists(path, null).getVersion());
System.out.println("该节点已删除,当前节点如下:");
// 删除后列出最新的zk节点结构
listTest();
} catch (Exception e) {
System.out.println(e);
}
}
}
参考:
https://blog.csdn.net/qq_41287993/article/details/88889553
https://blog.csdn.net/eieiei438/article/details/80223955