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 new file mode 100644 index 0000000000000000000000000000000000000000..749a428c83f6991d7f0df7f2daf6c84927f63b84 --- /dev/null +++ b/src/test/java/com/kwan/shuyu/watch/Watcher_01_NodeCache.java @@ -0,0 +1,84 @@ +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.junit.Test; + +/** + * Watcher监听 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/15 10:29 + */ +public class Watcher_01_NodeCache { + + 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:给指定一个节点注册监听器 + */ + @Test + public void testNodeCache() throws Exception { + //1. 创建NodeCache对象 + final NodeCache nodeCache = new NodeCache(client, "/app1"); + //2. 注册监听 + nodeCache.getListenable().addListener(new NodeCacheListener() { + @Override + public void nodeChanged() throws Exception { + System.out.println("节点变化了~"); + //获取修改节点后的数据 + byte[] data = nodeCache.getCurrentData().getData(); + System.out.println(new String(data)); + } + }); + //3. 开启监听.如果设置为true,则开启监听是,加载缓冲数据 + nodeCache.start(true); + while (true) { + } + } +} \ No newline at end of file diff --git a/src/test/java/com/kwan/shuyu/watch/CuratorWatcherTest.java b/src/test/java/com/kwan/shuyu/watch/Watcher_02_PathChildrenCache.java similarity index 66% rename from src/test/java/com/kwan/shuyu/watch/CuratorWatcherTest.java rename to src/test/java/com/kwan/shuyu/watch/Watcher_02_PathChildrenCache.java index e80b31a4b3c9c84514fa119e6f35ab0bec635f7a..94bb9f874f8b0bdc23e341256ec60c7f2de88502 100644 --- a/src/test/java/com/kwan/shuyu/watch/CuratorWatcherTest.java +++ b/src/test/java/com/kwan/shuyu/watch/Watcher_02_PathChildrenCache.java @@ -9,7 +9,15 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; -public class CuratorWatcherTest { +/** + * Watcher监听 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/15 10:29 + */ +public class Watcher_02_PathChildrenCache { + private CuratorFramework client; /** @@ -51,29 +59,6 @@ public class CuratorWatcherTest { } } - /** - * 演示 NodeCache:给指定一个节点注册监听器 - */ - @Test - public void testNodeCache() throws Exception { - //1. 创建NodeCache对象 - final NodeCache nodeCache = new NodeCache(client, "/app1"); - //2. 注册监听 - nodeCache.getListenable().addListener(new NodeCacheListener() { - @Override - public void nodeChanged() throws Exception { - System.out.println("节点变化了~"); - //获取修改节点后的数据 - byte[] data = nodeCache.getCurrentData().getData(); - System.out.println(new String(data)); - } - }); - //3. 开启监听.如果设置为true,则开启监听是,加载缓冲数据 - nodeCache.start(true); - while (true) { - } - } - /** * 演示 PathChildrenCache:监听某个节点的所有子节点们 */ @@ -103,25 +88,4 @@ public class CuratorWatcherTest { while (true) { } } - - /** - * 演示 TreeCache:监听某个节点自己和所有子节点们 - */ - @Test - public void testTreeCache() throws Exception { - //1. 创建监听器 - TreeCache treeCache = new TreeCache(client, "/app2"); - //2. 注册监听 - treeCache.getListenable().addListener(new TreeCacheListener() { - @Override - public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { - System.out.println("节点变化了"); - System.out.println(event); - } - }); - //3. 开启 - treeCache.start(); - while (true) { - } - } } \ No newline at end of file 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 new file mode 100644 index 0000000000000000000000000000000000000000..ca89a429114e818ab10f8cbee2cbd5bdb5a0976e --- /dev/null +++ b/src/test/java/com/kwan/shuyu/watch/Watcher_03_TreeCache.java @@ -0,0 +1,82 @@ +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.junit.Test; + +/** + * Watcher监听 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/15 10:29 + */ +public class Watcher_03_TreeCache { + + 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:监听某个节点自己和所有子节点们 + */ + @Test + public void testTreeCache() throws Exception { + //1. 创建监听器 + TreeCache treeCache = new TreeCache(client, "/app2"); + //2. 注册监听 + treeCache.getListenable().addListener(new TreeCacheListener() { + @Override + public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception { + System.out.println("节点变化了"); + System.out.println(event); + } + }); + //3. 开启 + treeCache.start(); + while (true) { + } + } +} \ No newline at end of file