Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Kwan的解忧杂货铺@新空间代码工作室
zookeeper-demo
提交
bd3706d5
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看板
提交
bd3706d5
编写于
5月 15, 2023
作者:
Kwan的解忧杂货铺@新空间代码工作室
🐭
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:使用springboot来封装
上级
d4e41daf
变更
23
隐藏空白更改
内联
并排
Showing
23 changed file
with
139 addition
and
696 deletion
+139
-696
pom.xml
pom.xml
+11
-23
src/main/java/com/kwan/shuyu/ZookeeperApplication.java
src/main/java/com/kwan/shuyu/ZookeeperApplication.java
+11
-0
src/main/java/com/kwan/shuyu/config/ZookeeperConfig.java
src/main/java/com/kwan/shuyu/config/ZookeeperConfig.java
+53
-0
src/main/java/com/kwan/shuyu/entry/Ticket12306.java
src/main/java/com/kwan/shuyu/entry/Ticket12306.java
+1
-1
src/main/resources/application.yml
src/main/resources/application.yml
+9
-0
src/main/resources/log4j.properties
src/main/resources/log4j.properties
+0
-6
src/test/java/com/kwan/shuyu/Ticket12306LockTest.java
src/test/java/com/kwan/shuyu/Ticket12306LockTest.java
+2
-0
src/test/java/com/kwan/shuyu/crud/CuratorTest_01_Create.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_01_Create.java
+0
-1
src/test/java/com/kwan/shuyu/crud/CuratorTest_02_Create.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_02_Create.java
+8
-45
src/test/java/com/kwan/shuyu/crud/CuratorTest_03_Create.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_03_Create.java
+2
-44
src/test/java/com/kwan/shuyu/crud/CuratorTest_04_Create.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_04_Create.java
+2
-46
src/test/java/com/kwan/shuyu/crud/CuratorTest_05_Get.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_05_Get.java
+2
-44
src/test/java/com/kwan/shuyu/crud/CuratorTest_06_Get.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_06_Get.java
+3
-44
src/test/java/com/kwan/shuyu/crud/CuratorTest_07_Get.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_07_Get.java
+3
-44
src/test/java/com/kwan/shuyu/crud/CuratorTest_08_Set.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_08_Set.java
+3
-44
src/test/java/com/kwan/shuyu/crud/CuratorTest_09_Set.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_09_Set.java
+3
-43
src/test/java/com/kwan/shuyu/crud/CuratorTest_10_Delete.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_10_Delete.java
+3
-44
src/test/java/com/kwan/shuyu/crud/CuratorTest_11_Delete.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_11_Delete.java
+3
-44
src/test/java/com/kwan/shuyu/crud/CuratorTest_12_Delete.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_12_Delete.java
+3
-44
src/test/java/com/kwan/shuyu/crud/CuratorTest_13_Delete.java
src/test/java/com/kwan/shuyu/crud/CuratorTest_13_Delete.java
+3
-44
src/test/java/com/kwan/shuyu/watch/Watcher_01_NodeCache.java
src/test/java/com/kwan/shuyu/watch/Watcher_01_NodeCache.java
+4
-45
src/test/java/com/kwan/shuyu/watch/Watcher_02_PathChildrenCache.java
...va/com/kwan/shuyu/watch/Watcher_02_PathChildrenCache.java
+5
-45
src/test/java/com/kwan/shuyu/watch/Watcher_03_TreeCache.java
src/test/java/com/kwan/shuyu/watch/Watcher_03_TreeCache.java
+5
-45
未找到文件。
pom.xml
浏览文件 @
bd3706d5
...
...
@@ -11,6 +11,17 @@
<maven.compiler.target>
8
</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter
</artifactId>
<version>
2.0.6.RELEASE
</version>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>
test
</scope>
<version>
2.0.6.RELEASE
</version>
</dependency>
<dependency>
<groupId>
org.apache.commons
</groupId>
<artifactId>
commons-lang3
</artifactId>
...
...
@@ -21,18 +32,6 @@
<artifactId>
fastjson
</artifactId>
<version>
1.2.83
</version>
</dependency>
<dependency>
<groupId>
org.jsoup
</groupId>
<artifactId>
jsoup
</artifactId>
<version>
1.15.3
</version>
</dependency>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
4.10
</version>
<scope>
test
</scope>
</dependency>
<!--curator-->
<dependency>
<groupId>
org.apache.curator
</groupId>
<artifactId>
curator-framework
</artifactId>
...
...
@@ -43,17 +42,6 @@
<artifactId>
curator-recipes
</artifactId>
<version>
4.0.0
</version>
</dependency>
<!--日志-->
<dependency>
<groupId>
org.slf4j
</groupId>
<artifactId>
slf4j-api
</artifactId>
<version>
1.7.21
</version>
</dependency>
<dependency>
<groupId>
org.slf4j
</groupId>
<artifactId>
slf4j-log4j12
</artifactId>
<version>
1.7.21
</version>
</dependency>
</dependencies>
<build>
<plugins>
...
...
src/main/java/com/kwan/shuyu/ZookeeperApplication.java
0 → 100644
浏览文件 @
bd3706d5
package
com.kwan.shuyu
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
@SpringBootApplication
public
class
ZookeeperApplication
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
ZookeeperApplication
.
class
,
args
);
}
}
src/main/java/com/kwan/shuyu/config/ZookeeperConfig.java
0 → 100644
浏览文件 @
bd3706d5
package
com.kwan.shuyu.config
;
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.springframework.beans.factory.annotation.Value
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
/**
* zookeeper的配置类
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/5/10 12:37
*/
@Configuration
public
class
ZookeeperConfig
{
@Value
(
"${kwan.zookeeper.hostlist}"
)
private
String
hostlist
;
@Bean
(
destroyMethod
=
"close"
)
public
CuratorFramework
connect
()
{
/*
*
* @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.第二种方式
CuratorFramework
client
=
CuratorFrameworkFactory
.
builder
()
.
connectString
(
hostlist
)
.
sessionTimeoutMs
(
60
*
1000
)
.
connectionTimeoutMs
(
15
*
1000
)
.
retryPolicy
(
retryPolicy
)
.
namespace
(
"itheima"
)
.
build
();
//开启连接
client
.
start
();
return
client
;
}
}
\ No newline at end of file
src/main/java/com/kwan/shuyu/Ticket12306.java
→
src/main/java/com/kwan/shuyu/
entry/
Ticket12306.java
浏览文件 @
bd3706d5
package
com.kwan.shuyu
;
package
com.kwan.shuyu
.entry
;
import
org.apache.curator.RetryPolicy
;
import
org.apache.curator.framework.CuratorFramework
;
...
...
src/main/resources/application.yml
0 → 100644
浏览文件 @
bd3706d5
server
:
port
:
8888
spring
:
application
:
name
:
zookeeper-service
kwan
:
zookeeper
:
hostlist
:
120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183
#多个节点用逗号分隔
\ No newline at end of file
src/main/resources/log4j.properties
已删除
100644 → 0
浏览文件 @
d4e41daf
log4j.rootLogger
=
off,stdout
log4j.appender.stdout
=
org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target
=
System.out
log4j.appender.stdout.layout
=
org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern
=
[%d{yyyy-MM-dd HH/:mm/:ss}]%-5p %c(line/:%L) %x-%m%n
\ No newline at end of file
src/test/java/com/kwan/shuyu/Ticket12306LockTest.java
浏览文件 @
bd3706d5
package
com.kwan.shuyu
;
import
com.kwan.shuyu.entry.Ticket12306
;
/**
* InterProcessMutex 分布式锁测试
*
...
...
src/test/java/com/kwan/shuyu/crud/CuratorTest_01_Create.java
浏览文件 @
bd3706d5
...
...
@@ -39,7 +39,6 @@ public class CuratorTest_01_Create {
//重试策略
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
)
...
...
src/test/java/com/kwan/shuyu/crud/CuratorTest_02_Create.java
浏览文件 @
bd3706d5
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
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringRunner
;
/**
* 创建节点 不带数据
...
...
@@ -15,49 +14,13 @@ import org.junit.Test;
* @version : 2.2.0
* @date : 2023/5/15 09:54
*/
@SpringBootTest
@RunWith
(
SpringRunner
.
class
)
public
class
CuratorTest_02_Create
{
@Autowired
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("")
...
...
@@ -69,7 +32,7 @@ public class CuratorTest_02_Create {
public
void
testCreate2
()
throws
Exception
{
//基本创建
//如果创建节点,没有指定数据,则默认将当前客户端的ip作为数据存储
String
path
=
client
.
create
().
forPath
(
"/app
1
"
);
String
path
=
client
.
create
().
forPath
(
"/app
5
"
);
System
.
out
.
println
(
path
);
}
}
\ No newline at end of file
src/test/java/com/kwan/shuyu/crud/CuratorTest_03_Create.java
浏览文件 @
bd3706d5
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
;
import
org.springframework.beans.factory.annotation.Autowired
;
/**
* 设置节点的类型
...
...
@@ -18,47 +14,9 @@ import org.junit.Test;
*/
public
class
CuratorTest_03_Create
{
@Autowired
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("")
...
...
src/test/java/com/kwan/shuyu/crud/CuratorTest_04_Create.java
浏览文件 @
bd3706d5
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
org.springframework.beans.factory.annotation.Autowired
;
/**
* 创建多级节点
...
...
@@ -17,49 +13,9 @@ import org.junit.Test;
*/
public
class
CuratorTest_04_Create
{
@Autowired
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("")
...
...
src/test/java/com/kwan/shuyu/crud/CuratorTest_05_Get.java
浏览文件 @
bd3706d5
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
org.springframework.beans.factory.annotation.Autowired
;
/**
* 查询数据
...
...
@@ -17,47 +13,9 @@ import org.junit.Test;
*/
public
class
CuratorTest_05_Get
{
@Autowired
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()
...
...
src/test/java/com/kwan/shuyu/crud/CuratorTest_06_Get.java
浏览文件 @
bd3706d5
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
org.springframework.beans.factory.annotation.Autowired
;
import
java.util.List
;
...
...
@@ -18,46 +14,9 @@ import java.util.List;
* @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
();
}
}
@Autowired
private
CuratorFramework
client
;
/**
* 查询节点:
...
...
src/test/java/com/kwan/shuyu/crud/CuratorTest_07_Get.java
浏览文件 @
bd3706d5
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
;
import
org.springframework.beans.factory.annotation.Autowired
;
/**
* 查询节点状态信息
...
...
@@ -17,46 +13,9 @@ import org.junit.Test;
* @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
();
}
}
@Autowired
private
CuratorFramework
client
;
/**
* 查询节点:
...
...
src/test/java/com/kwan/shuyu/crud/CuratorTest_08_Set.java
浏览文件 @
bd3706d5
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
org.springframework.beans.factory.annotation.Autowired
;
/**
* 基本修改数据
...
...
@@ -16,46 +12,9 @@ import org.junit.Test;
* @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
();
}
}
@Autowired
private
CuratorFramework
client
;
/**
* 修改数据
...
...
src/test/java/com/kwan/shuyu/crud/CuratorTest_09_Set.java
浏览文件 @
bd3706d5
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
;
import
org.springframework.beans.factory.annotation.Autowired
;
/**
* 根据版本修改
...
...
@@ -17,46 +13,10 @@ import org.junit.Test;
* @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
();
}
@Autowired
private
CuratorFramework
client
;
@After
public
void
close
()
{
if
(
client
!=
null
)
{
client
.
close
();
}
}
/**
* 修改数据
...
...
src/test/java/com/kwan/shuyu/crud/CuratorTest_10_Delete.java
浏览文件 @
bd3706d5
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
org.springframework.beans.factory.annotation.Autowired
;
/**
* 删除单个节点
...
...
@@ -16,46 +12,9 @@ import org.junit.Test;
* @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
();
}
}
@Autowired
private
CuratorFramework
client
;
/**
* 删除节点: delete deleteall
...
...
src/test/java/com/kwan/shuyu/crud/CuratorTest_11_Delete.java
浏览文件 @
bd3706d5
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
org.springframework.beans.factory.annotation.Autowired
;
/**
* 删除带有子节点的节点
...
...
@@ -16,46 +12,9 @@ import org.junit.Test;
* @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
();
}
}
@Autowired
private
CuratorFramework
client
;
/**
* 删除节点: delete deleteall
...
...
src/test/java/com/kwan/shuyu/crud/CuratorTest_12_Delete.java
浏览文件 @
bd3706d5
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
org.springframework.beans.factory.annotation.Autowired
;
/**
* 必须成功的删除:为了防止网络抖动。本质就是重试。
...
...
@@ -16,46 +12,9 @@ import org.junit.Test;
* @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
();
}
}
@Autowired
private
CuratorFramework
client
;
/**
* 删除节点: delete deleteall
...
...
src/test/java/com/kwan/shuyu/crud/CuratorTest_13_Delete.java
浏览文件 @
bd3706d5
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.framework.api.BackgroundCallback
;
import
org.apache.curator.framework.api.CuratorEvent
;
import
org.apache.curator.retry.ExponentialBackoffRetry
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
/**
* 删除回调:inBackground
...
...
@@ -18,46 +14,9 @@ import org.junit.Test;
* @date : 2023/5/15 09:54
*/
public
class
CuratorTest_13_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
();
}
}
@Autowired
private
CuratorFramework
client
;
/**
* 删除节点: delete deleteall
...
...
src/test/java/com/kwan/shuyu/watch/Watcher_01_NodeCache.java
浏览文件 @
bd3706d5
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.apache.curator.framework.recipes.cache.NodeCache
;
import
org.apache.curator.framework.recipes.cache.NodeCacheListener
;
import
org.junit.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
/**
* Watcher监听
...
...
@@ -18,47 +15,9 @@ import org.junit.Test;
*/
public
class
Watcher_01_NodeCache
{
@Autowired
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:给指定一个节点注册监听器
*/
...
...
src/test/java/com/kwan/shuyu/watch/Watcher_02_PathChildrenCache.java
浏览文件 @
bd3706d5
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.apache.curator.framework.recipes.cache.PathChildrenCache
;
import
org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent
;
import
org.apache.curator.framework.recipes.cache.PathChildrenCacheListener
;
import
org.junit.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
/**
* Watcher监听
...
...
@@ -18,47 +16,9 @@ import org.junit.Test;
*/
public
class
Watcher_02_PathChildrenCache
{
@Autowired
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
();
}
}
/**
* 演示 PathChildrenCache:监听某个节点的所有子节点们
*/
...
...
src/test/java/com/kwan/shuyu/watch/Watcher_03_TreeCache.java
浏览文件 @
bd3706d5
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.apache.curator.framework.recipes.cache.TreeCache
;
import
org.apache.curator.framework.recipes.cache.TreeCacheEvent
;
import
org.apache.curator.framework.recipes.cache.TreeCacheListener
;
import
org.junit.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
/**
* Watcher监听
...
...
@@ -18,47 +16,9 @@ import org.junit.Test;
*/
public
class
Watcher_03_TreeCache
{
@Autowired
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:监听某个节点自己和所有子节点们
*/
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录