From bd3706d53478514f1e3722c7670ecf41c669ddaa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com>
Date: Mon, 15 May 2023 11:05:05 +0800
Subject: [PATCH] =?UTF-8?q?fix:=E4=BD=BF=E7=94=A8springboot=E6=9D=A5?=
=?UTF-8?q?=E5=B0=81=E8=A3=85?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 34 ++++--------
.../com/kwan/shuyu/ZookeeperApplication.java | 11 ++++
.../kwan/shuyu/config/ZookeeperConfig.java | 53 +++++++++++++++++++
.../kwan/shuyu/{ => entry}/Ticket12306.java | 2 +-
src/main/resources/application.yml | 9 ++++
src/main/resources/log4j.properties | 6 ---
.../com/kwan/shuyu/Ticket12306LockTest.java | 2 +
.../shuyu/crud/CuratorTest_01_Create.java | 1 -
.../shuyu/crud/CuratorTest_02_Create.java | 53 +++----------------
.../shuyu/crud/CuratorTest_03_Create.java | 46 +---------------
.../shuyu/crud/CuratorTest_04_Create.java | 48 +----------------
.../kwan/shuyu/crud/CuratorTest_05_Get.java | 46 +---------------
.../kwan/shuyu/crud/CuratorTest_06_Get.java | 47 ++--------------
.../kwan/shuyu/crud/CuratorTest_07_Get.java | 47 ++--------------
.../kwan/shuyu/crud/CuratorTest_08_Set.java | 47 ++--------------
.../kwan/shuyu/crud/CuratorTest_09_Set.java | 46 ++--------------
.../shuyu/crud/CuratorTest_10_Delete.java | 47 ++--------------
.../shuyu/crud/CuratorTest_11_Delete.java | 47 ++--------------
.../shuyu/crud/CuratorTest_12_Delete.java | 47 ++--------------
.../shuyu/crud/CuratorTest_13_Delete.java | 47 ++--------------
.../shuyu/watch/Watcher_01_NodeCache.java | 49 ++---------------
.../watch/Watcher_02_PathChildrenCache.java | 50 ++---------------
.../shuyu/watch/Watcher_03_TreeCache.java | 50 ++---------------
23 files changed, 139 insertions(+), 696 deletions(-)
create mode 100644 src/main/java/com/kwan/shuyu/ZookeeperApplication.java
create mode 100644 src/main/java/com/kwan/shuyu/config/ZookeeperConfig.java
rename src/main/java/com/kwan/shuyu/{ => entry}/Ticket12306.java (98%)
create mode 100644 src/main/resources/application.yml
delete mode 100644 src/main/resources/log4j.properties
diff --git a/pom.xml b/pom.xml
index b1458fd..e6a7ffc 100644
--- a/pom.xml
+++ b/pom.xml
@@ -11,6 +11,17 @@
8
+
+ org.springframework.boot
+ spring-boot-starter
+ 2.0.6.RELEASE
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+ 2.0.6.RELEASE
+
org.apache.commons
commons-lang3
@@ -21,18 +32,6 @@
fastjson
1.2.83
-
- org.jsoup
- jsoup
- 1.15.3
-
-
- junit
- junit
- 4.10
- test
-
-
org.apache.curator
curator-framework
@@ -43,17 +42,6 @@
curator-recipes
4.0.0
-
-
- org.slf4j
- slf4j-api
- 1.7.21
-
-
- org.slf4j
- slf4j-log4j12
- 1.7.21
-
diff --git a/src/main/java/com/kwan/shuyu/ZookeeperApplication.java b/src/main/java/com/kwan/shuyu/ZookeeperApplication.java
new file mode 100644
index 0000000..6d412b7
--- /dev/null
+++ b/src/main/java/com/kwan/shuyu/ZookeeperApplication.java
@@ -0,0 +1,11 @@
+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);
+ }
+}
diff --git a/src/main/java/com/kwan/shuyu/config/ZookeeperConfig.java b/src/main/java/com/kwan/shuyu/config/ZookeeperConfig.java
new file mode 100644
index 0000000..7574a46
--- /dev/null
+++ b/src/main/java/com/kwan/shuyu/config/ZookeeperConfig.java
@@ -0,0 +1,53 @@
+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
diff --git a/src/main/java/com/kwan/shuyu/Ticket12306.java b/src/main/java/com/kwan/shuyu/entry/Ticket12306.java
similarity index 98%
rename from src/main/java/com/kwan/shuyu/Ticket12306.java
rename to src/main/java/com/kwan/shuyu/entry/Ticket12306.java
index 01c9426..468a06b 100644
--- a/src/main/java/com/kwan/shuyu/Ticket12306.java
+++ b/src/main/java/com/kwan/shuyu/entry/Ticket12306.java
@@ -1,4 +1,4 @@
-package com.kwan.shuyu;
+package com.kwan.shuyu.entry;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
new file mode 100644
index 0000000..b4ec847
--- /dev/null
+++ b/src/main/resources/application.yml
@@ -0,0 +1,9 @@
+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
diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties
deleted file mode 100644
index f380fa2..0000000
--- a/src/main/resources/log4j.properties
+++ /dev/null
@@ -1,6 +0,0 @@
-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
diff --git a/src/test/java/com/kwan/shuyu/Ticket12306LockTest.java b/src/test/java/com/kwan/shuyu/Ticket12306LockTest.java
index ffb3f11..b01ff73 100644
--- a/src/test/java/com/kwan/shuyu/Ticket12306LockTest.java
+++ b/src/test/java/com/kwan/shuyu/Ticket12306LockTest.java
@@ -1,6 +1,8 @@
package com.kwan.shuyu;
+import com.kwan.shuyu.entry.Ticket12306;
+
/**
* InterProcessMutex 分布式锁测试
*
diff --git a/src/test/java/com/kwan/shuyu/crud/CuratorTest_01_Create.java b/src/test/java/com/kwan/shuyu/crud/CuratorTest_01_Create.java
index fe86496..5e1fa8c 100644
--- a/src/test/java/com/kwan/shuyu/crud/CuratorTest_01_Create.java
+++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_01_Create.java
@@ -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)
diff --git a/src/test/java/com/kwan/shuyu/crud/CuratorTest_02_Create.java b/src/test/java/com/kwan/shuyu/crud/CuratorTest_02_Create.java
index 87da30c..9c37155 100644
--- a/src/test/java/com/kwan/shuyu/crud/CuratorTest_02_Create.java
+++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_02_Create.java
@@ -1,12 +1,11 @@
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("/app1");
+ String path = client.create().forPath("/app5");
System.out.println(path);
}
}
\ No newline at end of file
diff --git a/src/test/java/com/kwan/shuyu/crud/CuratorTest_03_Create.java b/src/test/java/com/kwan/shuyu/crud/CuratorTest_03_Create.java
index 0ba1d27..cce4272 100644
--- a/src/test/java/com/kwan/shuyu/crud/CuratorTest_03_Create.java
+++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_03_Create.java
@@ -1,13 +1,9 @@
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("")
diff --git a/src/test/java/com/kwan/shuyu/crud/CuratorTest_04_Create.java b/src/test/java/com/kwan/shuyu/crud/CuratorTest_04_Create.java
index 709bf31..5908e9e 100644
--- a/src/test/java/com/kwan/shuyu/crud/CuratorTest_04_Create.java
+++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_04_Create.java
@@ -1,12 +1,8 @@
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("")
diff --git a/src/test/java/com/kwan/shuyu/crud/CuratorTest_05_Get.java b/src/test/java/com/kwan/shuyu/crud/CuratorTest_05_Get.java
index d4d22a2..82019c2 100644
--- a/src/test/java/com/kwan/shuyu/crud/CuratorTest_05_Get.java
+++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_05_Get.java
@@ -1,12 +1,8 @@
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()
diff --git a/src/test/java/com/kwan/shuyu/crud/CuratorTest_06_Get.java b/src/test/java/com/kwan/shuyu/crud/CuratorTest_06_Get.java
index 0ec8850..dc75673 100644
--- a/src/test/java/com/kwan/shuyu/crud/CuratorTest_06_Get.java
+++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_06_Get.java
@@ -1,12 +1,8 @@
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;
/**
* 查询节点:
diff --git a/src/test/java/com/kwan/shuyu/crud/CuratorTest_07_Get.java b/src/test/java/com/kwan/shuyu/crud/CuratorTest_07_Get.java
index 5869524..5d2b444 100644
--- a/src/test/java/com/kwan/shuyu/crud/CuratorTest_07_Get.java
+++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_07_Get.java
@@ -1,13 +1,9 @@
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;
/**
* 查询节点:
diff --git a/src/test/java/com/kwan/shuyu/crud/CuratorTest_08_Set.java b/src/test/java/com/kwan/shuyu/crud/CuratorTest_08_Set.java
index 06f2da7..278301b 100644
--- a/src/test/java/com/kwan/shuyu/crud/CuratorTest_08_Set.java
+++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_08_Set.java
@@ -1,12 +1,8 @@
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;
/**
* 修改数据
diff --git a/src/test/java/com/kwan/shuyu/crud/CuratorTest_09_Set.java b/src/test/java/com/kwan/shuyu/crud/CuratorTest_09_Set.java
index 802f007..a004975 100644
--- a/src/test/java/com/kwan/shuyu/crud/CuratorTest_09_Set.java
+++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_09_Set.java
@@ -1,13 +1,9 @@
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();
- }
- }
/**
* 修改数据
diff --git a/src/test/java/com/kwan/shuyu/crud/CuratorTest_10_Delete.java b/src/test/java/com/kwan/shuyu/crud/CuratorTest_10_Delete.java
index 5cd8728..15ae895 100644
--- a/src/test/java/com/kwan/shuyu/crud/CuratorTest_10_Delete.java
+++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_10_Delete.java
@@ -1,12 +1,8 @@
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
diff --git a/src/test/java/com/kwan/shuyu/crud/CuratorTest_11_Delete.java b/src/test/java/com/kwan/shuyu/crud/CuratorTest_11_Delete.java
index 59fa5ff..44693ea 100644
--- a/src/test/java/com/kwan/shuyu/crud/CuratorTest_11_Delete.java
+++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_11_Delete.java
@@ -1,12 +1,8 @@
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
diff --git a/src/test/java/com/kwan/shuyu/crud/CuratorTest_12_Delete.java b/src/test/java/com/kwan/shuyu/crud/CuratorTest_12_Delete.java
index cbcece0..ad063b2 100644
--- a/src/test/java/com/kwan/shuyu/crud/CuratorTest_12_Delete.java
+++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_12_Delete.java
@@ -1,12 +1,8 @@
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
diff --git a/src/test/java/com/kwan/shuyu/crud/CuratorTest_13_Delete.java b/src/test/java/com/kwan/shuyu/crud/CuratorTest_13_Delete.java
index 6fbb27f..85aaf1a 100644
--- a/src/test/java/com/kwan/shuyu/crud/CuratorTest_13_Delete.java
+++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_13_Delete.java
@@ -1,14 +1,10 @@
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
diff --git a/src/test/java/com/kwan/shuyu/watch/Watcher_01_NodeCache.java b/src/test/java/com/kwan/shuyu/watch/Watcher_01_NodeCache.java
index 749a428..357f997 100644
--- a/src/test/java/com/kwan/shuyu/watch/Watcher_01_NodeCache.java
+++ b/src/test/java/com/kwan/shuyu/watch/Watcher_01_NodeCache.java
@@ -1,13 +1,10 @@
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:给指定一个节点注册监听器
*/
diff --git a/src/test/java/com/kwan/shuyu/watch/Watcher_02_PathChildrenCache.java b/src/test/java/com/kwan/shuyu/watch/Watcher_02_PathChildrenCache.java
index 94bb9f8..276b071 100644
--- a/src/test/java/com/kwan/shuyu/watch/Watcher_02_PathChildrenCache.java
+++ b/src/test/java/com/kwan/shuyu/watch/Watcher_02_PathChildrenCache.java
@@ -1,13 +1,11 @@
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:监听某个节点的所有子节点们
*/
diff --git a/src/test/java/com/kwan/shuyu/watch/Watcher_03_TreeCache.java b/src/test/java/com/kwan/shuyu/watch/Watcher_03_TreeCache.java
index ca89a42..573b213 100644
--- a/src/test/java/com/kwan/shuyu/watch/Watcher_03_TreeCache.java
+++ b/src/test/java/com/kwan/shuyu/watch/Watcher_03_TreeCache.java
@@ -1,13 +1,11 @@
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:监听某个节点自己和所有子节点们
*/
--
GitLab