Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
zookeeper-demo
提交
d4e41daf
Z
zookeeper-demo
项目概览
Kwan的解忧杂货铺@新空间代码工作室
/
zookeeper-demo
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Z
zookeeper-demo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
d4e41daf
编写于
5月 15, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:Watcher监听
上级
d1778871
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
175 addition
and
45 deletion
+175
-45
src/test/java/com/kwan/shuyu/watch/Watcher_01_NodeCache.java
src/test/java/com/kwan/shuyu/watch/Watcher_01_NodeCache.java
+84
-0
src/test/java/com/kwan/shuyu/watch/Watcher_02_PathChildrenCache.java
...va/com/kwan/shuyu/watch/Watcher_02_PathChildrenCache.java
+9
-45
src/test/java/com/kwan/shuyu/watch/Watcher_03_TreeCache.java
src/test/java/com/kwan/shuyu/watch/Watcher_03_TreeCache.java
+82
-0
未找到文件。
src/test/java/com/kwan/shuyu/watch/Watcher_01_NodeCache.java
0 → 100644
浏览文件 @
d4e41daf
package
com.kwan.shuyu.watch
;
import
org.apache.curator.RetryPolicy
;
import
org.apache.curator.framework.CuratorFramework
;
import
org.apache.curator.framework.CuratorFrameworkFactory
;
import
org.apache.curator.framework.recipes.cache.*
;
import
org.apache.curator.retry.ExponentialBackoffRetry
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
/**
* Watcher监听
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/15 10:29
*/
public
class
Watcher_01_NodeCache
{
private
CuratorFramework
client
;
/**
* 建立连接
*/
@Before
public
void
testConnect
()
{
/*
*
* @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183"
* @param sessionTimeoutMs 会话超时时间 单位ms
* @param connectionTimeoutMs 连接超时时间 单位ms
* @param retryPolicy 重试策略
*/
/* //重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
//1.第一种方式
CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183",
60 * 1000, 15 * 1000, retryPolicy);*/
//重试策略
RetryPolicy
retryPolicy
=
new
ExponentialBackoffRetry
(
3000
,
10
);
//2.第二种方式
//CuratorFrameworkFactory.builder();
client
=
CuratorFrameworkFactory
.
builder
()
.
connectString
(
"120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183"
)
.
sessionTimeoutMs
(
60
*
1000
)
.
connectionTimeoutMs
(
15
*
1000
)
.
retryPolicy
(
retryPolicy
)
.
namespace
(
"itheima"
)
.
build
();
//开启连接
client
.
start
();
}
@After
public
void
close
()
{
if
(
client
!=
null
)
{
client
.
close
();
}
}
/**
* 演示 NodeCache:给指定一个节点注册监听器
*/
@Test
public
void
testNodeCache
()
throws
Exception
{
//1. 创建NodeCache对象
final
NodeCache
nodeCache
=
new
NodeCache
(
client
,
"/app1"
);
//2. 注册监听
nodeCache
.
getListenable
().
addListener
(
new
NodeCacheListener
()
{
@Override
public
void
nodeChanged
()
throws
Exception
{
System
.
out
.
println
(
"节点变化了~"
);
//获取修改节点后的数据
byte
[]
data
=
nodeCache
.
getCurrentData
().
getData
();
System
.
out
.
println
(
new
String
(
data
));
}
});
//3. 开启监听.如果设置为true,则开启监听是,加载缓冲数据
nodeCache
.
start
(
true
);
while
(
true
)
{
}
}
}
\ No newline at end of file
src/test/java/com/kwan/shuyu/watch/
CuratorWatcherTest
.java
→
src/test/java/com/kwan/shuyu/watch/
Watcher_02_PathChildrenCache
.java
浏览文件 @
d4e41daf
...
@@ -9,7 +9,15 @@ import org.junit.After;
...
@@ -9,7 +9,15 @@ import org.junit.After;
import
org.junit.Before
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.Test
;
public
class
CuratorWatcherTest
{
/**
* Watcher监听
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/15 10:29
*/
public
class
Watcher_02_PathChildrenCache
{
private
CuratorFramework
client
;
private
CuratorFramework
client
;
/**
/**
...
@@ -51,29 +59,6 @@ public class CuratorWatcherTest {
...
@@ -51,29 +59,6 @@ public class CuratorWatcherTest {
}
}
}
}
/**
* 演示 NodeCache:给指定一个节点注册监听器
*/
@Test
public
void
testNodeCache
()
throws
Exception
{
//1. 创建NodeCache对象
final
NodeCache
nodeCache
=
new
NodeCache
(
client
,
"/app1"
);
//2. 注册监听
nodeCache
.
getListenable
().
addListener
(
new
NodeCacheListener
()
{
@Override
public
void
nodeChanged
()
throws
Exception
{
System
.
out
.
println
(
"节点变化了~"
);
//获取修改节点后的数据
byte
[]
data
=
nodeCache
.
getCurrentData
().
getData
();
System
.
out
.
println
(
new
String
(
data
));
}
});
//3. 开启监听.如果设置为true,则开启监听是,加载缓冲数据
nodeCache
.
start
(
true
);
while
(
true
)
{
}
}
/**
/**
* 演示 PathChildrenCache:监听某个节点的所有子节点们
* 演示 PathChildrenCache:监听某个节点的所有子节点们
*/
*/
...
@@ -103,25 +88,4 @@ public class CuratorWatcherTest {
...
@@ -103,25 +88,4 @@ public class CuratorWatcherTest {
while
(
true
)
{
while
(
true
)
{
}
}
}
}
/**
* 演示 TreeCache:监听某个节点自己和所有子节点们
*/
@Test
public
void
testTreeCache
()
throws
Exception
{
//1. 创建监听器
TreeCache
treeCache
=
new
TreeCache
(
client
,
"/app2"
);
//2. 注册监听
treeCache
.
getListenable
().
addListener
(
new
TreeCacheListener
()
{
@Override
public
void
childEvent
(
CuratorFramework
client
,
TreeCacheEvent
event
)
throws
Exception
{
System
.
out
.
println
(
"节点变化了"
);
System
.
out
.
println
(
event
);
}
});
//3. 开启
treeCache
.
start
();
while
(
true
)
{
}
}
}
}
\ No newline at end of file
src/test/java/com/kwan/shuyu/watch/Watcher_03_TreeCache.java
0 → 100644
浏览文件 @
d4e41daf
package
com.kwan.shuyu.watch
;
import
org.apache.curator.RetryPolicy
;
import
org.apache.curator.framework.CuratorFramework
;
import
org.apache.curator.framework.CuratorFrameworkFactory
;
import
org.apache.curator.framework.recipes.cache.*
;
import
org.apache.curator.retry.ExponentialBackoffRetry
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
/**
* Watcher监听
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/15 10:29
*/
public
class
Watcher_03_TreeCache
{
private
CuratorFramework
client
;
/**
* 建立连接
*/
@Before
public
void
testConnect
()
{
/*
*
* @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183"
* @param sessionTimeoutMs 会话超时时间 单位ms
* @param connectionTimeoutMs 连接超时时间 单位ms
* @param retryPolicy 重试策略
*/
/* //重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
//1.第一种方式
CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183",
60 * 1000, 15 * 1000, retryPolicy);*/
//重试策略
RetryPolicy
retryPolicy
=
new
ExponentialBackoffRetry
(
3000
,
10
);
//2.第二种方式
//CuratorFrameworkFactory.builder();
client
=
CuratorFrameworkFactory
.
builder
()
.
connectString
(
"120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183"
)
.
sessionTimeoutMs
(
60
*
1000
)
.
connectionTimeoutMs
(
15
*
1000
)
.
retryPolicy
(
retryPolicy
)
.
namespace
(
"itheima"
)
.
build
();
//开启连接
client
.
start
();
}
@After
public
void
close
()
{
if
(
client
!=
null
)
{
client
.
close
();
}
}
/**
* 演示 TreeCache:监听某个节点自己和所有子节点们
*/
@Test
public
void
testTreeCache
()
throws
Exception
{
//1. 创建监听器
TreeCache
treeCache
=
new
TreeCache
(
client
,
"/app2"
);
//2. 注册监听
treeCache
.
getListenable
().
addListener
(
new
TreeCacheListener
()
{
@Override
public
void
childEvent
(
CuratorFramework
client
,
TreeCacheEvent
event
)
throws
Exception
{
System
.
out
.
println
(
"节点变化了"
);
System
.
out
.
println
(
event
);
}
});
//3. 开启
treeCache
.
start
();
while
(
true
)
{
}
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录