1 Star 0 Fork 0

山河已无恙 / zookeeper_demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ZookeeperDemoApplicationTests.java 10.69 KB
一键复制 编辑 原始数据 按行查看 历史
山河已无恙 提交于 2021-07-03 01:03 . java 客户端操作
package com.liruilong.zookeeper_demo;
import com.liruilong.zookeeper_demo.book_demo.ZooKeeper_Create_API_ASync_Usage;
import com.liruilong.zookeeper_demo.book_demo.ZooKeeper_Create_API_Sync_Usage;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Before;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@RunWith(SpringRunner.class)
@SpringBootTest
class ZookeeperDemoApplicationTests {
private static final Logger logger = LoggerFactory.getLogger(ZookeeperDemoApplicationTests.class);
private static CountDownLatch connectedSemaphore = new CountDownLatch(1);
@Value("${zookeeper.address}")
private String connectString;
@Value("${zookeeper.timeout}")
private int timeout;
ZooKeeper zookeeper;
@Before
void contextLoads() {
logger.info("org.apache.zookeeper:zookeeper:3.5.5--创建连接");
try {
zookeeper = new ZooKeeper(connectString,timeout,(event) -> {
//process方法,该方法负责处理来自Zookeeper服务端的Watcher通知,在收到服务端发来的SyncConnected事件之后,解除主程序在CountDownLatch上的等待阻塞。至此,客户端会话创建完毕。
if (Watcher.Event.KeeperState.SyncConnected == event.getState()) {
connectedSemaphore.countDown();
}
});
connectedSemaphore.await();
logger.info("初始化ZooKeeper连接状态....{}",zookeeper.getState());
} catch (InterruptedException e) {
e.printStackTrace();
logger.error("初始化ZooKeeper连接异常....】={}",e);
}catch(IOException e){
e.printStackTrace();
logger.error("初始化ZooKeeper连接异常....】={}",e);
}
// Arrays.stream(ZooKeeper.class.getConstructors()).forEach(System.err::println);
Arrays.stream(ZooKeeper.class.getMethods()).forEach(System.err::println);
}
@Test
void contextLoads_1(){
ZooKeeper zooKeeper = null;
try {
zooKeeper = new ZooKeeper(connectString,timeout,(event) -> {
//process方法,该方法负责处理来自Zookeeper服务端的Watcher通知,在收到服务端发来的SyncConnected事件之后,解除主程序在CountDownLatch上的等待阻塞。至此,客户端会话创建完毕。
if (Watcher.Event.KeeperState.SyncConnected == event.getState()) {
logger.warn("1--连接成功:{}", event.getState());
connectedSemaphore.countDown();
}
});
logger.warn("1--ZooKeeper连接状态....{}",zooKeeper.getState());
connectedSemaphore.await();
long sessionId = zooKeeper.getSessionId();
byte[] passwd = zooKeeper.getSessionPasswd();
System.out.println(sessionId+":"+passwd.toString());
//Use correct sessionId and sessionPassWd
ZooKeeper zooKeeper_ = new ZooKeeper(connectString, 2000, (event) -> {
//process方法,该方法负责处理来自Zookeeper服务端的Watcher通知,在收到服务端发来的SyncConnected事件之后,解除主程序在CountDownLatch上的等待阻塞。至此,客户端会话创建完毕。
logger.debug("连接状态.......{}",event.getState());
if (Watcher.Event.KeeperState.SyncConnected == event.getState()) {
logger.debug("连接成功.......{}",event.getState());
connectedSemaphore.countDown();
}
}, sessionId, passwd);
logger.warn("初始化ZooKeeper连接状态....{}",zooKeeper_.getState());
ZooKeeper zooKeeper__ = new ZooKeeper(connectString, 2000, (event) -> {
//process方法,该方法负责处理来自Zookeeper服务端的Watcher通知,在收到服务端发来的SyncConnected事件之后,解除主程序在CountDownLatch上的等待阻塞。至此,客户端会话创建完毕。
logger.debug("连接状态.......{}",event.getState());
if (Watcher.Event.KeeperState.SyncConnected == event.getState()) {
logger.debug("连接成功.......{}",event.getState());
connectedSemaphore.countDown();
}
}, sessionId, "test".getBytes());
logger.warn("初始化ZooKeeper连接状态....{}",zooKeeper__.getState());
TimeUnit.MICROSECONDS.sleep(Integer.MAX_VALUE);
} catch (InterruptedException e) {
e.printStackTrace();
logger.error("初始化ZooKeeper连接异常....】={}",e);
}catch(IOException e){
e.printStackTrace();
logger.error("初始化ZooKeeper连接异常....】={}",e);
}
}
/*
* @return
* @Description ZooKeeper API创建节点,使用异步(async)接口
* @author Liruilong
* @date 2021/7/2 23:28
**/
@Test
void contextLoads_2() throws Exception{
contextLoads();
zookeeper.create("/zk-test-ephemeral-async", "123".getBytes(),
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL,
(rc,path,ctx,name)-> logger.warn("Create path result: [" + rc + ", " + path + ", " + ctx + ", real path name: " + name)
, "I am context.");
zookeeper.create("/zk-test-ephemeral-async", "345".getBytes(),
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL,
(rc,path,ctx,name)-> logger.warn("Create path result: [" + rc + ", " + path + ", " + ctx + ", real path name: " + name)
, "I am context.");
zookeeper.create("/zk-test-ephemeral-async", "456".getBytes(),
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL,
(rc,path,ctx,name)-> logger.warn("Create path result: [" + rc + ", " + path + ", " + ctx + ", real path name: " + name)
, "I am context.");
TimeUnit.MICROSECONDS.sleep(Integer.MAX_VALUE);
}
/*
* @return
* @Description ZooKeeper API创建节点,使用同步(sync)接口。
* @author Liruilong
* @date 2021/7/2 23:28
**/
@Test
void contextLoads_3() throws Exception{
contextLoads();
String path1 = zookeeper.create("/zk-test-ephemeral-sync",
"".getBytes(),
ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.EPHEMERAL);
System.out.println("Success create znode: " + path1);
String path2 = zookeeper.create("/zk-test-ephemeral-sync",
"".getBytes(),
ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println("Success create znode: " + path2);
TimeUnit.MICROSECONDS.sleep(Integer.MAX_VALUE);
}
/*
* @return
* @Description ZooKeeper API 删除节点,使用同步(sync)接口。
* @author Liruilong
* @date 2021/7/3 0:17
**/
@Test
void contextLoads_4() throws Exception{
contextLoads();
String path1 = zookeeper.create("/zk-test-ephemeral-sync",
"".getBytes(),
ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.EPHEMERAL);
System.out.println("Success create znode: " + path1);
zookeeper.delete(path1,-1);
String path2 = zookeeper.create("/zk-test-ephemeral-sync",
"".getBytes(),
ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println("Success create znode: " + path2);
zookeeper.delete(path2,-1);
TimeUnit.MICROSECONDS.sleep(Integer.MAX_VALUE);
}
/*
* @return
* @Description 读取数据
* @author Liruilong
* @date 2021/7/3 0:44
**/
@Test
void contextLoads_5() throws Exception{
contextLoads();
String path1 = zookeeper.create("/zk-test-ephemeral-sync-1",
"".getBytes(),
ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.EPHEMERAL);
System.out.println("Success create znode: " + path1);
String path2 = zookeeper.create(path1+"/zk-test-ephemeral-sync-2",
"".getBytes(),
ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println("Success create znode: " + path2);
zookeeper.getChildren(path1,true).forEach(System.out::println);
zookeeper.getChildren(path2,true).forEach(System.out::println);;
TimeUnit.MICROSECONDS.sleep(Integer.MAX_VALUE);
}
}
// 构造方法
//public org.apache.zookeeper.ZooKeeper(java.lang.String,int,org.apache.zookeeper.Watcher,boolean,org.apache.zookeeper.client.ZKClientConfig) throws java.io.IOException
//public org.apache.zookeeper.ZooKeeper(java.lang.String,int,org.apache.zookeeper.Watcher) throws java.io.IOException
//public org.apache.zookeeper.ZooKeeper(java.lang.String,int,org.apache.zookeeper.Watcher,org.apache.zookeeper.client.ZKClientConfig) throws java.io.IOException
//public org.apache.zookeeper.ZooKeeper(java.lang.String,int,org.apache.zookeeper.Watcher,boolean,org.apache.zookeeper.client.HostProvider) throws java.io.IOException
//public org.apache.zookeeper.ZooKeeper(java.lang.String,int,org.apache.zookeeper.Watcher,boolean,org.apache.zookeeper.client.HostProvider,org.apache.zookeeper.client.ZKClientConfig) throws java.io.IOException
//public org.apache.zookeeper.ZooKeeper(java.lang.String,int,org.apache.zookeeper.Watcher,boolean) throws java.io.IOException
//public org.apache.zookeeper.ZooKeeper(java.lang.String,int,org.apache.zookeeper.Watcher,long,byte[],boolean,org.apache.zookeeper.client.HostProvider) throws java.io.IOException
//public org.apache.zookeeper.ZooKeeper(java.lang.String,int,org.apache.zookeeper.Watcher,long,byte[],boolean) throws java.io.IOException
//public org.apache.zookeeper.ZooKeeper(java.lang.String,int,org.apache.zookeeper.Watcher,long,byte[],boolean,org.apache.zookeeper.client.HostProvider,org.apache.zookeeper.client.ZKClientConfig) throws java.io.IOException
//public org.apache.zookeeper.ZooKeeper(java.lang.String,int,org.apache.zookeeper.Watcher,long,byte[]) throws java.io.IOException
Java
1
https://gitee.com/liruilonger/zookeeper_demo.git
git@gitee.com:liruilonger/zookeeper_demo.git
liruilonger
zookeeper_demo
zookeeper_demo
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891