package com.liuyc;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.nio.charset.StandardCharsets;
@Slf4j
@SpringBootTest
public class BootZkClientApplicationTests {
@Autowired
private CuratorFramework curatorFramework;
@Test
public void createNode() throws Exception {
//添加持久节点
String path1 = curatorFramework.create().forPath("/curatorTest1","aaaaaaaaaaa".getBytes());
System.out.println("path1:"+path1);
//添加临时序号节点
String path2 = curatorFramework.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/curatorTest1", "bbbbbbbbb".getBytes());
System.out.println("path2:"+path2);
System.in.read();
}
@Test
public void getData() throws Exception {
byte[] bytes = curatorFramework.getData().forPath("/curatorTest1");
System.out.println(new String(bytes, StandardCharsets.UTF_8));
}
@Test
public void setData() throws Exception {
Stat stat = curatorFramework.setData().forPath("/curatorTest1", "ccccccc".getBytes());
System.out.println(stat);
}
/**
* 创建子节点,如父节点为空,则一起创建
* @throws Exception
*/
@Test
public void createWithParent() throws Exception {
String path = curatorFramework.create().creatingParentsIfNeeded().forPath("/curatorTest2/curatorTestTest1", "ggggg".getBytes());
System.out.println(path);
}
/**
* 删除节点
* @throws Exception
*/
@Test
public void delete() throws Exception {
Void unused = curatorFramework.delete().forPath("/curatorTest2");
System.out.println(unused);
}
/**
* 删除节点,如有子节点则一起删除
* @throws Exception
*/
@Test
public void deleteWith() throws Exception {
Void unused = curatorFramework.delete().deletingChildrenIfNeeded().forPath("/curatorTest2");
System.out.println(unused);
}
/**
* 监听节点
*/
@Test
public void addNodeWatch() throws Exception {
NodeCache nodeCache = new NodeCache(curatorFramework, "/curatorTest1");
nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
System.out.println("监听执行");
byte[] bytes = curatorFramework.getData().forPath("/curatorTest1");
System.out.println(new String(bytes, StandardCharsets.UTF_8));
}
});
nodeCache.start();
System.in.read();
}
/**
* 读锁
*/
@Test
public void getGetReadLock() throws Exception {
InterProcessReadWriteLock interProcessReadWriteLock =new InterProcessReadWriteLock(curatorFramework,"/lock1");
InterProcessMutex interProcessMutex = interProcessReadWriteLock.readLock();
System.out.println("等待获取读锁对象");
//获取锁
interProcessMutex.acquire();
for (int i = 0; i < 100; i++) {
Thread.sleep(3000);
System.out.println(i);
}
interProcessMutex.release();
System.out.println("等待释放读锁");
}
/**
* 读锁
*/
@Test
public void getGetReadLock1() throws Exception {
InterProcessReadWriteLock interProcessReadWriteLock =new InterProcessReadWriteLock(curatorFramework,"/lock1");
InterProcessMutex interProcessMutex = interProcessReadWriteLock.readLock();
System.out.println("等待获取读锁对象");
//获取锁
interProcessMutex.acquire();
for (int i = 0; i < 100; i++) {
Thread.sleep(3000);
System.out.println(i);
}
interProcessMutex.release();
System.out.println("等待释放读锁");
}
/**
* 写锁
*/
@Test
public void getGetWriteLock() throws Exception {
InterProcessReadWriteLock interProcessReadWriteLock =new InterProcessReadWriteLock(curatorFramework,"/lock1");
InterProcessMutex interProcessMutex = interProcessReadWriteLock.writeLock();
System.out.println("等待获取写锁对象");
//获取锁
interProcessMutex.acquire();
for (int i = 0; i < 100; i++) {
Thread.sleep(3000);
System.out.println(i);
}
interProcessMutex.release();
System.out.println("等待释放写锁");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
zookeeper-demo.7z
共30个文件
java:8个
xml:7个
class:6个
需积分: 2 0 下载量 113 浏览量
2024-05-20
17:12:15
上传
评论
收藏 7KB 7Z 举报
温馨提示
java客户端连接zookeeper服务端
资源推荐
资源详情
资源评论
收起资源包目录
zookeeper-demo.7z (30个子文件)
zookeeper-demo
pom.xml 2KB
src
test
resources
java
com
liuyc
LiuycDemoApplicationTests.java 411B
BootZkClientApplicationTests.java 5KB
main
resources
application.properties 148B
application.yml 0B
java
GCTest.java 252B
com
liuyc
processor
MyBeanPostProcessor.java 623B
controller
UserController.java 341B
Main.java 298B
config
CuratorConfig.java 882B
WrapperZK.java 442B
webapp
WEB-INF
web.xml 222B
index.jsp 57B
.idea
jarRepositories.xml 864B
workspace.xml 6KB
misc.xml 541B
compiler.xml 951B
.gitignore 50B
encodings.xml 267B
target
classes
application.properties 148B
GCTest.class 660B
application.yml 0B
com
liuyc
processor
MyBeanPostProcessor.class 923B
controller
UserController.class 630B
Main.class 686B
config
WrapperZK.class 3KB
CuratorConfig.class 1KB
maven-status
maven-compiler-plugin
compile
default-compile
createdFiles.lst 192B
inputFiles.lst 474B
generated-sources
annotations
.gitignore 490B
共 30 条
- 1
资源评论
北极熊不在北极
- 粉丝: 14
- 资源: 26
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功