From d1778871b4f99994153e4752fff4446bfd58c121 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 10:29:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:zookeeper=20=20Curator=20=E6=93=8D=E4=BD=9C?= =?UTF-8?q?java=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/kwan/shuyu/Ticket12306.java | 4 +- src/test/java/com/kwan/shuyu/CuratorTest.java | 192 ------------------ ...LockTest.java => Ticket12306LockTest.java} | 10 +- .../shuyu/crud/CuratorTest_01_Create.java | 75 +++++++ .../shuyu/crud/CuratorTest_02_Create.java | 75 +++++++ .../shuyu/crud/CuratorTest_03_Create.java | 76 +++++++ .../shuyu/crud/CuratorTest_04_Create.java | 77 +++++++ .../kwan/shuyu/crud/CuratorTest_05_Get.java | 73 +++++++ .../kwan/shuyu/crud/CuratorTest_06_Get.java | 74 +++++++ .../kwan/shuyu/crud/CuratorTest_07_Get.java | 75 +++++++ .../kwan/shuyu/crud/CuratorTest_08_Set.java | 72 +++++++ .../kwan/shuyu/crud/CuratorTest_09_Set.java | 78 +++++++ .../shuyu/crud/CuratorTest_10_Delete.java | 74 +++++++ .../shuyu/crud/CuratorTest_11_Delete.java | 74 +++++++ .../shuyu/crud/CuratorTest_12_Delete.java | 74 +++++++ .../shuyu/crud/CuratorTest_13_Delete.java | 82 ++++++++ .../shuyu/{ => watch}/CuratorWatcherTest.java | 8 +- 17 files changed, 994 insertions(+), 199 deletions(-) delete mode 100644 src/test/java/com/kwan/shuyu/CuratorTest.java rename src/test/java/com/kwan/shuyu/{LockTest.java => Ticket12306LockTest.java} (65%) create mode 100644 src/test/java/com/kwan/shuyu/crud/CuratorTest_01_Create.java create mode 100644 src/test/java/com/kwan/shuyu/crud/CuratorTest_02_Create.java create mode 100644 src/test/java/com/kwan/shuyu/crud/CuratorTest_03_Create.java create mode 100644 src/test/java/com/kwan/shuyu/crud/CuratorTest_04_Create.java create mode 100644 src/test/java/com/kwan/shuyu/crud/CuratorTest_05_Get.java create mode 100644 src/test/java/com/kwan/shuyu/crud/CuratorTest_06_Get.java create mode 100644 src/test/java/com/kwan/shuyu/crud/CuratorTest_07_Get.java create mode 100644 src/test/java/com/kwan/shuyu/crud/CuratorTest_08_Set.java create mode 100644 src/test/java/com/kwan/shuyu/crud/CuratorTest_09_Set.java create mode 100644 src/test/java/com/kwan/shuyu/crud/CuratorTest_10_Delete.java create mode 100644 src/test/java/com/kwan/shuyu/crud/CuratorTest_11_Delete.java create mode 100644 src/test/java/com/kwan/shuyu/crud/CuratorTest_12_Delete.java create mode 100644 src/test/java/com/kwan/shuyu/crud/CuratorTest_13_Delete.java rename src/test/java/com/kwan/shuyu/{ => watch}/CuratorWatcherTest.java (94%) diff --git a/src/main/java/com/kwan/shuyu/Ticket12306.java b/src/main/java/com/kwan/shuyu/Ticket12306.java index 318472f..01c9426 100644 --- a/src/main/java/com/kwan/shuyu/Ticket12306.java +++ b/src/main/java/com/kwan/shuyu/Ticket12306.java @@ -27,9 +27,9 @@ public class Ticket12306 implements Runnable { //重试策略 RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10); //2.第二种方式 - //CuratorFrameworkFactory.builder(); CuratorFramework client = CuratorFrameworkFactory.builder() - .connectString("192.168.149.135:2181") + //集群节点 + .connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183") .sessionTimeoutMs(60 * 1000) .connectionTimeoutMs(15 * 1000) .retryPolicy(retryPolicy) diff --git a/src/test/java/com/kwan/shuyu/CuratorTest.java b/src/test/java/com/kwan/shuyu/CuratorTest.java deleted file mode 100644 index 9e43155..0000000 --- a/src/test/java/com/kwan/shuyu/CuratorTest.java +++ /dev/null @@ -1,192 +0,0 @@ -package com.kwan.shuyu; - -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.apache.zookeeper.CreateMode; -import org.apache.zookeeper.data.Stat; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.util.List; - -public class CuratorTest { - private CuratorFramework client; - - /** - * 建立连接 - */ - @Before - public void testConnect() { - /* - * - * @param connectString 连接字符串。zk server 地址和端口 "192.168.149.135:2181,192.168.149.136:2181" - * @param sessionTimeoutMs 会话超时时间 单位ms - * @param connectionTimeoutMs 连接超时时间 单位ms - * @param retryPolicy 重试策略 - */ - /* //重试策略 - RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10); - //1.第一种方式 - CuratorFramework client = CuratorFrameworkFactory.newClient("192.168.149.135:2181", - 60 * 1000, 15 * 1000, retryPolicy);*/ - //重试策略 - RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10); - //2.第二种方式 - //CuratorFrameworkFactory.builder(); - client = CuratorFrameworkFactory.builder() - .connectString("192.168.149.135:2181") - .sessionTimeoutMs(60 * 1000) - .connectionTimeoutMs(15 * 1000) - .retryPolicy(retryPolicy) - .namespace("itheima") - .build(); - //开启连接 - client.start(); - } -//==============================create============================================================================= - - /** - * 创建节点:create 持久 临时 顺序 数据 - * 1. 基本创建 :create().forPath("") - * 2. 创建节点 带有数据:create().forPath("",data) - * 3. 设置节点的类型:create().withMode().forPath("",data) - * 4. 创建多级节点 /app1/p1 :create().creatingParentsIfNeeded().forPath("",data) - */ - @Test - public void testCreate() throws Exception { - //2. 创建节点 带有数据 - //如果创建节点,没有指定数据,则默认将当前客户端的ip作为数据存储 - String path = client.create().forPath("/app2", "hehe".getBytes()); - System.out.println(path); - } - - @Test - public void testCreate2() throws Exception { - //1. 基本创建 - //如果创建节点,没有指定数据,则默认将当前客户端的ip作为数据存储 - String path = client.create().forPath("/app1"); - System.out.println(path); - } - - @Test - public void testCreate3() throws Exception { - //3. 设置节点的类型 - //默认类型:持久化 - String path = client.create().withMode(CreateMode.EPHEMERAL).forPath("/app3"); - System.out.println(path); - } - - @Test - public void testCreate4() throws Exception { - //4. 创建多级节点 /app1/p1 - //creatingParentsIfNeeded():如果父节点不存在,则创建父节点 - String path = client.create().creatingParentsIfNeeded().forPath("/app4/p1"); - System.out.println(path); - } -//===========================get================================================================================ - - /** - * 查询节点: - * 1. 查询数据:get: getData().forPath() - * 2. 查询子节点: ls: getChildren().forPath() - * 3. 查询节点状态信息:ls -s:getData().storingStatIn(状态对象).forPath() - */ - @Test - public void testGet1() throws Exception { - //1. 查询数据:get - byte[] data = client.getData().forPath("/app1"); - System.out.println(new String(data)); - } - - @Test - public void testGet2() throws Exception { - // 2. 查询子节点: ls - List path = client.getChildren().forPath("/"); - System.out.println(path); - } - - @Test - public void testGet3() throws Exception { - Stat status = new Stat(); - System.out.println(status); - //3. 查询节点状态信息:ls -s - client.getData().storingStatIn(status).forPath("/app1"); - System.out.println(status); - } - //===========================set================================================================================ - - /** - * 修改数据 - * 1. 基本修改数据:setData().forPath() - * 2. 根据版本修改: setData().withVersion().forPath() - * * version 是通过查询出来的。目的就是为了让其他客户端或者线程不干扰我。 - * - * @throws Exception - */ - @Test - public void testSet() throws Exception { - client.setData().forPath("/app1", "itcast".getBytes()); - } - - @Test - public void testSetForVersion() throws Exception { - Stat status = new Stat(); - //3. 查询节点状态信息:ls -s - client.getData().storingStatIn(status).forPath("/app1"); - int version = status.getVersion();//查询出来的 3 - System.out.println(version); - client.setData().withVersion(version).forPath("/app1", "hehe".getBytes()); - } - //===========================delete================================================================================ - - /** - * 删除节点: delete deleteall - * 1. 删除单个节点:delete().forPath("/app1"); - * 2. 删除带有子节点的节点:delete().deletingChildrenIfNeeded().forPath("/app1"); - * 3. 必须成功的删除:为了防止网络抖动。本质就是重试。 client.delete().guaranteed().forPath("/app2"); - * 4. 回调:inBackground - * - * @throws Exception - */ - @Test - public void testDelete() throws Exception { - // 1. 删除单个节点 - client.delete().forPath("/app1"); - } - - @Test - public void testDelete2() throws Exception { - //2. 删除带有子节点的节点 - client.delete().deletingChildrenIfNeeded().forPath("/app4"); - } - - @Test - public void testDelete3() throws Exception { - //3. 必须成功的删除 - client.delete().guaranteed().forPath("/app2"); - } - - @Test - public void testDelete4() throws Exception { - //4. 回调 - client.delete().guaranteed().inBackground(new BackgroundCallback() { - @Override - public void processResult(CuratorFramework client, CuratorEvent event) throws Exception { - System.out.println("我被删除了~"); - System.out.println(event); - } - }).forPath("/app1"); - } - - @After - public void close() { - if (client != null) { - client.close(); - } - } -} \ No newline at end of file diff --git a/src/test/java/com/kwan/shuyu/LockTest.java b/src/test/java/com/kwan/shuyu/Ticket12306LockTest.java similarity index 65% rename from src/test/java/com/kwan/shuyu/LockTest.java rename to src/test/java/com/kwan/shuyu/Ticket12306LockTest.java index 133f317..ffb3f11 100644 --- a/src/test/java/com/kwan/shuyu/LockTest.java +++ b/src/test/java/com/kwan/shuyu/Ticket12306LockTest.java @@ -1,6 +1,14 @@ package com.kwan.shuyu; -public class LockTest { + +/** + * InterProcessMutex 分布式锁测试 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/15 09:53 + */ +public class Ticket12306LockTest { public static void main(String[] args) { Ticket12306 ticket12306 = new Ticket12306(); //创建客户端 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 new file mode 100644 index 0000000..fe86496 --- /dev/null +++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_01_Create.java @@ -0,0 +1,75 @@ +package com.kwan.shuyu.crud; + +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * 创建节点 带有数据 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/15 09:54 + */ +public class CuratorTest_01_Create { + + private CuratorFramework client; + + /** + * 建立连接 + */ + @Before + public void testConnect() { + /* + * + * @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183" + * @param sessionTimeoutMs 会话超时时间 单位ms + * @param connectionTimeoutMs 连接超时时间 单位ms + * @param retryPolicy 重试策略 + */ + /* //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10); + //1.第一种方式 + CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183", + 60 * 1000, 15 * 1000, retryPolicy);*/ + //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10); + //2.第二种方式 + //CuratorFrameworkFactory.builder(); + client = CuratorFrameworkFactory.builder() + .connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183") + .sessionTimeoutMs(60 * 1000) + .connectionTimeoutMs(15 * 1000) + .retryPolicy(retryPolicy) + .namespace("itheima") + .build(); + //开启连接 + client.start(); + } + + @After + public void close() { + if (client != null) { + client.close(); + } + } + + /** + * 创建节点:create 持久 临时 顺序 数据 + * 1. 基本创建 :create().forPath("") + * 2. 创建节点 带有数据:create().forPath("",data) + * 3. 设置节点的类型:create().withMode().forPath("",data) + * 4. 创建多级节点 /app1/p1 :create().creatingParentsIfNeeded().forPath("",data) + */ + @Test + public void testCreate() throws Exception { + //默认是持久无序节点 + //如果创建节点,没有指定数据,则默认将当前客户端的ip作为数据存储 + String path = client.create().forPath("/app2", "hehe".getBytes()); + System.out.println(path); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..87da30c --- /dev/null +++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_02_Create.java @@ -0,0 +1,75 @@ +package com.kwan.shuyu.crud; + +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * 创建节点 不带数据 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/15 09:54 + */ +public class CuratorTest_02_Create { + + private CuratorFramework client; + + /** + * 建立连接 + */ + @Before + public void testConnect() { + /* + * + * @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183" + * @param sessionTimeoutMs 会话超时时间 单位ms + * @param connectionTimeoutMs 连接超时时间 单位ms + * @param retryPolicy 重试策略 + */ + /* //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10); + //1.第一种方式 + CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183", + 60 * 1000, 15 * 1000, retryPolicy);*/ + //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10); + //2.第二种方式 + //CuratorFrameworkFactory.builder(); + client = CuratorFrameworkFactory.builder() + .connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183") + .sessionTimeoutMs(60 * 1000) + .connectionTimeoutMs(15 * 1000) + .retryPolicy(retryPolicy) + .namespace("itheima") + .build(); + //开启连接 + client.start(); + } + + @After + public void close() { + if (client != null) { + client.close(); + } + } + + /** + * 创建节点:create 持久 临时 顺序 数据 + * 1. 基本创建 :create().forPath("") + * 2. 创建节点 带有数据:create().forPath("",data) + * 3. 设置节点的类型:create().withMode().forPath("",data) + * 4. 创建多级节点 /app1/p1 :create().creatingParentsIfNeeded().forPath("",data) + */ + @Test + public void testCreate2() throws Exception { + //基本创建 + //如果创建节点,没有指定数据,则默认将当前客户端的ip作为数据存储 + String path = client.create().forPath("/app1"); + System.out.println(path); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..0ba1d27 --- /dev/null +++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_03_Create.java @@ -0,0 +1,76 @@ +package com.kwan.shuyu.crud; + +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.zookeeper.CreateMode; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * 设置节点的类型 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/15 09:54 + */ +public class CuratorTest_03_Create { + + private CuratorFramework client; + + /** + * 建立连接 + */ + @Before + public void testConnect() { + /* + * + * @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183" + * @param sessionTimeoutMs 会话超时时间 单位ms + * @param connectionTimeoutMs 连接超时时间 单位ms + * @param retryPolicy 重试策略 + */ + /* //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10); + //1.第一种方式 + CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183", + 60 * 1000, 15 * 1000, retryPolicy);*/ + //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10); + //2.第二种方式 + //CuratorFrameworkFactory.builder(); + client = CuratorFrameworkFactory.builder() + .connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183") + .sessionTimeoutMs(60 * 1000) + .connectionTimeoutMs(15 * 1000) + .retryPolicy(retryPolicy) + .namespace("itheima") + .build(); + //开启连接 + client.start(); + } + + @After + public void close() { + if (client != null) { + client.close(); + } + } + + /** + * 创建节点:create 持久 临时 顺序 数据 + * 1. 基本创建 :create().forPath("") + * 2. 创建节点 带有数据:create().forPath("",data) + * 3. 设置节点的类型:create().withMode().forPath("",data) + * 4. 创建多级节点 /app1/p1 :create().creatingParentsIfNeeded().forPath("",data) + */ + @Test + public void testCreate3() throws Exception { + //设置节点的类型 + //默认类型:持久化 + String path = client.create().withMode(CreateMode.EPHEMERAL).forPath("/app3"); + System.out.println(path); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..709bf31 --- /dev/null +++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_04_Create.java @@ -0,0 +1,77 @@ +package com.kwan.shuyu.crud; + +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * 创建多级节点 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/15 09:54 + */ +public class CuratorTest_04_Create { + + private CuratorFramework client; + + /** + * 建立连接 + */ + @Before + public void testConnect() { + /* + * + * @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183" + * @param sessionTimeoutMs 会话超时时间 单位ms + * @param connectionTimeoutMs 连接超时时间 单位ms + * @param retryPolicy 重试策略 + */ + /** + //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10); + //1.第一种方式 + CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183", + 60 * 1000, 15 * 1000, retryPolicy); + */ + //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10); + //2.第二种方式 + //CuratorFrameworkFactory.builder(); + client = CuratorFrameworkFactory.builder() + .connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183") + .sessionTimeoutMs(60 * 1000) + .connectionTimeoutMs(15 * 1000) + .retryPolicy(retryPolicy) + .namespace("itheima") + .build(); + //开启连接 + client.start(); + } + + @After + public void close() { + if (client != null) { + client.close(); + } + } + + /** + * 创建节点:create 持久 临时 顺序 数据 + * 1. 基本创建 :create().forPath("") + * 2. 创建节点 带有数据:create().forPath("",data) + * 3. 设置节点的类型:create().withMode().forPath("",data) + * 4. 创建多级节点 /app1/p1 :create().creatingParentsIfNeeded().forPath("",data) + */ + @Test + public void testCreate4() throws Exception { + //4. 创建多级节点 /app1/p1 + //creatingParentsIfNeeded():如果父节点不存在,则创建父节点 + String path = client.create().creatingParentsIfNeeded().forPath("/app4/p1"); + System.out.println(path); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..d4d22a2 --- /dev/null +++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_05_Get.java @@ -0,0 +1,73 @@ +package com.kwan.shuyu.crud; + +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * 查询数据 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/15 09:54 + */ +public class CuratorTest_05_Get { + + private CuratorFramework client; + + /** + * 建立连接 + */ + @Before + public void testConnect() { + /* + * + * @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183" + * @param sessionTimeoutMs 会话超时时间 单位ms + * @param connectionTimeoutMs 连接超时时间 单位ms + * @param retryPolicy 重试策略 + */ + /* //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10); + //1.第一种方式 + CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183", + 60 * 1000, 15 * 1000, retryPolicy);*/ + //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10); + //2.第二种方式 + //CuratorFrameworkFactory.builder(); + client = CuratorFrameworkFactory.builder() + .connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183") + .sessionTimeoutMs(60 * 1000) + .connectionTimeoutMs(15 * 1000) + .retryPolicy(retryPolicy) + .namespace("itheima") + .build(); + //开启连接 + client.start(); + } + + @After + public void close() { + if (client != null) { + client.close(); + } + } + + /** + * 查询节点: + * 1. 查询数据:get: getData().forPath() + * 2. 查询子节点: ls: getChildren().forPath() + * 3. 查询节点状态信息:ls -s:getData().storingStatIn(状态对象).forPath() + */ + @Test + public void testGet1() throws Exception { + //1. 查询数据:get + byte[] data = client.getData().forPath("/app1"); + System.out.println(new String(data)); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..0ec8850 --- /dev/null +++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_06_Get.java @@ -0,0 +1,74 @@ +package com.kwan.shuyu.crud; + +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +/** + * 查询子节点 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/15 09:54 + */ +public class CuratorTest_06_Get { + private CuratorFramework client; + + /** + * 建立连接 + */ + @Before + public void testConnect() { + /* + * + * @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183" + * @param sessionTimeoutMs 会话超时时间 单位ms + * @param connectionTimeoutMs 连接超时时间 单位ms + * @param retryPolicy 重试策略 + */ + /* //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10); + //1.第一种方式 + CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183", + 60 * 1000, 15 * 1000, retryPolicy);*/ + //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10); + //2.第二种方式 + //CuratorFrameworkFactory.builder(); + client = CuratorFrameworkFactory.builder() + .connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183") + .sessionTimeoutMs(60 * 1000) + .connectionTimeoutMs(15 * 1000) + .retryPolicy(retryPolicy) + .namespace("itheima") + .build(); + //开启连接 + client.start(); + } + + @After + public void close() { + if (client != null) { + client.close(); + } + } + + /** + * 查询节点: + * 1. 查询数据:get: getData().forPath() + * 2. 查询子节点: ls: getChildren().forPath() + * 3. 查询节点状态信息:ls -s:getData().storingStatIn(状态对象).forPath() + */ + @Test + public void testGet2() throws Exception { + // 2. 查询子节点: ls + List path = client.getChildren().forPath("/"); + System.out.println(path); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..5869524 --- /dev/null +++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_07_Get.java @@ -0,0 +1,75 @@ +package com.kwan.shuyu.crud; + +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.zookeeper.data.Stat; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * 查询节点状态信息 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/15 09:54 + */ +public class CuratorTest_07_Get { + private CuratorFramework client; + + /** + * 建立连接 + */ + @Before + public void testConnect() { + /* + * + * @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183" + * @param sessionTimeoutMs 会话超时时间 单位ms + * @param connectionTimeoutMs 连接超时时间 单位ms + * @param retryPolicy 重试策略 + */ + /* //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10); + //1.第一种方式 + CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183", + 60 * 1000, 15 * 1000, retryPolicy);*/ + //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10); + //2.第二种方式 + //CuratorFrameworkFactory.builder(); + client = CuratorFrameworkFactory.builder() + .connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183") + .sessionTimeoutMs(60 * 1000) + .connectionTimeoutMs(15 * 1000) + .retryPolicy(retryPolicy) + .namespace("itheima") + .build(); + //开启连接 + client.start(); + } + + @After + public void close() { + if (client != null) { + client.close(); + } + } + + /** + * 查询节点: + * 1. 查询数据:get: getData().forPath() + * 2. 查询子节点: ls: getChildren().forPath() + * 3. 查询节点状态信息:ls -s:getData().storingStatIn(状态对象).forPath() + */ + @Test + public void testGet3() throws Exception { + Stat status = new Stat(); + System.out.println(status); + //3. 查询节点状态信息:ls -s + client.getData().storingStatIn(status).forPath("/app1"); + System.out.println(status); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..06f2da7 --- /dev/null +++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_08_Set.java @@ -0,0 +1,72 @@ +package com.kwan.shuyu.crud; + +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * 基本修改数据 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/15 09:54 + */ +public class CuratorTest_08_Set { + private CuratorFramework client; + + /** + * 建立连接 + */ + @Before + public void testConnect() { + /* + * + * @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183" + * @param sessionTimeoutMs 会话超时时间 单位ms + * @param connectionTimeoutMs 连接超时时间 单位ms + * @param retryPolicy 重试策略 + */ + /* //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10); + //1.第一种方式 + CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183", + 60 * 1000, 15 * 1000, retryPolicy);*/ + //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10); + //2.第二种方式 + //CuratorFrameworkFactory.builder(); + client = CuratorFrameworkFactory.builder() + .connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183") + .sessionTimeoutMs(60 * 1000) + .connectionTimeoutMs(15 * 1000) + .retryPolicy(retryPolicy) + .namespace("itheima") + .build(); + //开启连接 + client.start(); + } + + @After + public void close() { + if (client != null) { + client.close(); + } + } + + /** + * 修改数据 + * 1. 基本修改数据:setData().forPath() + * 2. 根据版本修改: setData().withVersion().forPath() + * * version 是通过查询出来的。目的就是为了让其他客户端或者线程不干扰我。 + * + * @throws Exception + */ + @Test + public void testSet() throws Exception { + client.setData().forPath("/app1", "itcast".getBytes()); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..802f007 --- /dev/null +++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_09_Set.java @@ -0,0 +1,78 @@ +package com.kwan.shuyu.crud; + +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.apache.zookeeper.data.Stat; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * 根据版本修改 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/15 09:54 + */ +public class CuratorTest_09_Set { + private CuratorFramework client; + + /** + * 建立连接 + */ + @Before + public void testConnect() { + /* + * + * @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183" + * @param sessionTimeoutMs 会话超时时间 单位ms + * @param connectionTimeoutMs 连接超时时间 单位ms + * @param retryPolicy 重试策略 + */ + /* //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10); + //1.第一种方式 + CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183", + 60 * 1000, 15 * 1000, retryPolicy);*/ + //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10); + //2.第二种方式 + //CuratorFrameworkFactory.builder(); + client = CuratorFrameworkFactory.builder() + .connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183") + .sessionTimeoutMs(60 * 1000) + .connectionTimeoutMs(15 * 1000) + .retryPolicy(retryPolicy) + .namespace("itheima") + .build(); + //开启连接 + client.start(); + } + + @After + public void close() { + if (client != null) { + client.close(); + } + } + + /** + * 修改数据 + * 1. 基本修改数据:setData().forPath() + * 2. 根据版本修改: setData().withVersion().forPath() + * * version 是通过查询出来的。目的就是为了让其他客户端或者线程不干扰我。 + * + * @throws Exception + */ + @Test + public void testSetForVersion() throws Exception { + Stat status = new Stat(); + //3. 查询节点状态信息:ls -s + client.getData().storingStatIn(status).forPath("/app1"); + int version = status.getVersion();//查询出来的 3 + System.out.println(version); + client.setData().withVersion(version).forPath("/app1", "hehe".getBytes()); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..5cd8728 --- /dev/null +++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_10_Delete.java @@ -0,0 +1,74 @@ +package com.kwan.shuyu.crud; + +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * 删除单个节点 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/15 09:54 + */ +public class CuratorTest_10_Delete { + private CuratorFramework client; + + /** + * 建立连接 + */ + @Before + public void testConnect() { + /* + * + * @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183" + * @param sessionTimeoutMs 会话超时时间 单位ms + * @param connectionTimeoutMs 连接超时时间 单位ms + * @param retryPolicy 重试策略 + */ + /* //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10); + //1.第一种方式 + CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183", + 60 * 1000, 15 * 1000, retryPolicy);*/ + //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10); + //2.第二种方式 + //CuratorFrameworkFactory.builder(); + client = CuratorFrameworkFactory.builder() + .connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183") + .sessionTimeoutMs(60 * 1000) + .connectionTimeoutMs(15 * 1000) + .retryPolicy(retryPolicy) + .namespace("itheima") + .build(); + //开启连接 + client.start(); + } + + @After + public void close() { + if (client != null) { + client.close(); + } + } + + /** + * 删除节点: delete deleteall + * 1. 删除单个节点:delete().forPath("/app1"); + * 2. 删除带有子节点的节点:delete().deletingChildrenIfNeeded().forPath("/app1"); + * 3. 必须成功的删除:为了防止网络抖动。本质就是重试。 client.delete().guaranteed().forPath("/app2"); + * 4. 回调:inBackground + * + * @throws Exception + */ + @Test + public void testDelete() throws Exception { + // 1. 删除单个节点 + client.delete().forPath("/app1"); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..59fa5ff --- /dev/null +++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_11_Delete.java @@ -0,0 +1,74 @@ +package com.kwan.shuyu.crud; + +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * 删除带有子节点的节点 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/15 09:54 + */ +public class CuratorTest_11_Delete { + private CuratorFramework client; + + /** + * 建立连接 + */ + @Before + public void testConnect() { + /* + * + * @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183" + * @param sessionTimeoutMs 会话超时时间 单位ms + * @param connectionTimeoutMs 连接超时时间 单位ms + * @param retryPolicy 重试策略 + */ + /* //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10); + //1.第一种方式 + CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183", + 60 * 1000, 15 * 1000, retryPolicy);*/ + //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10); + //2.第二种方式 + //CuratorFrameworkFactory.builder(); + client = CuratorFrameworkFactory.builder() + .connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183") + .sessionTimeoutMs(60 * 1000) + .connectionTimeoutMs(15 * 1000) + .retryPolicy(retryPolicy) + .namespace("itheima") + .build(); + //开启连接 + client.start(); + } + + @After + public void close() { + if (client != null) { + client.close(); + } + } + + /** + * 删除节点: delete deleteall + * 1. 删除单个节点:delete().forPath("/app1"); + * 2. 删除带有子节点的节点:delete().deletingChildrenIfNeeded().forPath("/app1"); + * 3. 必须成功的删除:为了防止网络抖动。本质就是重试。 client.delete().guaranteed().forPath("/app2"); + * 4. 回调:inBackground + * + * @throws Exception + */ + @Test + public void testDelete2() throws Exception { + //2. 删除带有子节点的节点 + client.delete().deletingChildrenIfNeeded().forPath("/app4"); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..cbcece0 --- /dev/null +++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_12_Delete.java @@ -0,0 +1,74 @@ +package com.kwan.shuyu.crud; + +import org.apache.curator.RetryPolicy; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * 必须成功的删除:为了防止网络抖动。本质就是重试。 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/15 09:54 + */ +public class CuratorTest_12_Delete { + private CuratorFramework client; + + /** + * 建立连接 + */ + @Before + public void testConnect() { + /* + * + * @param connectString 连接字符串。zk server 地址和端口 "120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183" + * @param sessionTimeoutMs 会话超时时间 单位ms + * @param connectionTimeoutMs 连接超时时间 单位ms + * @param retryPolicy 重试策略 + */ + /* //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10); + //1.第一种方式 + CuratorFramework client = CuratorFrameworkFactory.newClient("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183", + 60 * 1000, 15 * 1000, retryPolicy);*/ + //重试策略 + RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000, 10); + //2.第二种方式 + //CuratorFrameworkFactory.builder(); + client = CuratorFrameworkFactory.builder() + .connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183") + .sessionTimeoutMs(60 * 1000) + .connectionTimeoutMs(15 * 1000) + .retryPolicy(retryPolicy) + .namespace("itheima") + .build(); + //开启连接 + client.start(); + } + + @After + public void close() { + if (client != null) { + client.close(); + } + } + + /** + * 删除节点: delete deleteall + * 1. 删除单个节点:delete().forPath("/app1"); + * 2. 删除带有子节点的节点:delete().deletingChildrenIfNeeded().forPath("/app1"); + * 3. 必须成功的删除:为了防止网络抖动。本质就是重试。 client.delete().guaranteed().forPath("/app2"); + * 4. 回调:inBackground + * + * @throws Exception + */ + @Test + public void testDelete3() throws Exception { + //3. 必须成功的删除 + client.delete().guaranteed().forPath("/app2"); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..6fbb27f --- /dev/null +++ b/src/test/java/com/kwan/shuyu/crud/CuratorTest_13_Delete.java @@ -0,0 +1,82 @@ +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; + +/** + * 删除回调:inBackground + * + * @author : qinyingjie + * @version : 2.2.0 + * @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(); + } + } + + /** + * 删除节点: delete deleteall + * 1. 删除单个节点:delete().forPath("/app1"); + * 2. 删除带有子节点的节点:delete().deletingChildrenIfNeeded().forPath("/app1"); + * 3. 必须成功的删除:为了防止网络抖动。本质就是重试。 client.delete().guaranteed().forPath("/app2"); + * 4. 回调:inBackground + * + * @throws Exception + */ + @Test + public void testDelete4() throws Exception { + //4. 回调 + client.delete().guaranteed().inBackground(new BackgroundCallback() { + @Override + public void processResult(CuratorFramework client, CuratorEvent event) throws Exception { + System.out.println("我被删除了~"); + System.out.println(event); + } + }).forPath("/app1"); + } +} \ No newline at end of file diff --git a/src/test/java/com/kwan/shuyu/CuratorWatcherTest.java b/src/test/java/com/kwan/shuyu/watch/CuratorWatcherTest.java similarity index 94% rename from src/test/java/com/kwan/shuyu/CuratorWatcherTest.java rename to src/test/java/com/kwan/shuyu/watch/CuratorWatcherTest.java index 7dcdc24..e80b31a 100644 --- a/src/test/java/com/kwan/shuyu/CuratorWatcherTest.java +++ b/src/test/java/com/kwan/shuyu/watch/CuratorWatcherTest.java @@ -1,4 +1,4 @@ -package com.kwan.shuyu; +package com.kwan.shuyu.watch; import org.apache.curator.RetryPolicy; import org.apache.curator.framework.CuratorFramework; @@ -19,7 +19,7 @@ public class CuratorWatcherTest { public void testConnect() { /* * - * @param connectString 连接字符串。zk server 地址和端口 "192.168.149.135:2181,192.168.149.136:2181" + * @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 重试策略 @@ -27,14 +27,14 @@ public class CuratorWatcherTest { /* //重试策略 RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10); //1.第一种方式 - CuratorFramework client = CuratorFrameworkFactory.newClient("192.168.149.135:2181", + 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("192.168.149.135:2181") + .connectString("120.79.36.53:2181,120.79.36.53:2182,120.79.36.53:2183") .sessionTimeoutMs(60 * 1000) .connectionTimeoutMs(15 * 1000) .retryPolicy(retryPolicy) -- GitLab