diff --git a/pom.xml b/pom.xml
index b1458fd5719a2ed131cb123e1dabe61365d3781b..e6a7ffcf3af7e45b7bef6721e8bb372137cdc2fb 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 0000000000000000000000000000000000000000..6d412b72165b33dfed8f35c07ec101488b858bae
--- /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 0000000000000000000000000000000000000000..7574a4611a0a6fb43e28c239cb64e6e2b47655b6
--- /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 01c9426aa6e58bebe82b2304a541e103c9955ef4..468a06bd0a16e2a9747c26b85a2504532a02416d 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 0000000000000000000000000000000000000000..b4ec8472d6043aec2730e5aa06074287dfd501bf
--- /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 f380fa2196d4572fd1aa417edde73778242707b5..0000000000000000000000000000000000000000
--- 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 ffb3f11b33c561d3e258084e97bb25796a3c7469..b01ff73f337364979933c425ab71b9fc624787c2 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 fe864968bab9165cd8d4f98405eff592fbb538ae..5e1fa8cb31de58ee8a949419ab96893fcc3ba5d4 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 87da30c77a937769ff5d56c804928ef8b0b014fa..9c37155410460cb5e96c6567b0ca7c1e33b36ea3 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 0ba1d272f4c9d5be89a823c0eb58968ac0cdbd9e..cce42724636e0149af06866d102b86daaaa980ad 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 709bf3197ca66ebe57ee3b8c9540a62f5fca9bff..5908e9e322328b90c65a062049fb9e696449afc4 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 d4d22a288d8cc6a0c13d2be4b621460df39bdfdd..82019c2460ed67561ff9b4bb04f4d1fa4ba52c48 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 0ec88501ff2d7a894886e67f2ec9608e11df9cd7..dc75673cbce99cd28baed3fe54fbf4556b9e5ba1 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 58695243c2638a5e99b9eb9a0a25ec2c0d875e63..5d2b444499d6f65c1f82941bb57c064fc49e33c6 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 06f2da7c7ae23beaa5d899e44d5073e0b6c7bfde..278301b1c934d4c4b12daae60cc1c5a72fb6392a 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 802f007b536eb8886603282799ae6a15e28f8d2b..a004975a1951f023d45d35f8c18935470dabab9c 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 5cd872819dd9b0bc44af498b1b02a1f74cf41e28..15ae895efff4105fd4b276ce53f97404f08c4e0a 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 59fa5ffcb34227b1cade42e2829c00ec13839040..44693ea65279c5950029b4048828d0ddf3c19f76 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 cbcece0f0d1e72da13b16da605106516530f0091..ad063b27d9a760293ce96871d2d2c3e803513695 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 6fbb27f41ec54423dce0f8c840397dd6cb431c3f..85aaf1a3c260b3625aa319c689b80a82f881dd61 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 749a428c83f6991d7f0df7f2daf6c84927f63b84..357f997aaeddc4aeb41068b31d889ee224e8e4f7 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 94bb9f874f8b0bdc23e341256ec60c7f2de88502..276b071b173d22aa4aa0e766f9c5615e84210341 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 ca89a429114e818ab10f8cbee2cbd5bdb5a0976e..573b213afbc41f07f8a4ab85719a8e880d7d3e19 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:监听某个节点自己和所有子节点们
*/