package com.yqq.zookeeper.curd;

import org.apache.log4j.Logger;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
* @Author yqq
* @Date 2021/10/24 00:25
* @Version 1.0
*/
public class ZKCurd {
//定义会话的超时时间
private final static int SESSION_TIME = 30000;
//定义zk集群的ip地址
private final static String ZK_SERVERS = "192.168.134.102:2181,192.168.134.103:2181,192.168.134.104:2181";
//日志对象
private final static Logger LOGGER = Logger.getLogger(ZKCurd.class);
private ZooKeeper zooKeeper = null;
private Watcher watcher = new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
LOGGER.info("event:"+watchedEvent.toString());
}
};
@Before
public void connect() throws IOException {
zooKeeper = new ZooKeeper(ZK_SERVERS,SESSION_TIME,watcher);
//获取当前的会话sessionID
Long sessionId = zooKeeper.getSessionId();
LOGGER.info("sessionId:"+sessionId);
System.out.println("zookeeper:"+zooKeeper);
}
@After
public void close() throws InterruptedException {
zooKeeper.close();
}
/**
* 创建节点
* final String path:要创建的节点的全路径
* byte data[]:节点中数据内容
* List<ACL> acl:节点权限
* CreateModel createModel:节点类型
* *PERSISTENT:普通持久节点
* *PERSISTENT_SEQUENTIAL:普通临时持久节点
* *EPHEMERAL:普通临时节点
* *EPHEMERAL_SEQUENTIAL:普通临时有序节点
*/
@Test
public void create() {
String result = null;
try {
// result = zooKeeper.create("/James","good nice".getBytes(StandardCharsets.UTF_8),
// ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
result = zooKeeper.create("/JamesLLL","good nice".getBytes(StandardCharsets.UTF_8),
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
} catch (Exception e) {
LOGGER.error(e.getMessage());
Assert.fail();
}
LOGGER.info("create result:{"+result+"}");
}
@Test
public void exist(){
try {
Stat stat = zooKeeper.exists("/zhangsan",false);
if(stat==null)
LOGGER.error("/zhangsan not exits");
else
LOGGER.info("/zhangsan exits");
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
}
@Test
public void getData(){
String result = null;
try {
byte[] data = zooKeeper.getData("/James",null,null);
result = new String(data);
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
LOGGER.info("getData result :"+result);
}
@Test
public void getChildren(){
try {
List<String> childrenList = zooKeeper.getChildren("/",true);
childrenList.forEach(e -> {
LOGGER.info("child :"+e);
});
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
}
@Test
public void getChildrenData(){
try {
List<String> childrenList = zooKeeper.getChildren("/",true);
childrenList.forEach(e -> {
try {
byte[] datas = zooKeeper.getData("/"+e,null,null);
String data = new String(datas);
LOGGER.info("getChildData :"+data);
} catch (Exception e1) {
LOGGER.error(e1.getMessage());
}
LOGGER.info("child :"+e);
});
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
}
@Test
public void setData(){
Stat stat = null;
try {
//-1 表示不管version的值为几都能修改
stat = zooKeeper.setData("/James","詹姆斯".getBytes(StandardCharsets.UTF_8),-1);
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
//dataVersion:getVersion获取的节点值的版本,每被修改一次,该值加1
LOGGER.info("version:{"+stat.getVersion()+"}");
}
}
[zk: localhost:2181(CONNECTED) 8] get -s /James
good nice
cZxid = 0x200000035
ctime = Sun Oct 24 01:25:57 CST 2021
mZxid = 0x200000035
mtime = Sun Oct 24 01:25:57 CST 2021
pZxid = 0x300000007
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 1
[zk: localhost:2181(CONNECTED) 9] get -s /James
詹姆斯
cZxid = 0x200000035
ctime = Sun Oct 24 01:25:57 CST 2021
mZxid = 0x300000011
mtime = Sun Oct 24 11:55:27 CST 2021
pZxid = 0x300000007
cversion = 1
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 1
2021-10-24 11:55:25,690 INFO [com.yqq.zookeeper.curd.ZKCurd] - version:{1}