Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
zookeeper-demo
提交
d1778871
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看板
提交
d1778871
编写于
5月 15, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:zookeeper Curator 操作java api
上级
98c42d95
变更
16
显示空白变更内容
内联
并排
Showing
16 changed file
with
994 addition
and
7 deletion
+994
-7
src/main/java/com/kwan/shuyu/Ticket12306.java
src/main/java/com/kwan/shuyu/Ticket12306.java
+2
-2
src/test/java/com/kwan/shuyu/Ticket12306LockTest.java
src/test/java/com/kwan/shuyu/Ticket12306LockTest.java
+9
-1
src/test/java/com/kwan/shuyu/crud/CuratorTest_01_Create.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_01_Create.java
+75
-0
src/test/java/com/kwan/shuyu/crud/CuratorTest_02_Create.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_02_Create.java
+75
-0
src/test/java/com/kwan/shuyu/crud/CuratorTest_03_Create.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_03_Create.java
+76
-0
src/test/java/com/kwan/shuyu/crud/CuratorTest_04_Create.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_04_Create.java
+77
-0
src/test/java/com/kwan/shuyu/crud/CuratorTest_05_Get.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_05_Get.java
+73
-0
src/test/java/com/kwan/shuyu/crud/CuratorTest_06_Get.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_06_Get.java
+74
-0
src/test/java/com/kwan/shuyu/crud/CuratorTest_07_Get.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_07_Get.java
+75
-0
src/test/java/com/kwan/shuyu/crud/CuratorTest_08_Set.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_08_Set.java
+72
-0
src/test/java/com/kwan/shuyu/crud/CuratorTest_09_Set.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_09_Set.java
+78
-0
src/test/java/com/kwan/shuyu/crud/CuratorTest_10_Delete.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_10_Delete.java
+74
-0
src/test/java/com/kwan/shuyu/crud/CuratorTest_11_Delete.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_11_Delete.java
+74
-0
src/test/java/com/kwan/shuyu/crud/CuratorTest_12_Delete.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_12_Delete.java
+74
-0
src/test/java/com/kwan/shuyu/crud/CuratorTest_13_Delete.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_13_Delete.java
+82
-0
src/test/java/com/kwan/shuyu/watch/CuratorWatcherTest.java
src/test/java/com/kwan/shuyu/watch/CuratorWatcherTest.java
+4
-4
未找到文件。
src/main/java/com/kwan/shuyu/Ticket12306.java
浏览文件 @
d1778871
...
@@ -27,9 +27,9 @@ public class Ticket12306 implements Runnable {
...
@@ -27,9 +27,9 @@ public class Ticket12306 implements Runnable {
//重试策略
//重试策略
RetryPolicy
retryPolicy
=
new
ExponentialBackoffRetry
(
3000
,
10
);
RetryPolicy
retryPolicy
=
new
ExponentialBackoffRetry
(
3000
,
10
);
//2.第二种方式
//2.第二种方式
//CuratorFrameworkFactory.builder();
CuratorFramework
client
=
CuratorFrameworkFactory
.
builder
()
CuratorFramework
client
=
CuratorFrameworkFactory
.
builder
()
.
connectString
(
"192.168.149.135:2181"
)
//集群节点
.
connectString
(
"120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183"
)
.
sessionTimeoutMs
(
60
*
1000
)
.
sessionTimeoutMs
(
60
*
1000
)
.
connectionTimeoutMs
(
15
*
1000
)
.
connectionTimeoutMs
(
15
*
1000
)
.
retryPolicy
(
retryPolicy
)
.
retryPolicy
(
retryPolicy
)
...
...
src/test/java/com/kwan/shuyu/LockTest.java
→
src/test/java/com/kwan/shuyu/
Ticket12306
LockTest.java
浏览文件 @
d1778871
package
com.kwan.shuyu
;
package
com.kwan.shuyu
;
public
class
LockTest
{
/**
* InterProcessMutex 分布式锁测试
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/15 09:53
*/
public
class
Ticket12306LockTest
{
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
Ticket12306
ticket12306
=
new
Ticket12306
();
Ticket12306
ticket12306
=
new
Ticket12306
();
//创建客户端
//创建客户端
...
...
src/test/java/com/kwan/shuyu/crud/CuratorTest_01_Create.java
0 → 100644
浏览文件 @
d1778871
package
com.kwan.shuyu.crud
;
import
org.apache.curator.RetryPolicy
;
import
org.apache.curator.framework.CuratorFramework
;
import
org.apache.curator.framework.CuratorFrameworkFactory
;
import
org.apache.curator.retry.ExponentialBackoffRetry
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
/**
* 创建节点 带有数据
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/15 09:54
*/
public
class
CuratorTest_01_Create
{
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
();
}
}
/**
* 创建节点:create 持久 临时 顺序 数据
* 1. 基本创建 :create().forPath("")
* 2. 创建节点 带有数据:create().forPath("",data)
* 3. 设置节点的类型:create().withMode().forPath("",data)
* 4. 创建多级节点 /app1/p1 :create().creatingParentsIfNeeded().forPath("",data)
*/
@Test
public
void
testCreate
()
throws
Exception
{
//默认是持久无序节点
//如果创建节点,没有指定数据,则默认将当前客户端的ip作为数据存储
String
path
=
client
.
create
().
forPath
(
"/app2"
,
"hehe"
.
getBytes
());
System
.
out
.
println
(
path
);
}
}
\ No newline at end of file
src/test/java/com/kwan/shuyu/crud/CuratorTest_02_Create.java
0 → 100644
浏览文件 @
d1778871
package
com.kwan.shuyu.crud
;
import
org.apache.curator.RetryPolicy
;
import
org.apache.curator.framework.CuratorFramework
;
import
org.apache.curator.framework.CuratorFrameworkFactory
;
import
org.apache.curator.retry.ExponentialBackoffRetry
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
/**
* 创建节点 不带数据
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/15 09:54
*/
public
class
CuratorTest_02_Create
{
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
();
}
}
/**
* 创建节点:create 持久 临时 顺序 数据
* 1. 基本创建 :create().forPath("")
* 2. 创建节点 带有数据:create().forPath("",data)
* 3. 设置节点的类型:create().withMode().forPath("",data)
* 4. 创建多级节点 /app1/p1 :create().creatingParentsIfNeeded().forPath("",data)
*/
@Test
public
void
testCreate2
()
throws
Exception
{
//基本创建
//如果创建节点,没有指定数据,则默认将当前客户端的ip作为数据存储
String
path
=
client
.
create
().
forPath
(
"/app1"
);
System
.
out
.
println
(
path
);
}
}
\ No newline at end of file
src/test/java/com/kwan/shuyu/crud/CuratorTest_03_Create.java
0 → 100644
浏览文件 @
d1778871
package
com.kwan.shuyu.crud
;
import
org.apache.curator.RetryPolicy
;
import
org.apache.curator.framework.CuratorFramework
;
import
org.apache.curator.framework.CuratorFrameworkFactory
;
import
org.apache.curator.retry.ExponentialBackoffRetry
;
import
org.apache.zookeeper.CreateMode
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
/**
* 设置节点的类型
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/15 09:54
*/
public
class
CuratorTest_03_Create
{
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
();
}
}
/**
* 创建节点:create 持久 临时 顺序 数据
* 1. 基本创建 :create().forPath("")
* 2. 创建节点 带有数据:create().forPath("",data)
* 3. 设置节点的类型:create().withMode().forPath("",data)
* 4. 创建多级节点 /app1/p1 :create().creatingParentsIfNeeded().forPath("",data)
*/
@Test
public
void
testCreate3
()
throws
Exception
{
//设置节点的类型
//默认类型:持久化
String
path
=
client
.
create
().
withMode
(
CreateMode
.
EPHEMERAL
).
forPath
(
"/app3"
);
System
.
out
.
println
(
path
);
}
}
\ No newline at end of file
src/test/java/com/kwan/shuyu/crud/CuratorTest_04_Create.java
0 → 100644
浏览文件 @
d1778871
package
com.kwan.shuyu.crud
;
import
org.apache.curator.RetryPolicy
;
import
org.apache.curator.framework.CuratorFramework
;
import
org.apache.curator.framework.CuratorFrameworkFactory
;
import
org.apache.curator.retry.ExponentialBackoffRetry
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
/**
* 创建多级节点
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/15 09:54
*/
public
class
CuratorTest_04_Create
{
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
();
}
}
/**
* 创建节点:create 持久 临时 顺序 数据
* 1. 基本创建 :create().forPath("")
* 2. 创建节点 带有数据:create().forPath("",data)
* 3. 设置节点的类型:create().withMode().forPath("",data)
* 4. 创建多级节点 /app1/p1 :create().creatingParentsIfNeeded().forPath("",data)
*/
@Test
public
void
testCreate4
()
throws
Exception
{
//4. 创建多级节点 /app1/p1
//creatingParentsIfNeeded():如果父节点不存在,则创建父节点
String
path
=
client
.
create
().
creatingParentsIfNeeded
().
forPath
(
"/app4/p1"
);
System
.
out
.
println
(
path
);
}
}
\ No newline at end of file
src/test/java/com/kwan/shuyu/crud/CuratorTest_05_Get.java
0 → 100644
浏览文件 @
d1778871
package
com.kwan.shuyu.crud
;
import
org.apache.curator.RetryPolicy
;
import
org.apache.curator.framework.CuratorFramework
;
import
org.apache.curator.framework.CuratorFrameworkFactory
;
import
org.apache.curator.retry.ExponentialBackoffRetry
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
/**
* 查询数据
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/15 09:54
*/
public
class
CuratorTest_05_Get
{
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
();
}
}
/**
* 查询节点:
* 1. 查询数据:get: getData().forPath()
* 2. 查询子节点: ls: getChildren().forPath()
* 3. 查询节点状态信息:ls -s:getData().storingStatIn(状态对象).forPath()
*/
@Test
public
void
testGet1
()
throws
Exception
{
//1. 查询数据:get
byte
[]
data
=
client
.
getData
().
forPath
(
"/app1"
);
System
.
out
.
println
(
new
String
(
data
));
}
}
\ No newline at end of file
src/test/java/com/kwan/shuyu/crud/CuratorTest_06_Get.java
0 → 100644
浏览文件 @
d1778871
package
com.kwan.shuyu.crud
;
import
org.apache.curator.RetryPolicy
;
import
org.apache.curator.framework.CuratorFramework
;
import
org.apache.curator.framework.CuratorFrameworkFactory
;
import
org.apache.curator.retry.ExponentialBackoffRetry
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
java.util.List
;
/**
* 查询子节点
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/15 09:54
*/
public
class
CuratorTest_06_Get
{
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
();
}
}
/**
* 查询节点:
* 1. 查询数据:get: getData().forPath()
* 2. 查询子节点: ls: getChildren().forPath()
* 3. 查询节点状态信息:ls -s:getData().storingStatIn(状态对象).forPath()
*/
@Test
public
void
testGet2
()
throws
Exception
{
// 2. 查询子节点: ls
List
<
String
>
path
=
client
.
getChildren
().
forPath
(
"/"
);
System
.
out
.
println
(
path
);
}
}
\ No newline at end of file
src/test/java/com/kwan/shuyu/crud/CuratorTest_07_Get.java
0 → 100644
浏览文件 @
d1778871
package
com.kwan.shuyu.crud
;
import
org.apache.curator.RetryPolicy
;
import
org.apache.curator.framework.CuratorFramework
;
import
org.apache.curator.framework.CuratorFrameworkFactory
;
import
org.apache.curator.retry.ExponentialBackoffRetry
;
import
org.apache.zookeeper.data.Stat
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
/**
* 查询节点状态信息
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/15 09:54
*/
public
class
CuratorTest_07_Get
{
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
();
}
}
/**
* 查询节点:
* 1. 查询数据:get: getData().forPath()
* 2. 查询子节点: ls: getChildren().forPath()
* 3. 查询节点状态信息:ls -s:getData().storingStatIn(状态对象).forPath()
*/
@Test
public
void
testGet3
()
throws
Exception
{
Stat
status
=
new
Stat
();
System
.
out
.
println
(
status
);
//3. 查询节点状态信息:ls -s
client
.
getData
().
storingStatIn
(
status
).
forPath
(
"/app1"
);
System
.
out
.
println
(
status
);
}
}
\ No newline at end of file
src/test/java/com/kwan/shuyu/crud/CuratorTest_08_Set.java
0 → 100644
浏览文件 @
d1778871
package
com.kwan.shuyu.crud
;
import
org.apache.curator.RetryPolicy
;
import
org.apache.curator.framework.CuratorFramework
;
import
org.apache.curator.framework.CuratorFrameworkFactory
;
import
org.apache.curator.retry.ExponentialBackoffRetry
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
/**
* 基本修改数据
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/15 09:54
*/
public
class
CuratorTest_08_Set
{
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
();
}
}
/**
* 修改数据
* 1. 基本修改数据:setData().forPath()
* 2. 根据版本修改: setData().withVersion().forPath()
* * version 是通过查询出来的。目的就是为了让其他客户端或者线程不干扰我。
*
* @throws Exception
*/
@Test
public
void
testSet
()
throws
Exception
{
client
.
setData
().
forPath
(
"/app1"
,
"itcast"
.
getBytes
());
}
}
\ No newline at end of file
src/test/java/com/kwan/shuyu/crud/CuratorTest_09_Set.java
0 → 100644
浏览文件 @
d1778871
package
com.kwan.shuyu.crud
;
import
org.apache.curator.RetryPolicy
;
import
org.apache.curator.framework.CuratorFramework
;
import
org.apache.curator.framework.CuratorFrameworkFactory
;
import
org.apache.curator.retry.ExponentialBackoffRetry
;
import
org.apache.zookeeper.data.Stat
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
/**
* 根据版本修改
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/15 09:54
*/
public
class
CuratorTest_09_Set
{
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
();
}
}
/**
* 修改数据
* 1. 基本修改数据:setData().forPath()
* 2. 根据版本修改: setData().withVersion().forPath()
* * version 是通过查询出来的。目的就是为了让其他客户端或者线程不干扰我。
*
* @throws Exception
*/
@Test
public
void
testSetForVersion
()
throws
Exception
{
Stat
status
=
new
Stat
();
//3. 查询节点状态信息:ls -s
client
.
getData
().
storingStatIn
(
status
).
forPath
(
"/app1"
);
int
version
=
status
.
getVersion
();
//查询出来的 3
System
.
out
.
println
(
version
);
client
.
setData
().
withVersion
(
version
).
forPath
(
"/app1"
,
"hehe"
.
getBytes
());
}
}
\ No newline at end of file
src/test/java/com/kwan/shuyu/crud/CuratorTest_10_Delete.java
0 → 100644
浏览文件 @
d1778871
package
com.kwan.shuyu.crud
;
import
org.apache.curator.RetryPolicy
;
import
org.apache.curator.framework.CuratorFramework
;
import
org.apache.curator.framework.CuratorFrameworkFactory
;
import
org.apache.curator.retry.ExponentialBackoffRetry
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
/**
* 删除单个节点
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/15 09:54
*/
public
class
CuratorTest_10_Delete
{
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
();
}
}
/**
* 删除节点: delete deleteall
* 1. 删除单个节点:delete().forPath("/app1");
* 2. 删除带有子节点的节点:delete().deletingChildrenIfNeeded().forPath("/app1");
* 3. 必须成功的删除:为了防止网络抖动。本质就是重试。 client.delete().guaranteed().forPath("/app2");
* 4. 回调:inBackground
*
* @throws Exception
*/
@Test
public
void
testDelete
()
throws
Exception
{
// 1. 删除单个节点
client
.
delete
().
forPath
(
"/app1"
);
}
}
\ No newline at end of file
src/test/java/com/kwan/shuyu/crud/CuratorTest_11_Delete.java
0 → 100644
浏览文件 @
d1778871
package
com.kwan.shuyu.crud
;
import
org.apache.curator.RetryPolicy
;
import
org.apache.curator.framework.CuratorFramework
;
import
org.apache.curator.framework.CuratorFrameworkFactory
;
import
org.apache.curator.retry.ExponentialBackoffRetry
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
/**
* 删除带有子节点的节点
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/15 09:54
*/
public
class
CuratorTest_11_Delete
{
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
();
}
}
/**
* 删除节点: delete deleteall
* 1. 删除单个节点:delete().forPath("/app1");
* 2. 删除带有子节点的节点:delete().deletingChildrenIfNeeded().forPath("/app1");
* 3. 必须成功的删除:为了防止网络抖动。本质就是重试。 client.delete().guaranteed().forPath("/app2");
* 4. 回调:inBackground
*
* @throws Exception
*/
@Test
public
void
testDelete2
()
throws
Exception
{
//2. 删除带有子节点的节点
client
.
delete
().
deletingChildrenIfNeeded
().
forPath
(
"/app4"
);
}
}
\ No newline at end of file
src/test/java/com/kwan/shuyu/crud/CuratorTest_12_Delete.java
0 → 100644
浏览文件 @
d1778871
package
com.kwan.shuyu.crud
;
import
org.apache.curator.RetryPolicy
;
import
org.apache.curator.framework.CuratorFramework
;
import
org.apache.curator.framework.CuratorFrameworkFactory
;
import
org.apache.curator.retry.ExponentialBackoffRetry
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
/**
* 必须成功的删除:为了防止网络抖动。本质就是重试。
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/15 09:54
*/
public
class
CuratorTest_12_Delete
{
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
();
}
}
/**
* 删除节点: delete deleteall
* 1. 删除单个节点:delete().forPath("/app1");
* 2. 删除带有子节点的节点:delete().deletingChildrenIfNeeded().forPath("/app1");
* 3. 必须成功的删除:为了防止网络抖动。本质就是重试。 client.delete().guaranteed().forPath("/app2");
* 4. 回调:inBackground
*
* @throws Exception
*/
@Test
public
void
testDelete3
()
throws
Exception
{
//3. 必须成功的删除
client
.
delete
().
guaranteed
().
forPath
(
"/app2"
);
}
}
\ No newline at end of file
src/test/java/com/kwan/shuyu/
CuratorTest
.java
→
src/test/java/com/kwan/shuyu/
crud/CuratorTest_13_Delete
.java
浏览文件 @
d1778871
package
com.kwan.shuyu
;
package
com.kwan.shuyu
.crud
;
import
org.apache.curator.RetryPolicy
;
import
org.apache.curator.RetryPolicy
;
import
org.apache.curator.framework.CuratorFramework
;
import
org.apache.curator.framework.CuratorFramework
;
...
@@ -6,15 +6,18 @@ import org.apache.curator.framework.CuratorFrameworkFactory;
...
@@ -6,15 +6,18 @@ import org.apache.curator.framework.CuratorFrameworkFactory;
import
org.apache.curator.framework.api.BackgroundCallback
;
import
org.apache.curator.framework.api.BackgroundCallback
;
import
org.apache.curator.framework.api.CuratorEvent
;
import
org.apache.curator.framework.api.CuratorEvent
;
import
org.apache.curator.retry.ExponentialBackoffRetry
;
import
org.apache.curator.retry.ExponentialBackoffRetry
;
import
org.apache.zookeeper.CreateMode
;
import
org.apache.zookeeper.data.Stat
;
import
org.junit.After
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.Test
;
import
java.util.List
;
/**
* 删除回调:inBackground
public
class
CuratorTest
{
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/15 09:54
*/
public
class
CuratorTest_13_Delete
{
private
CuratorFramework
client
;
private
CuratorFramework
client
;
/**
/**
...
@@ -24,7 +27,7 @@ public class CuratorTest {
...
@@ -24,7 +27,7 @@ public class CuratorTest {
public
void
testConnect
()
{
public
void
testConnect
()
{
/*
/*
*
*
* @param connectString 连接字符串。zk server 地址和端口 "1
92.168.149.135:2181,192.168.149.136:2181
"
* @param connectString 连接字符串。zk server 地址和端口 "1
20.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183
"
* @param sessionTimeoutMs 会话超时时间 单位ms
* @param sessionTimeoutMs 会话超时时间 单位ms
* @param connectionTimeoutMs 连接超时时间 单位ms
* @param connectionTimeoutMs 连接超时时间 单位ms
* @param retryPolicy 重试策略
* @param retryPolicy 重试策略
...
@@ -32,14 +35,14 @@ public class CuratorTest {
...
@@ -32,14 +35,14 @@ public class CuratorTest {
/* //重试策略
/* //重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
//1.第一种方式
//1.第一种方式
CuratorFramework client = CuratorFrameworkFactory.newClient("1
92.168.149.135:2181
",
CuratorFramework client = CuratorFrameworkFactory.newClient("1
20.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183
",
60 * 1000, 15 * 1000, retryPolicy);*/
60 * 1000, 15 * 1000, retryPolicy);*/
//重试策略
//重试策略
RetryPolicy
retryPolicy
=
new
ExponentialBackoffRetry
(
3000
,
10
);
RetryPolicy
retryPolicy
=
new
ExponentialBackoffRetry
(
3000
,
10
);
//2.第二种方式
//2.第二种方式
//CuratorFrameworkFactory.builder();
//CuratorFrameworkFactory.builder();
client
=
CuratorFrameworkFactory
.
builder
()
client
=
CuratorFrameworkFactory
.
builder
()
.
connectString
(
"1
92.168.149.135:2181
"
)
.
connectString
(
"1
20.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183
"
)
.
sessionTimeoutMs
(
60
*
1000
)
.
sessionTimeoutMs
(
60
*
1000
)
.
connectionTimeoutMs
(
15
*
1000
)
.
connectionTimeoutMs
(
15
*
1000
)
.
retryPolicy
(
retryPolicy
)
.
retryPolicy
(
retryPolicy
)
...
@@ -48,101 +51,13 @@ public class CuratorTest {
...
@@ -48,101 +51,13 @@ public class CuratorTest {
//开启连接
//开启连接
client
.
start
();
client
.
start
();
}
}
//==============================create=============================================================================
/**
* 创建节点:create 持久 临时 顺序 数据
* 1. 基本创建 :create().forPath("")
* 2. 创建节点 带有数据:create().forPath("",data)
* 3. 设置节点的类型:create().withMode().forPath("",data)
* 4. 创建多级节点 /app1/p1 :create().creatingParentsIfNeeded().forPath("",data)
*/
@Test
public
void
testCreate
()
throws
Exception
{
//2. 创建节点 带有数据
//如果创建节点,没有指定数据,则默认将当前客户端的ip作为数据存储
String
path
=
client
.
create
().
forPath
(
"/app2"
,
"hehe"
.
getBytes
());
System
.
out
.
println
(
path
);
}
@Test
public
void
testCreate2
()
throws
Exception
{
//1. 基本创建
//如果创建节点,没有指定数据,则默认将当前客户端的ip作为数据存储
String
path
=
client
.
create
().
forPath
(
"/app1"
);
System
.
out
.
println
(
path
);
}
@Test
public
void
testCreate3
()
throws
Exception
{
//3. 设置节点的类型
//默认类型:持久化
String
path
=
client
.
create
().
withMode
(
CreateMode
.
EPHEMERAL
).
forPath
(
"/app3"
);
System
.
out
.
println
(
path
);
}
@Test
public
void
testCreate4
()
throws
Exception
{
//4. 创建多级节点 /app1/p1
//creatingParentsIfNeeded():如果父节点不存在,则创建父节点
String
path
=
client
.
create
().
creatingParentsIfNeeded
().
forPath
(
"/app4/p1"
);
System
.
out
.
println
(
path
);
}
//===========================get================================================================================
/**
* 查询节点:
* 1. 查询数据:get: getData().forPath()
* 2. 查询子节点: ls: getChildren().forPath()
* 3. 查询节点状态信息:ls -s:getData().storingStatIn(状态对象).forPath()
*/
@Test
public
void
testGet1
()
throws
Exception
{
//1. 查询数据:get
byte
[]
data
=
client
.
getData
().
forPath
(
"/app1"
);
System
.
out
.
println
(
new
String
(
data
));
}
@Test
public
void
testGet2
()
throws
Exception
{
// 2. 查询子节点: ls
List
<
String
>
path
=
client
.
getChildren
().
forPath
(
"/"
);
System
.
out
.
println
(
path
);
}
@Test
public
void
testGet3
()
throws
Exception
{
Stat
status
=
new
Stat
();
System
.
out
.
println
(
status
);
//3. 查询节点状态信息:ls -s
client
.
getData
().
storingStatIn
(
status
).
forPath
(
"/app1"
);
System
.
out
.
println
(
status
);
}
//===========================set================================================================================
/**
@After
* 修改数据
public
void
close
()
{
* 1. 基本修改数据:setData().forPath()
if
(
client
!=
null
)
{
* 2. 根据版本修改: setData().withVersion().forPath()
client
.
close
();
* * version 是通过查询出来的。目的就是为了让其他客户端或者线程不干扰我。
*
* @throws Exception
*/
@Test
public
void
testSet
()
throws
Exception
{
client
.
setData
().
forPath
(
"/app1"
,
"itcast"
.
getBytes
());
}
}
@Test
public
void
testSetForVersion
()
throws
Exception
{
Stat
status
=
new
Stat
();
//3. 查询节点状态信息:ls -s
client
.
getData
().
storingStatIn
(
status
).
forPath
(
"/app1"
);
int
version
=
status
.
getVersion
();
//查询出来的 3
System
.
out
.
println
(
version
);
client
.
setData
().
withVersion
(
version
).
forPath
(
"/app1"
,
"hehe"
.
getBytes
());
}
}
//===========================delete================================================================================
/**
/**
* 删除节点: delete deleteall
* 删除节点: delete deleteall
...
@@ -153,24 +68,6 @@ public class CuratorTest {
...
@@ -153,24 +68,6 @@ public class CuratorTest {
*
*
* @throws Exception
* @throws Exception
*/
*/
@Test
public
void
testDelete
()
throws
Exception
{
// 1. 删除单个节点
client
.
delete
().
forPath
(
"/app1"
);
}
@Test
public
void
testDelete2
()
throws
Exception
{
//2. 删除带有子节点的节点
client
.
delete
().
deletingChildrenIfNeeded
().
forPath
(
"/app4"
);
}
@Test
public
void
testDelete3
()
throws
Exception
{
//3. 必须成功的删除
client
.
delete
().
guaranteed
().
forPath
(
"/app2"
);
}
@Test
@Test
public
void
testDelete4
()
throws
Exception
{
public
void
testDelete4
()
throws
Exception
{
//4. 回调
//4. 回调
...
@@ -182,11 +79,4 @@ public class CuratorTest {
...
@@ -182,11 +79,4 @@ public class CuratorTest {
}
}
}).
forPath
(
"/app1"
);
}).
forPath
(
"/app1"
);
}
}
@After
public
void
close
()
{
if
(
client
!=
null
)
{
client
.
close
();
}
}
}
}
\ No newline at end of file
src/test/java/com/kwan/shuyu/CuratorWatcherTest.java
→
src/test/java/com/kwan/shuyu/
watch/
CuratorWatcherTest.java
浏览文件 @
d1778871
package
com.kwan.shuyu
;
package
com.kwan.shuyu
.watch
;
import
org.apache.curator.RetryPolicy
;
import
org.apache.curator.RetryPolicy
;
import
org.apache.curator.framework.CuratorFramework
;
import
org.apache.curator.framework.CuratorFramework
;
...
@@ -19,7 +19,7 @@ public class CuratorWatcherTest {
...
@@ -19,7 +19,7 @@ public class CuratorWatcherTest {
public
void
testConnect
()
{
public
void
testConnect
()
{
/*
/*
*
*
* @param connectString 连接字符串。zk server 地址和端口 "1
92.168.149.135:2181,192.168.149.136:2181
"
* @param connectString 连接字符串。zk server 地址和端口 "1
20.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183
"
* @param sessionTimeoutMs 会话超时时间 单位ms
* @param sessionTimeoutMs 会话超时时间 单位ms
* @param connectionTimeoutMs 连接超时时间 单位ms
* @param connectionTimeoutMs 连接超时时间 单位ms
* @param retryPolicy 重试策略
* @param retryPolicy 重试策略
...
@@ -27,14 +27,14 @@ public class CuratorWatcherTest {
...
@@ -27,14 +27,14 @@ public class CuratorWatcherTest {
/* //重试策略
/* //重试策略
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
//1.第一种方式
//1.第一种方式
CuratorFramework client = CuratorFrameworkFactory.newClient("1
92.168.149.135:2181
",
CuratorFramework client = CuratorFrameworkFactory.newClient("1
20.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183
",
60 * 1000, 15 * 1000, retryPolicy);*/
60 * 1000, 15 * 1000, retryPolicy);*/
//重试策略
//重试策略
RetryPolicy
retryPolicy
=
new
ExponentialBackoffRetry
(
3000
,
10
);
RetryPolicy
retryPolicy
=
new
ExponentialBackoffRetry
(
3000
,
10
);
//2.第二种方式
//2.第二种方式
//CuratorFrameworkFactory.builder();
//CuratorFrameworkFactory.builder();
client
=
CuratorFrameworkFactory
.
builder
()
client
=
CuratorFrameworkFactory
.
builder
()
.
connectString
(
"1
92.168.149.135:2181
"
)
.
connectString
(
"1
20.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183
"
)
.
sessionTimeoutMs
(
60
*
1000
)
.
sessionTimeoutMs
(
60
*
1000
)
.
connectionTimeoutMs
(
15
*
1000
)
.
connectionTimeoutMs
(
15
*
1000
)
.
retryPolicy
(
retryPolicy
)
.
retryPolicy
(
retryPolicy
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录