├── zookeeper ├── lib │ ├── guava-14.0.1.jar │ ├── log4j-1.2.15.jar │ ├── zkclient-0.1.jar │ ├── slf4j-api-1.6.1.jar │ ├── zookeeper-3.4.5.jar │ ├── curator-client-2.4.2.jar │ ├── slf4j-log4j12-1.6.1.jar │ ├── curator-framework-2.4.2.jar │ └── curator-recipes-2.4.2.jar ├── bin │ └── bjsxt │ │ ├── zookeeper │ │ ├── cluster │ │ │ ├── Test.class │ │ │ ├── Client1.class │ │ │ ├── Client2.class │ │ │ ├── Test$1.class │ │ │ └── ZKWatcher.class │ │ ├── auth │ │ │ └── ZookeeperAuth.class │ │ ├── base │ │ │ ├── ZookeeperBase$1.class │ │ │ └── ZookeeperBase.class │ │ └── watcher │ │ │ └── ZooKeeperWatcher.class │ │ └── zkclient │ │ ├── base │ │ └── ZkClientBase.class │ │ └── watcher │ │ ├── ZkClientWatcher1$1.class │ │ ├── ZkClientWatcher1.class │ │ ├── ZkClientWatcher2$1.class │ │ └── ZkClientWatcher2.class ├── out │ └── production │ │ └── zookeeper │ │ └── bjsxt │ │ ├── curator │ │ ├── lock │ │ │ ├── Lock1.class │ │ │ ├── Lock2.class │ │ │ ├── Lock1$1.class │ │ │ └── Lock2$1.class │ │ ├── cluster │ │ │ ├── Test.class │ │ │ ├── Client1.class │ │ │ ├── Client2.class │ │ │ ├── CuratorWatcher.class │ │ │ ├── CuratorWatcher$1.class │ │ │ └── CuratorWatcher$2.class │ │ ├── base │ │ │ └── CuratorBase.class │ │ ├── barrier │ │ │ ├── CuratorBarrier1.class │ │ │ ├── CuratorBarrier2.class │ │ │ ├── CuratorBarrier1$1.class │ │ │ └── CuratorBarrier2$1.class │ │ ├── watcher │ │ │ ├── CuratorWatcher1.class │ │ │ ├── CuratorWatcher2.class │ │ │ ├── CuratorWatcher1$1.class │ │ │ ├── CuratorWatcher2$1.class │ │ │ └── CuratorWatcher2$2.class │ │ └── atomicinteger │ │ │ └── CuratorAtomicInteger.class │ │ ├── zookeeper │ │ ├── cluster │ │ │ ├── Test$1.class │ │ │ ├── Test.class │ │ │ ├── Client1.class │ │ │ ├── Client2.class │ │ │ └── ZKWatcher.class │ │ ├── auth │ │ │ └── ZookeeperAuth.class │ │ ├── base │ │ │ ├── ZookeeperBase.class │ │ │ └── ZookeeperBase$1.class │ │ └── watcher │ │ │ └── ZooKeeperWatcher.class │ │ └── zkclient │ │ ├── base │ │ └── ZkClientBase.class │ │ └── watcher │ │ ├── ZkClientWatcher1.class │ │ ├── ZkClientWatcher2.class │ │ ├── ZkClientWatcher1$1.class │ │ └── ZkClientWatcher2$1.class ├── .idea │ ├── misc.xml │ ├── modules.xml │ ├── libraries │ │ └── lib.xml │ └── workspace.xml ├── src │ └── bjsxt │ │ ├── curator │ │ ├── cluster │ │ │ ├── Client2.java │ │ │ ├── Client1.java │ │ │ ├── Test.java │ │ │ └── CuratorWatcher.java │ │ ├── lock │ │ │ ├── Lock1.java │ │ │ └── Lock2.java │ │ ├── atomicinteger │ │ │ └── CuratorAtomicInteger.java │ │ ├── barrier │ │ │ ├── CuratorBarrier1.java │ │ │ └── CuratorBarrier2.java │ │ ├── watcher │ │ │ ├── CuratorWatcher1.java │ │ │ └── CuratorWatcher2.java │ │ └── base │ │ │ └── CuratorBase.java │ │ ├── zookeeper │ │ ├── cluster │ │ │ ├── Client1.java │ │ │ ├── Client2.java │ │ │ ├── Test.java │ │ │ └── ZKWatcher.java │ │ ├── base │ │ │ └── ZookeeperBase.java │ │ ├── watcher │ │ │ └── ZooKeeperWatcher.java │ │ └── auth │ │ │ └── ZookeeperAuth.java │ │ └── zkclient │ │ ├── watcher │ │ ├── ZkClientWatcher2.java │ │ └── ZkClientWatcher1.java │ │ └── base │ │ └── ZkClientBase.java ├── .project ├── zookeeper.iml ├── .settings │ └── org.eclipse.jdt.core.prefs └── .classpath └── README.md /zookeeper/lib/guava-14.0.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/lib/guava-14.0.1.jar -------------------------------------------------------------------------------- /zookeeper/lib/log4j-1.2.15.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/lib/log4j-1.2.15.jar -------------------------------------------------------------------------------- /zookeeper/lib/zkclient-0.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/lib/zkclient-0.1.jar -------------------------------------------------------------------------------- /zookeeper/lib/slf4j-api-1.6.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/lib/slf4j-api-1.6.1.jar -------------------------------------------------------------------------------- /zookeeper/lib/zookeeper-3.4.5.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/lib/zookeeper-3.4.5.jar -------------------------------------------------------------------------------- /zookeeper/lib/curator-client-2.4.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/lib/curator-client-2.4.2.jar -------------------------------------------------------------------------------- /zookeeper/lib/slf4j-log4j12-1.6.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/lib/slf4j-log4j12-1.6.1.jar -------------------------------------------------------------------------------- /zookeeper/lib/curator-framework-2.4.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/lib/curator-framework-2.4.2.jar -------------------------------------------------------------------------------- /zookeeper/lib/curator-recipes-2.4.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/lib/curator-recipes-2.4.2.jar -------------------------------------------------------------------------------- /zookeeper/bin/bjsxt/zookeeper/cluster/Test.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/bin/bjsxt/zookeeper/cluster/Test.class -------------------------------------------------------------------------------- /zookeeper/bin/bjsxt/zookeeper/cluster/Client1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/bin/bjsxt/zookeeper/cluster/Client1.class -------------------------------------------------------------------------------- /zookeeper/bin/bjsxt/zookeeper/cluster/Client2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/bin/bjsxt/zookeeper/cluster/Client2.class -------------------------------------------------------------------------------- /zookeeper/bin/bjsxt/zookeeper/cluster/Test$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/bin/bjsxt/zookeeper/cluster/Test$1.class -------------------------------------------------------------------------------- /zookeeper/bin/bjsxt/zkclient/base/ZkClientBase.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/bin/bjsxt/zkclient/base/ZkClientBase.class -------------------------------------------------------------------------------- /zookeeper/bin/bjsxt/zookeeper/cluster/ZKWatcher.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/bin/bjsxt/zookeeper/cluster/ZKWatcher.class -------------------------------------------------------------------------------- /zookeeper/bin/bjsxt/zookeeper/auth/ZookeeperAuth.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/bin/bjsxt/zookeeper/auth/ZookeeperAuth.class -------------------------------------------------------------------------------- /zookeeper/bin/bjsxt/zookeeper/base/ZookeeperBase$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/bin/bjsxt/zookeeper/base/ZookeeperBase$1.class -------------------------------------------------------------------------------- /zookeeper/bin/bjsxt/zookeeper/base/ZookeeperBase.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/bin/bjsxt/zookeeper/base/ZookeeperBase.class -------------------------------------------------------------------------------- /zookeeper/bin/bjsxt/zkclient/watcher/ZkClientWatcher1$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/bin/bjsxt/zkclient/watcher/ZkClientWatcher1$1.class -------------------------------------------------------------------------------- /zookeeper/bin/bjsxt/zkclient/watcher/ZkClientWatcher1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/bin/bjsxt/zkclient/watcher/ZkClientWatcher1.class -------------------------------------------------------------------------------- /zookeeper/bin/bjsxt/zkclient/watcher/ZkClientWatcher2$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/bin/bjsxt/zkclient/watcher/ZkClientWatcher2$1.class -------------------------------------------------------------------------------- /zookeeper/bin/bjsxt/zkclient/watcher/ZkClientWatcher2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/bin/bjsxt/zkclient/watcher/ZkClientWatcher2.class -------------------------------------------------------------------------------- /zookeeper/bin/bjsxt/zookeeper/watcher/ZooKeeperWatcher.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/bin/bjsxt/zookeeper/watcher/ZooKeeperWatcher.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/curator/lock/Lock1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/curator/lock/Lock1.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/curator/lock/Lock2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/curator/lock/Lock2.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/curator/cluster/Test.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/curator/cluster/Test.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/curator/lock/Lock1$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/curator/lock/Lock1$1.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/curator/lock/Lock2$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/curator/lock/Lock2$1.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/curator/base/CuratorBase.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/curator/base/CuratorBase.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/curator/cluster/Client1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/curator/cluster/Client1.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/curator/cluster/Client2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/curator/cluster/Client2.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/zookeeper/cluster/Test$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/zookeeper/cluster/Test$1.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/zookeeper/cluster/Test.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/zookeeper/cluster/Test.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/zkclient/base/ZkClientBase.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/zkclient/base/ZkClientBase.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/zookeeper/cluster/Client1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/zookeeper/cluster/Client1.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/zookeeper/cluster/Client2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/zookeeper/cluster/Client2.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/zookeeper/auth/ZookeeperAuth.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/zookeeper/auth/ZookeeperAuth.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/zookeeper/base/ZookeeperBase.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/zookeeper/base/ZookeeperBase.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/zookeeper/cluster/ZKWatcher.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/zookeeper/cluster/ZKWatcher.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/curator/barrier/CuratorBarrier1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/curator/barrier/CuratorBarrier1.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/curator/barrier/CuratorBarrier2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/curator/barrier/CuratorBarrier2.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/curator/cluster/CuratorWatcher.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/curator/cluster/CuratorWatcher.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/curator/watcher/CuratorWatcher1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/curator/watcher/CuratorWatcher1.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/curator/watcher/CuratorWatcher2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/curator/watcher/CuratorWatcher2.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/zookeeper/base/ZookeeperBase$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/zookeeper/base/ZookeeperBase$1.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/curator/barrier/CuratorBarrier1$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/curator/barrier/CuratorBarrier1$1.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/curator/barrier/CuratorBarrier2$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/curator/barrier/CuratorBarrier2$1.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/curator/cluster/CuratorWatcher$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/curator/cluster/CuratorWatcher$1.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/curator/cluster/CuratorWatcher$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/curator/cluster/CuratorWatcher$2.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/curator/watcher/CuratorWatcher1$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/curator/watcher/CuratorWatcher1$1.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/curator/watcher/CuratorWatcher2$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/curator/watcher/CuratorWatcher2$1.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/curator/watcher/CuratorWatcher2$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/curator/watcher/CuratorWatcher2$2.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/zkclient/watcher/ZkClientWatcher1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/zkclient/watcher/ZkClientWatcher1.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/zkclient/watcher/ZkClientWatcher2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/zkclient/watcher/ZkClientWatcher2.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/zookeeper/watcher/ZooKeeperWatcher.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/zookeeper/watcher/ZooKeeperWatcher.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/zkclient/watcher/ZkClientWatcher1$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/zkclient/watcher/ZkClientWatcher1$1.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/zkclient/watcher/ZkClientWatcher2$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/zkclient/watcher/ZkClientWatcher2$1.class -------------------------------------------------------------------------------- /zookeeper/out/production/zookeeper/bjsxt/curator/atomicinteger/CuratorAtomicInteger.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhaikaishun/zookeeper_tutorial/HEAD/zookeeper/out/production/zookeeper/bjsxt/curator/atomicinteger/CuratorAtomicInteger.class -------------------------------------------------------------------------------- /zookeeper/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /zookeeper/src/bjsxt/curator/cluster/Client2.java: -------------------------------------------------------------------------------- 1 | package bjsxt.curator.cluster; 2 | 3 | 4 | public class Client2 { 5 | 6 | public static void main(String[] args) throws Exception{ 7 | 8 | CuratorWatcher watcher = new CuratorWatcher(); 9 | Thread.sleep(100000000); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /zookeeper/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /zookeeper/src/bjsxt/curator/cluster/Client1.java: -------------------------------------------------------------------------------- 1 | package bjsxt.curator.cluster; 2 | 3 | 4 | 5 | public class Client1 { 6 | 7 | public static void main(String[] args) throws Exception{ 8 | 9 | CuratorWatcher watcher = new CuratorWatcher(); 10 | Thread.sleep(100000000); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /zookeeper/src/bjsxt/zookeeper/cluster/Client1.java: -------------------------------------------------------------------------------- 1 | package bjsxt.zookeeper.cluster; 2 | 3 | import bjsxt.zookeeper.cluster.ZKWatcher; 4 | 5 | public class Client1 { 6 | 7 | public static void main(String[] args) throws Exception{ 8 | 9 | ZKWatcher myWatcher = new ZKWatcher(); 10 | Thread.sleep(100000000); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /zookeeper/src/bjsxt/zookeeper/cluster/Client2.java: -------------------------------------------------------------------------------- 1 | package bjsxt.zookeeper.cluster; 2 | 3 | import bjsxt.zookeeper.cluster.ZKWatcher; 4 | 5 | public class Client2 { 6 | 7 | public static void main(String[] args) throws Exception{ 8 | 9 | ZKWatcher myWatcher = new ZKWatcher(); 10 | Thread.sleep(100000000); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /zookeeper/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | zookeeper 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /zookeeper/zookeeper.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /zookeeper/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.7 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.7 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # zookeeper_tutorial 2 | zookeeper的笔记以及教程 3 | 4 | 1. [zookeeper简介](https://blog.csdn.net/T1DMzks/article/details/78440652) 5 | 6 | 2. [搭建zookeeper集群](https://blog.csdn.net/T1DMzks/article/details/78440668) 7 | 8 | 3. [操作zookeeper之原生API的基本操作](https://blog.csdn.net/T1DMzks/article/details/78440679) 9 | 10 | 4. [zookeeper的watch(原生API)](https://blog.csdn.net/T1DMzks/article/details/78440700) 11 | 12 | 5. [zookeeper的zkclient的使用简介](https://blog.csdn.net/T1DMzks/article/details/78440717) 13 | 14 | 6. [zookeeper的ACL](https://blog.csdn.net/T1DMzks/article/details/78440752) 欢迎补充 15 | 16 | 7. [zookeeper Curator框架简单使用](https://blog.csdn.net/T1DMzks/article/details/78463098) 17 | 18 | 19 | 转发 20 | [浅谈分布式服务协调技术 Zookeeper](https://blog.csdn.net/T1DMzks/article/details/78167914) 21 | 22 | -------------------------------------------------------------------------------- /zookeeper/.idea/libraries/lib.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /zookeeper/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /zookeeper/src/bjsxt/curator/lock/Lock1.java: -------------------------------------------------------------------------------- 1 | package bjsxt.curator.lock; 2 | 3 | import java.text.SimpleDateFormat; 4 | import java.util.Date; 5 | import java.util.concurrent.CountDownLatch; 6 | import java.util.concurrent.locks.ReentrantLock; 7 | 8 | public class Lock1 { 9 | 10 | static ReentrantLock reentrantLock = new ReentrantLock(); 11 | static int count = 10; 12 | public static void genarNo(){ 13 | try { 14 | reentrantLock.lock(); 15 | count--; 16 | //System.out.println(count); 17 | } finally { 18 | reentrantLock.unlock(); 19 | } 20 | } 21 | 22 | public static void main(String[] args) throws Exception{ 23 | 24 | final CountDownLatch countdown = new CountDownLatch(1); 25 | for(int i = 0; i < 10; i++){ 26 | new Thread(new Runnable() { 27 | @Override 28 | public void run() { 29 | try { 30 | countdown.await(); 31 | genarNo(); 32 | SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss|SSS"); 33 | System.out.println(sdf.format(new Date())); 34 | //System.out.println(System.currentTimeMillis()); 35 | } catch (Exception e) { 36 | e.printStackTrace(); 37 | } finally { 38 | } 39 | } 40 | },"t" + i).start(); 41 | } 42 | Thread.sleep(50); 43 | countdown.countDown(); 44 | 45 | 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /zookeeper/src/bjsxt/zkclient/watcher/ZkClientWatcher2.java: -------------------------------------------------------------------------------- 1 | package bjsxt.zkclient.watcher; 2 | 3 | import org.I0Itec.zkclient.IZkDataListener; 4 | import org.I0Itec.zkclient.ZkClient; 5 | import org.I0Itec.zkclient.ZkConnection; 6 | 7 | public class ZkClientWatcher2 { 8 | 9 | /** zookeeper地址 */ 10 | static final String CONNECT_ADDR = "192.168.1.31:2181,192.168.1.32:2181,192.168.1.33:2181"; 11 | /** session超时时间 */ 12 | static final int SESSION_OUTTIME = 10000;//ms 13 | 14 | 15 | public static void main(String[] args) throws Exception { 16 | ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), SESSION_OUTTIME); 17 | 18 | zkc.createPersistent("/super", "1234"); 19 | 20 | //对父节点添加监听子节点变化。 21 | zkc.subscribeDataChanges("/super", new IZkDataListener() { 22 | @Override 23 | public void handleDataDeleted(String path) throws Exception { 24 | System.out.println("删除的节点为:" + path); 25 | } 26 | 27 | @Override 28 | public void handleDataChange(String path, Object data) throws Exception { 29 | System.out.println("变更的节点为:" + path + ", 变更内容为:" + data); 30 | } 31 | }); 32 | 33 | Thread.sleep(3000); 34 | zkc.writeData("/super", "456", -1); 35 | Thread.sleep(1000); 36 | 37 | zkc.delete("/super"); 38 | Thread.sleep(Integer.MAX_VALUE); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /zookeeper/src/bjsxt/zkclient/watcher/ZkClientWatcher1.java: -------------------------------------------------------------------------------- 1 | package bjsxt.zkclient.watcher; 2 | 3 | import java.util.List; 4 | 5 | import org.I0Itec.zkclient.IZkChildListener; 6 | import org.I0Itec.zkclient.ZkClient; 7 | import org.I0Itec.zkclient.ZkConnection; 8 | 9 | public class ZkClientWatcher1 { 10 | 11 | /** zookeeper地址 */ 12 | static final String CONNECT_ADDR = "192.168.1.31:2181,192.168.1.32:2181,192.168.1.33:2181"; 13 | /** session超时时间 */ 14 | static final int SESSION_OUTTIME = 10000;//ms 15 | 16 | 17 | public static void main(String[] args) throws Exception { 18 | ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), SESSION_OUTTIME); 19 | 20 | //对父节点添加监听子节点变化。 21 | zkc.subscribeChildChanges("/super", new IZkChildListener() { 22 | @Override 23 | public void handleChildChange(String parentPath, List currentChilds) throws Exception { 24 | System.out.println("parentPath: " + parentPath); 25 | System.out.println("currentChilds: " + currentChilds); 26 | } 27 | }); 28 | 29 | Thread.sleep(3000); 30 | 31 | zkc.createPersistent("/super"); 32 | Thread.sleep(1000); 33 | 34 | zkc.createPersistent("/super" + "/" + "c1", "c1内容"); 35 | Thread.sleep(1000); 36 | 37 | zkc.createPersistent("/super" + "/" + "c2", "c2内容"); 38 | Thread.sleep(1000); 39 | 40 | zkc.delete("/super/c2"); 41 | Thread.sleep(1000); 42 | 43 | zkc.deleteRecursive("/super"); 44 | Thread.sleep(Integer.MAX_VALUE); 45 | 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /zookeeper/src/bjsxt/zkclient/base/ZkClientBase.java: -------------------------------------------------------------------------------- 1 | package bjsxt.zkclient.base; 2 | 3 | import java.util.List; 4 | import java.util.concurrent.TimeUnit; 5 | 6 | import org.I0Itec.zkclient.ZkClient; 7 | import org.I0Itec.zkclient.ZkConnection; 8 | 9 | public class ZkClientBase { 10 | 11 | /** zookeeper地址 */ 12 | static final String CONNECT_ADDR = "192.168.1.31:2181,192.168.1.32:2181,192.168.1.33:2181"; 13 | /** session超时时间 */ 14 | static final int SESSION_OUTTIME = 10000;//ms 15 | 16 | 17 | public static void main(String[] args) throws Exception { 18 | ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), SESSION_OUTTIME); 19 | //1. create and delete方法 20 | 21 | zkc.createEphemeral("/temp"); 22 | zkc.createPersistent("/super/c1", true); 23 | Thread.sleep(10000); 24 | zkc.delete("/temp"); 25 | zkc.deleteRecursive("/super"); 26 | 27 | //2. 设置path和data 并且读取子节点和每个节点的内容 28 | zkc.createPersistent("/super", "1234"); 29 | zkc.createPersistent("/super/c1", "c1内容"); 30 | zkc.createPersistent("/super/c2", "c2内容"); 31 | List list = zkc.getChildren("/super"); 32 | for(String p : list){ 33 | System.out.println(p); 34 | String rp = "/super/" + p; 35 | String data = zkc.readData(rp); 36 | System.out.println("节点为:" + rp + ",内容为: " + data); 37 | } 38 | 39 | //3. 更新和判断节点是否存在 40 | zkc.writeData("/super/c1", "新内容"); 41 | System.out.println(zkc.readData("/super/c1").toString()); 42 | System.out.println(zkc.exists("/super/c1")); 43 | 44 | // 4.递归删除/super内容 45 | zkc.deleteRecursive("/super"); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /zookeeper/src/bjsxt/curator/atomicinteger/CuratorAtomicInteger.java: -------------------------------------------------------------------------------- 1 | package bjsxt.curator.atomicinteger; 2 | 3 | import org.apache.curator.RetryPolicy; 4 | import org.apache.curator.framework.CuratorFramework; 5 | import org.apache.curator.framework.CuratorFrameworkFactory; 6 | import org.apache.curator.framework.recipes.atomic.AtomicValue; 7 | import org.apache.curator.framework.recipes.atomic.DistributedAtomicInteger; 8 | import org.apache.curator.retry.ExponentialBackoffRetry; 9 | import org.apache.curator.retry.RetryNTimes; 10 | 11 | public class CuratorAtomicInteger { 12 | 13 | /** zookeeper地址 */ 14 | static final String CONNECT_ADDR = "192.168.1.171:2181,192.168.1.172:2181,192.168.1.173:2181"; 15 | /** session超时时间 */ 16 | static final int SESSION_OUTTIME = 5000;//ms 17 | 18 | public static void main(String[] args) throws Exception { 19 | 20 | //1 重试策略:初试时间为1s 重试10次 21 | RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10); 22 | //2 通过工厂创建连接 23 | CuratorFramework cf = CuratorFrameworkFactory.builder() 24 | .connectString(CONNECT_ADDR) 25 | .sessionTimeoutMs(SESSION_OUTTIME) 26 | .retryPolicy(retryPolicy) 27 | .build(); 28 | //3 开启连接 29 | cf.start(); 30 | //cf.delete().forPath("/super"); 31 | 32 | 33 | //4 使用DistributedAtomicInteger 34 | DistributedAtomicInteger atomicIntger = 35 | new DistributedAtomicInteger(cf, "/super", new RetryNTimes(3, 1000)); 36 | //atomicIntger.forceSet(0); //第一次需要有吧? 37 | 38 | AtomicValue value = atomicIntger.add(1); 39 | // atomicIntger.increment(); 40 | // AtomicValue value = atomicIntger.get(); 41 | System.out.println(value.succeeded()); 42 | System.out.println(value.postValue()); //最新值 43 | System.out.println(value.preValue()); //原始值 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /zookeeper/src/bjsxt/curator/barrier/CuratorBarrier1.java: -------------------------------------------------------------------------------- 1 | package bjsxt.curator.barrier; 2 | 3 | import java.util.Random; 4 | 5 | import org.apache.curator.RetryPolicy; 6 | import org.apache.curator.framework.CuratorFramework; 7 | import org.apache.curator.framework.CuratorFrameworkFactory; 8 | import org.apache.curator.framework.recipes.barriers.DistributedDoubleBarrier; 9 | import org.apache.curator.retry.ExponentialBackoffRetry; 10 | 11 | public class CuratorBarrier1 { 12 | 13 | /** zookeeper地址 */ 14 | static final String CONNECT_ADDR = "192.168.1.171:2181,192.168.1.172:2181,192.168.1.173:2181"; 15 | /** session超时时间 */ 16 | static final int SESSION_OUTTIME = 5000;//ms 17 | 18 | public static void main(String[] args) throws Exception { 19 | 20 | 21 | 22 | for(int i = 0; i < 5; i++){ 23 | new Thread(new Runnable() { 24 | @Override 25 | public void run() { 26 | try { 27 | RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10); 28 | CuratorFramework cf = CuratorFrameworkFactory.builder() 29 | .connectString(CONNECT_ADDR) 30 | .retryPolicy(retryPolicy) 31 | .build(); 32 | cf.start(); 33 | 34 | DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(cf, "/super", 5); 35 | Thread.sleep(1000 * (new Random()).nextInt(3)); 36 | System.out.println(Thread.currentThread().getName() + "已经准备"); 37 | barrier.enter(); 38 | System.out.println("同时开始运行..."); 39 | Thread.sleep(1000 * (new Random()).nextInt(3)); 40 | System.out.println(Thread.currentThread().getName() + "运行完毕"); 41 | barrier.leave(); 42 | System.out.println("同时退出运行..."); 43 | 44 | 45 | } catch (Exception e) { 46 | e.printStackTrace(); 47 | } 48 | } 49 | },"t" + i).start(); 50 | } 51 | 52 | 53 | 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /zookeeper/src/bjsxt/curator/cluster/Test.java: -------------------------------------------------------------------------------- 1 | package bjsxt.curator.cluster; 2 | 3 | import org.apache.curator.RetryPolicy; 4 | import org.apache.curator.framework.CuratorFramework; 5 | import org.apache.curator.framework.CuratorFrameworkFactory; 6 | import org.apache.curator.retry.ExponentialBackoffRetry; 7 | import org.apache.zookeeper.CreateMode; 8 | 9 | public class Test { 10 | 11 | /** zookeeper地址 */ 12 | static final String CONNECT_ADDR = "192.168.1.171:2181,192.168.1.172:2181,192.168.1.173:2181"; 13 | /** session超时时间 */ 14 | static final int SESSION_OUTTIME = 5000;//ms 15 | 16 | public static void main(String[] args) throws Exception { 17 | 18 | //1 重试策略:初试时间为1s 重试10次 19 | RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10); 20 | //2 通过工厂创建连接 21 | CuratorFramework cf = CuratorFrameworkFactory.builder() 22 | .connectString(CONNECT_ADDR) 23 | .sessionTimeoutMs(SESSION_OUTTIME) 24 | .retryPolicy(retryPolicy) 25 | .build(); 26 | //3 开启连接 27 | cf.start(); 28 | 29 | 30 | Thread.sleep(3000); 31 | System.out.println(cf.getChildren().forPath("/super").get(0)); 32 | 33 | //4 创建节点 34 | // Thread.sleep(1000); 35 | cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/super/c1","c1内容".getBytes()); 36 | Thread.sleep(1000); 37 | // cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/super/c2","c2内容".getBytes()); 38 | // Thread.sleep(1000); 39 | // 40 | // 41 | // 42 | // //5 读取节点 43 | // Thread.sleep(1000); 44 | // String ret1 = new String(cf.getData().forPath("/super/c1")); 45 | // System.out.println(ret1); 46 | // 47 | // 48 | // //6 修改节点 49 | Thread.sleep(1000); 50 | cf.setData().forPath("/super/c2", "修改的新c2内容".getBytes()); 51 | String ret2 = new String(cf.getData().forPath("/super/c2")); 52 | System.out.println(ret2); 53 | // 54 | // 55 | // 56 | // //7 删除节点 57 | // Thread.sleep(1000); 58 | // cf.delete().forPath("/super/c1"); 59 | 60 | 61 | 62 | 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /zookeeper/src/bjsxt/curator/barrier/CuratorBarrier2.java: -------------------------------------------------------------------------------- 1 | package bjsxt.curator.barrier; 2 | 3 | import java.text.SimpleDateFormat; 4 | import java.util.Date; 5 | 6 | import org.apache.curator.RetryPolicy; 7 | import org.apache.curator.framework.CuratorFramework; 8 | import org.apache.curator.framework.CuratorFrameworkFactory; 9 | import org.apache.curator.framework.recipes.atomic.AtomicValue; 10 | import org.apache.curator.framework.recipes.atomic.DistributedAtomicInteger; 11 | import org.apache.curator.framework.recipes.barriers.DistributedBarrier; 12 | import org.apache.curator.framework.recipes.queue.DistributedQueue; 13 | import org.apache.curator.retry.ExponentialBackoffRetry; 14 | import org.apache.curator.retry.RetryNTimes; 15 | 16 | public class CuratorBarrier2 { 17 | 18 | /** zookeeper地址 */ 19 | static final String CONNECT_ADDR = "192.168.1.171:2181,192.168.1.172:2181,192.168.1.173:2181"; 20 | /** session超时时间 */ 21 | static final int SESSION_OUTTIME = 5000;//ms 22 | 23 | static DistributedBarrier barrier = null; 24 | 25 | public static void main(String[] args) throws Exception { 26 | 27 | 28 | 29 | for(int i = 0; i < 5; i++){ 30 | new Thread(new Runnable() { 31 | @Override 32 | public void run() { 33 | try { 34 | RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10); 35 | CuratorFramework cf = CuratorFrameworkFactory.builder() 36 | .connectString(CONNECT_ADDR) 37 | .sessionTimeoutMs(SESSION_OUTTIME) 38 | .retryPolicy(retryPolicy) 39 | .build(); 40 | cf.start(); 41 | barrier = new DistributedBarrier(cf, "/super"); 42 | System.out.println(Thread.currentThread().getName() + "设置barrier!"); 43 | barrier.setBarrier(); //设置 44 | barrier.waitOnBarrier(); //等待 45 | System.out.println("---------开始执行程序----------"); 46 | } catch (Exception e) { 47 | e.printStackTrace(); 48 | } 49 | } 50 | },"t" + i).start(); 51 | } 52 | 53 | Thread.sleep(5000); 54 | barrier.removeBarrier(); //释放 55 | 56 | 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /zookeeper/src/bjsxt/curator/watcher/CuratorWatcher1.java: -------------------------------------------------------------------------------- 1 | package bjsxt.curator.watcher; 2 | 3 | import java.util.List; 4 | import java.util.concurrent.ExecutorService; 5 | import java.util.concurrent.Executors; 6 | 7 | import org.apache.curator.RetryPolicy; 8 | import org.apache.curator.framework.CuratorFramework; 9 | import org.apache.curator.framework.CuratorFrameworkFactory; 10 | import org.apache.curator.framework.api.BackgroundCallback; 11 | import org.apache.curator.framework.api.CuratorEvent; 12 | import org.apache.curator.framework.recipes.cache.NodeCache; 13 | import org.apache.curator.framework.recipes.cache.NodeCacheListener; 14 | import org.apache.curator.retry.ExponentialBackoffRetry; 15 | import org.apache.zookeeper.CreateMode; 16 | import org.apache.zookeeper.data.Stat; 17 | 18 | public class CuratorWatcher1 { 19 | 20 | /** zookeeper地址 */ 21 | static final String CONNECT_ADDR = "192.168.1.171:2181,192.168.1.172:2181,192.168.1.173:2181"; 22 | /** session超时时间 */ 23 | static final int SESSION_OUTTIME = 5000;//ms 24 | 25 | public static void main(String[] args) throws Exception { 26 | 27 | //1 重试策略:初试时间为1s 重试10次 28 | RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10); 29 | //2 通过工厂创建连接 30 | CuratorFramework cf = CuratorFrameworkFactory.builder() 31 | .connectString(CONNECT_ADDR) 32 | .sessionTimeoutMs(SESSION_OUTTIME) 33 | .retryPolicy(retryPolicy) 34 | .build(); 35 | 36 | //3 建立连接 37 | cf.start(); 38 | 39 | //4 建立一个cache缓存 40 | final NodeCache cache = new NodeCache(cf, "/super", false); 41 | cache.start(true); 42 | cache.getListenable().addListener(new NodeCacheListener() { 43 | /** 44 | * 方法名称:nodeChanged
45 | * 概要说明:触发事件为创建节点和更新节点,在删除节点的时候并不触发此操作。
46 | * @see org.apache.curator.framework.recipes.cache.NodeCacheListener#nodeChanged() 47 | */ 48 | @Override 49 | public void nodeChanged() throws Exception { 50 | System.out.println("路径为:" + cache.getCurrentData().getPath()); 51 | System.out.println("数据为:" + new String(cache.getCurrentData().getData())); 52 | System.out.println("状态为:" + cache.getCurrentData().getStat()); 53 | System.out.println("---------------------------------------"); 54 | } 55 | }); 56 | 57 | Thread.sleep(1000); 58 | cf.create().forPath("/super", "123".getBytes()); 59 | 60 | Thread.sleep(1000); 61 | cf.setData().forPath("/super", "456".getBytes()); 62 | 63 | Thread.sleep(1000); 64 | cf.delete().forPath("/super"); 65 | 66 | Thread.sleep(Integer.MAX_VALUE); 67 | 68 | 69 | 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /zookeeper/src/bjsxt/zookeeper/cluster/Test.java: -------------------------------------------------------------------------------- 1 | package bjsxt.zookeeper.cluster; 2 | 3 | import java.util.concurrent.CountDownLatch; 4 | 5 | import org.apache.zookeeper.CreateMode; 6 | import org.apache.zookeeper.WatchedEvent; 7 | import org.apache.zookeeper.Watcher; 8 | import org.apache.zookeeper.Watcher.Event.EventType; 9 | import org.apache.zookeeper.Watcher.Event.KeeperState; 10 | import org.apache.zookeeper.ZooDefs.Ids; 11 | import org.apache.zookeeper.ZooKeeper; 12 | 13 | public class Test { 14 | 15 | 16 | /** zookeeper地址 */ 17 | static final String CONNECT_ADDR = "192.168.1.31:2181,192.168.1.32:2181,192.168.1.33:2181"; 18 | /** session超时时间 */ 19 | static final int SESSION_OUTTIME = 2000;//ms 20 | /** 信号量,阻塞程序执行,用于等待zookeeper连接成功,发送成功信号 */ 21 | static final CountDownLatch connectedSemaphore = new CountDownLatch(1); 22 | 23 | public static void main(String[] args) throws Exception{ 24 | 25 | ZooKeeper zk = new ZooKeeper(CONNECT_ADDR, SESSION_OUTTIME, new Watcher(){ 26 | @Override 27 | public void process(WatchedEvent event) { 28 | //获取事件的状态 29 | KeeperState keeperState = event.getState(); 30 | EventType eventType = event.getType(); 31 | //如果是建立连接 32 | if(KeeperState.SyncConnected == keeperState){ 33 | if(EventType.None == eventType){ 34 | //如果建立连接成功,则发送信号量,让后续阻塞程序向下执行 35 | connectedSemaphore.countDown(); 36 | System.out.println("zk 建立连接"); 37 | } 38 | } 39 | } 40 | }); 41 | 42 | //进行阻塞 43 | connectedSemaphore.await(); 44 | 45 | // //创建子节点 46 | // zk.create("/super/c1", "c1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 47 | //创建子节点 48 | // zk.create("/super/c2", "c2".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 49 | //创建子节点 50 | String result = zk.create("/super/c3", "c3".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 51 | System.out.println(result); 52 | //创建子节点 53 | // zk.create("/super/c4", "c4".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 54 | 55 | // zk.create("/super/c4/c44", "c44".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 56 | 57 | //获取节点信息 58 | // byte[] data = zk.getData("/testRoot", false, null); 59 | // System.out.println(new String(data)); 60 | // System.out.println(zk.getChildren("/testRoot", false)); 61 | 62 | //修改节点的值 63 | // zk.setData("/super/c1", "modify c1".getBytes(), -1); 64 | // zk.setData("/super/c2", "modify c2".getBytes(), -1); 65 | // byte[] data = zk.getData("/super/c2", false, null); 66 | // System.out.println(new String(data)); 67 | 68 | // //判断节点是否存在 69 | // System.out.println(zk.exists("/super/c3", false)); 70 | // //删除节点 71 | // zk.delete("/super/c3", -1); 72 | 73 | zk.close(); 74 | 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /zookeeper/src/bjsxt/curator/lock/Lock2.java: -------------------------------------------------------------------------------- 1 | package bjsxt.curator.lock; 2 | 3 | import java.text.SimpleDateFormat; 4 | import java.util.Date; 5 | import java.util.concurrent.CountDownLatch; 6 | import java.util.concurrent.locks.ReentrantLock; 7 | 8 | import org.apache.curator.RetryPolicy; 9 | import org.apache.curator.framework.CuratorFramework; 10 | import org.apache.curator.framework.CuratorFrameworkFactory; 11 | import org.apache.curator.framework.recipes.locks.InterProcessMutex; 12 | import org.apache.curator.retry.ExponentialBackoffRetry; 13 | 14 | 15 | public class Lock2 { 16 | 17 | /** zookeeper地址 */ 18 | static final String CONNECT_ADDR = "192.168.1.171:2181,192.168.1.172:2181,192.168.1.173:2181"; 19 | /** session超时时间 */ 20 | static final int SESSION_OUTTIME = 5000;//ms 21 | 22 | static int count = 10; 23 | public static void genarNo(){ 24 | try { 25 | count--; 26 | System.out.println(count); 27 | } finally { 28 | 29 | } 30 | } 31 | 32 | public static void main(String[] args) throws Exception { 33 | 34 | //1 重试策略:初试时间为1s 重试10次 35 | RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10); 36 | //2 通过工厂创建连接 37 | CuratorFramework cf = CuratorFrameworkFactory.builder() 38 | .connectString(CONNECT_ADDR) 39 | .sessionTimeoutMs(SESSION_OUTTIME) 40 | .retryPolicy(retryPolicy) 41 | // .namespace("super") 42 | .build(); 43 | //3 开启连接 44 | cf.start(); 45 | 46 | //4 分布式锁 47 | // final InterProcessMutex lock = new InterProcessMutex(cf, "/super"); 48 | final ReentrantLock reentrantLock = new ReentrantLock(); 49 | final CountDownLatch countdown = new CountDownLatch(1); 50 | 51 | for(int i = 0; i < 10; i++){ 52 | new Thread(new Runnable() { 53 | @Override 54 | public void run() { 55 | InterProcessMutex lock = new InterProcessMutex(cf, "/super"); 56 | try { 57 | countdown.await(); 58 | //加锁 59 | lock.acquire(); 60 | reentrantLock.lock(); 61 | //-------------业务处理开始 62 | genarNo(); 63 | SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss|SSS"); 64 | System.out.println(sdf.format(new Date())); 65 | Thread.sleep(1000); 66 | //System.out.println(System.currentTimeMillis()); 67 | //-------------业务处理结束 68 | } catch (Exception e) { 69 | e.printStackTrace(); 70 | } finally { 71 | try { 72 | //释放 73 | lock.release(); 74 | //reentrantLock.unlock(); 75 | } catch (Exception e) { 76 | e.printStackTrace(); 77 | } 78 | } 79 | } 80 | },"t" + i).start(); 81 | } 82 | Thread.sleep(100); 83 | countdown.countDown(); 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /zookeeper/src/bjsxt/zookeeper/base/ZookeeperBase.java: -------------------------------------------------------------------------------- 1 | package bjsxt.zookeeper.base; 2 | 3 | import java.util.concurrent.CountDownLatch; 4 | 5 | import org.apache.zookeeper.*; 6 | import org.apache.zookeeper.Watcher.Event.EventType; 7 | import org.apache.zookeeper.Watcher.Event.KeeperState; 8 | import org.apache.zookeeper.ZooDefs.Ids; 9 | 10 | /** 11 | * Zookeeper base学习笔记 12 | * @since 2015-6-13 13 | */ 14 | public class ZookeeperBase { 15 | 16 | /** zookeeper地址 */ 17 | static final String CONNECT_ADDR = "192.168.1.31:2181,192.168.1.32:2181,192.168.1.33:2181"; 18 | /** session超时时间 */ 19 | static final int SESSION_OUTTIME = 2000;//ms 20 | /** 信号量,阻塞程序执行,用于等待zookeeper连接成功,发送成功信号 */ 21 | static final CountDownLatch connectedSemaphore = new CountDownLatch(1); 22 | 23 | public static void main(String[] args) throws Exception{ 24 | 25 | ZooKeeper zk = new ZooKeeper(CONNECT_ADDR, SESSION_OUTTIME, new Watcher(){ 26 | @Override 27 | public void process(WatchedEvent event) { 28 | //获取事件的状态 29 | KeeperState keeperState = event.getState(); 30 | EventType eventType = event.getType(); 31 | //如果是建立连接 32 | if(KeeperState.SyncConnected == keeperState){ 33 | if(EventType.None == eventType){ 34 | //如果建立连接成功,则发送信号量,让后续阻塞程序向下执行 35 | System.out.println("zk 建立连接"); 36 | connectedSemaphore.countDown(); 37 | 38 | } 39 | } 40 | } 41 | }); 42 | 43 | //进行阻塞 44 | connectedSemaphore.await(); 45 | System.out.println("执行了"); 46 | //创建父节点 47 | // String result = zk.create("/testRoot", "testRoot".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 48 | // System.out.println(result); 49 | 50 | //创建子节点 51 | // zk.create("/testRoot1/children", "children data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 52 | 53 | //获取节点洗信息 54 | // byte[] data = zk.getData("/testRoot", false, null); 55 | // System.out.println(new String(data)); 56 | // System.out.println(zk.getChildren("/testRoot", false)); 57 | 58 | //修改节点的值 59 | // zk.setData("/testRoot", "modify data root".getBytes(), -1); 60 | // byte[] data = zk.getData("/testRoot", false, null); 61 | // System.out.println(new String(data)); 62 | 63 | //判断节点是否存在 64 | // System.out.println(zk.exists("/testRoot/children", false)); 65 | //删除节点 66 | // zk.delete("/testRoot/children", -1); 67 | // System.out.println(zk.exists("/testRoot/children", false)); 68 | 69 | //异步的删除节点 70 | // zk.delete("/node01", -1, new AsyncCallback.VoidCallback() { 71 | // @Override 72 | // public void processResult(int rc, String path, Object ctx) { 73 | // try { 74 | // Thread.sleep(1000); 75 | // } catch (InterruptedException e) { 76 | // e.printStackTrace(); 77 | // } 78 | // System.out.println("rc: "+rc); 79 | // System.out.println("path: "+path); 80 | // System.out.println("ctx: "+ctx); 81 | // } 82 | // },"a"); 83 | // System.out.println("继续执行"); 84 | // Thread.sleep(5000); 85 | zk.close(); 86 | 87 | 88 | 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /zookeeper/src/bjsxt/curator/base/CuratorBase.java: -------------------------------------------------------------------------------- 1 | package bjsxt.curator.base; 2 | 3 | import java.util.List; 4 | import java.util.concurrent.ExecutorService; 5 | import java.util.concurrent.Executors; 6 | 7 | import org.apache.curator.RetryPolicy; 8 | import org.apache.curator.framework.CuratorFramework; 9 | import org.apache.curator.framework.CuratorFrameworkFactory; 10 | import org.apache.curator.framework.api.BackgroundCallback; 11 | import org.apache.curator.framework.api.CuratorEvent; 12 | import org.apache.curator.retry.ExponentialBackoffRetry; 13 | import org.apache.zookeeper.CreateMode; 14 | import org.apache.zookeeper.ZooKeeper.States; 15 | import org.apache.zookeeper.data.Stat; 16 | 17 | public class CuratorBase { 18 | 19 | /** zookeeper地址 */ 20 | static final String CONNECT_ADDR = "192.168.1.171:2181,192.168.1.172:2181,192.168.1.173:2181"; 21 | /** session超时时间 */ 22 | static final int SESSION_OUTTIME = 5000;//ms 23 | 24 | public static void main(String[] args) throws Exception { 25 | 26 | //1 重试策略:初试时间为1s 重试10次 27 | RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10); 28 | //2 通过工厂创建连接 29 | CuratorFramework cf = CuratorFrameworkFactory.builder() 30 | .connectString(CONNECT_ADDR) 31 | .sessionTimeoutMs(SESSION_OUTTIME) 32 | .retryPolicy(retryPolicy) 33 | // .namespace("super") 34 | .build(); 35 | //3 开启连接 36 | cf.start(); 37 | 38 | // System.out.println(States.CONNECTED); 39 | // System.out.println(cf.getState()); 40 | 41 | // 新加、删除 42 | /** 43 | //4 建立节点 指定节点类型(不加withMode默认为持久类型节点)、路径、数据内容 44 | cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/super/c1","c1内容".getBytes()); 45 | //5 删除节点 46 | cf.delete().guaranteed().deletingChildrenIfNeeded().forPath("/super"); 47 | */ 48 | 49 | // 读取、修改 50 | /** 51 | //创建节点 52 | // cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/super/c1","c1内容".getBytes()); 53 | // cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/super/c2","c2内容".getBytes()); 54 | //读取节点 55 | // String ret1 = new String(cf.getData().forPath("/super/c2")); 56 | // System.out.println(ret1); 57 | //修改节点 58 | // cf.setData().forPath("/super/c2", "修改c2内容".getBytes()); 59 | // String ret2 = new String(cf.getData().forPath("/super/c2")); 60 | // System.out.println(ret2); 61 | */ 62 | 63 | // 绑定回调函数 64 | /** 65 | ExecutorService pool = Executors.newCachedThreadPool(); 66 | cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT) 67 | .inBackground(new BackgroundCallback() { 68 | @Override 69 | public void processResult(CuratorFramework cf, CuratorEvent ce) throws Exception { 70 | System.out.println("code:" + ce.getResultCode()); 71 | System.out.println("type:" + ce.getType()); 72 | System.out.println("线程为:" + Thread.currentThread().getName()); 73 | } 74 | }, pool) 75 | .forPath("/super/c3","c3内容".getBytes()); 76 | Thread.sleep(Integer.MAX_VALUE); 77 | */ 78 | 79 | 80 | // 读取子节点getChildren方法 和 判断节点是否存在checkExists方法 81 | /** 82 | List list = cf.getChildren().forPath("/super"); 83 | for(String p : list){ 84 | System.out.println(p); 85 | } 86 | 87 | Stat stat = cf.checkExists().forPath("/super/c3"); 88 | System.out.println(stat); 89 | 90 | Thread.sleep(2000); 91 | cf.delete().guaranteed().deletingChildrenIfNeeded().forPath("/super"); 92 | */ 93 | 94 | 95 | //cf.delete().guaranteed().deletingChildrenIfNeeded().forPath("/super"); 96 | 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /zookeeper/src/bjsxt/curator/cluster/CuratorWatcher.java: -------------------------------------------------------------------------------- 1 | package bjsxt.curator.cluster; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | import java.util.concurrent.ConcurrentHashMap; 6 | import java.util.concurrent.CopyOnWriteArrayList; 7 | import java.util.concurrent.CountDownLatch; 8 | 9 | import org.apache.curator.RetryPolicy; 10 | import org.apache.curator.framework.CuratorFramework; 11 | import org.apache.curator.framework.CuratorFrameworkFactory; 12 | import org.apache.curator.framework.recipes.cache.PathChildrenCache; 13 | import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent; 14 | import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; 15 | import org.apache.curator.framework.recipes.cache.PathChildrenCache.StartMode; 16 | import org.apache.curator.retry.ExponentialBackoffRetry; 17 | import org.apache.zookeeper.CreateMode; 18 | import org.apache.zookeeper.KeeperException; 19 | import org.apache.zookeeper.WatchedEvent; 20 | import org.apache.zookeeper.Watcher; 21 | import org.apache.zookeeper.Watcher.Event.EventType; 22 | import org.apache.zookeeper.Watcher.Event.KeeperState; 23 | import org.apache.zookeeper.ZooDefs.Ids; 24 | import org.apache.zookeeper.ZooKeeper; 25 | 26 | public class CuratorWatcher { 27 | 28 | /** 父节点path */ 29 | static final String PARENT_PATH = "/super"; 30 | 31 | /** zookeeper服务器地址 */ 32 | public static final String CONNECT_ADDR = "192.168.1.171:2181,192.168.1.172:2181,192.168.1.173:2181"; /** 定义session失效时间 */ 33 | 34 | public static final int SESSION_TIMEOUT = 30000; 35 | 36 | public CuratorWatcher() throws Exception{ 37 | //1 重试策略:初试时间为1s 重试10次 38 | RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10); 39 | //2 通过工厂创建连接 40 | CuratorFramework cf = CuratorFrameworkFactory.builder() 41 | .connectString(CONNECT_ADDR) 42 | .sessionTimeoutMs(SESSION_TIMEOUT) 43 | .retryPolicy(retryPolicy) 44 | .build(); 45 | //3 建立连接 46 | cf.start(); 47 | 48 | //4 创建跟节点 49 | if(cf.checkExists().forPath(PARENT_PATH) == null){ 50 | cf.create().withMode(CreateMode.PERSISTENT).forPath(PARENT_PATH,"super init".getBytes()); 51 | } 52 | 53 | //4 建立一个PathChildrenCache缓存,第三个参数为是否接受节点数据内容 如果为false则不接受 54 | PathChildrenCache cache = new PathChildrenCache(cf, PARENT_PATH, true); 55 | //5 在初始化的时候就进行缓存监听 56 | cache.start(StartMode.POST_INITIALIZED_EVENT); 57 | cache.getListenable().addListener(new PathChildrenCacheListener() { 58 | /** 59 | * 方法名称:监听子节点变更
60 | * 概要说明:新建、修改、删除
61 | * @see org.apache.curator.framework.recipes.cache.PathChildrenCacheListener#childEvent(org.apache.curator.framework.CuratorFramework, org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) 62 | */ 63 | @Override 64 | public void childEvent(CuratorFramework cf, PathChildrenCacheEvent event) throws Exception { 65 | switch (event.getType()) { 66 | case CHILD_ADDED: 67 | System.out.println("CHILD_ADDED :" + event.getData().getPath()); 68 | System.out.println("CHILD_ADDED :" + new String(event.getData().getData())); 69 | break; 70 | case CHILD_UPDATED: 71 | System.out.println("CHILD_UPDATED :" + event.getData().getPath()); 72 | System.out.println("CHILD_UPDATED :" + new String(event.getData().getData())); 73 | break; 74 | case CHILD_REMOVED: 75 | System.out.println("CHILD_REMOVED :" + event.getData().getPath()); 76 | System.out.println("CHILD_REMOVED :" + new String(event.getData().getData())); 77 | break; 78 | default: 79 | break; 80 | } 81 | } 82 | }); 83 | } 84 | 85 | } 86 | -------------------------------------------------------------------------------- /zookeeper/src/bjsxt/curator/watcher/CuratorWatcher2.java: -------------------------------------------------------------------------------- 1 | package bjsxt.curator.watcher; 2 | 3 | import java.util.List; 4 | import java.util.concurrent.ExecutorService; 5 | import java.util.concurrent.Executors; 6 | 7 | import org.apache.curator.RetryPolicy; 8 | import org.apache.curator.framework.CuratorFramework; 9 | import org.apache.curator.framework.CuratorFrameworkFactory; 10 | import org.apache.curator.framework.api.BackgroundCallback; 11 | import org.apache.curator.framework.api.CuratorEvent; 12 | import org.apache.curator.framework.recipes.cache.NodeCache; 13 | import org.apache.curator.framework.recipes.cache.NodeCacheListener; 14 | import org.apache.curator.framework.recipes.cache.PathChildrenCache; 15 | import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent; 16 | import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener; 17 | import org.apache.curator.framework.recipes.cache.PathChildrenCache.StartMode; 18 | import org.apache.curator.retry.ExponentialBackoffRetry; 19 | import org.apache.zookeeper.CreateMode; 20 | import org.apache.zookeeper.data.Stat; 21 | 22 | public class CuratorWatcher2 { 23 | 24 | /** zookeeper地址 */ 25 | static final String CONNECT_ADDR = "192.168.1.171:2181,192.168.1.172:2181,192.168.1.173:2181"; 26 | /** session超时时间 */ 27 | static final int SESSION_OUTTIME = 5000;//ms 28 | 29 | public static void main(String[] args) throws Exception { 30 | 31 | //1 重试策略:初试时间为1s 重试10次 32 | RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10); 33 | //2 通过工厂创建连接 34 | CuratorFramework cf = CuratorFrameworkFactory.builder() 35 | .connectString(CONNECT_ADDR) 36 | .sessionTimeoutMs(SESSION_OUTTIME) 37 | .retryPolicy(retryPolicy) 38 | .build(); 39 | 40 | //3 建立连接 41 | cf.start(); 42 | 43 | //4 建立一个PathChildrenCache缓存,第三个参数为是否接受节点数据内容 如果为false则不接受 44 | PathChildrenCache cache = new PathChildrenCache(cf, "/super", true); 45 | //5 在初始化的时候就进行缓存监听 46 | cache.start(StartMode.POST_INITIALIZED_EVENT); 47 | cache.getListenable().addListener(new PathChildrenCacheListener() { 48 | /** 49 | * 方法名称:监听子节点变更
50 | * 概要说明:新建、修改、删除
51 | * @see org.apache.curator.framework.recipes.cache.PathChildrenCacheListener#childEvent(org.apache.curator.framework.CuratorFramework, org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) 52 | */ 53 | @Override 54 | public void childEvent(CuratorFramework cf, PathChildrenCacheEvent event) throws Exception { 55 | switch (event.getType()) { 56 | case CHILD_ADDED: 57 | System.out.println("CHILD_ADDED :" + event.getData().getPath()); 58 | //也可以获取内容 59 | System.out.println("CHILD_ADDED 内容 :" + new String(event.getData().getData(),"utf-8")); 60 | break; 61 | case CHILD_UPDATED: 62 | System.out.println("CHILD_UPDATED :" + event.getData().getPath()); 63 | System.out.println("CHILD_UPDATED 内容 :" + new String(event.getData().getData(),"utf-8")); 64 | break; 65 | case CHILD_REMOVED: 66 | System.out.println("CHILD_REMOVED :" + event.getData().getPath()); 67 | break; 68 | default: 69 | break; 70 | } 71 | } 72 | }); 73 | 74 | //创建本身节点不发生变化 75 | cf.create().forPath("/super", "init".getBytes()); 76 | 77 | //添加子节点 78 | Thread.sleep(1000); 79 | cf.create().forPath("/super/c1", "c1内容".getBytes()); 80 | Thread.sleep(1000); 81 | cf.create().forPath("/super/c2", "c2内容".getBytes()); 82 | 83 | //修改子节点 84 | Thread.sleep(1000); 85 | cf.setData().forPath("/super/c1", "c1更新内容".getBytes()); 86 | 87 | //删除子节点 88 | Thread.sleep(1000); 89 | cf.delete().forPath("/super/c2"); 90 | 91 | //删除本身节点 92 | Thread.sleep(1000); 93 | cf.delete().deletingChildrenIfNeeded().forPath("/super"); 94 | 95 | Thread.sleep(Integer.MAX_VALUE); 96 | 97 | 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /zookeeper/src/bjsxt/zookeeper/cluster/ZKWatcher.java: -------------------------------------------------------------------------------- 1 | package bjsxt.zookeeper.cluster; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | import java.util.concurrent.ConcurrentHashMap; 6 | import java.util.concurrent.CopyOnWriteArrayList; 7 | import java.util.concurrent.CountDownLatch; 8 | 9 | import org.apache.zookeeper.CreateMode; 10 | import org.apache.zookeeper.KeeperException; 11 | import org.apache.zookeeper.WatchedEvent; 12 | import org.apache.zookeeper.Watcher; 13 | import org.apache.zookeeper.Watcher.Event.EventType; 14 | import org.apache.zookeeper.Watcher.Event.KeeperState; 15 | import org.apache.zookeeper.ZooDefs.Ids; 16 | import org.apache.zookeeper.ZooKeeper; 17 | 18 | public class ZKWatcher implements Watcher { 19 | 20 | /** zk变量 */ 21 | private ZooKeeper zk = null; 22 | 23 | /** 父节点path */ 24 | static final String PARENT_PATH = "/super"; 25 | 26 | /** 信号量设置,用于等待zookeeper连接建立之后 通知阻塞程序继续向下执行 */ 27 | private CountDownLatch connectedSemaphore = new CountDownLatch(1); 28 | 29 | private List cowaList = new CopyOnWriteArrayList(); 30 | 31 | 32 | /** zookeeper服务器地址 */ 33 | public static final String CONNECTION_ADDR = "192.168.1.31:2181,192.168.1.32:2181,192.168.1.33:2181"; 34 | /** 定义session失效时间 */ 35 | public static final int SESSION_TIMEOUT = 30000; 36 | 37 | public ZKWatcher() throws Exception{ 38 | zk = new ZooKeeper(CONNECTION_ADDR, SESSION_TIMEOUT, this); 39 | System.out.println("开始连接ZK服务器"); 40 | connectedSemaphore.await(); 41 | } 42 | 43 | 44 | @Override 45 | public void process(WatchedEvent event) { 46 | // 连接状态 47 | KeeperState keeperState = event.getState(); 48 | // 事件类型 49 | EventType eventType = event.getType(); 50 | // 受影响的path 51 | String path = event.getPath(); 52 | System.out.println("受影响的path : " + path); 53 | 54 | 55 | if (KeeperState.SyncConnected == keeperState) { 56 | // 成功连接上ZK服务器 57 | if (EventType.None == eventType) { 58 | System.out.println("成功连接上ZK服务器"); 59 | connectedSemaphore.countDown(); 60 | try { 61 | if(this.zk.exists(PARENT_PATH, false) == null){ 62 | this.zk.create(PARENT_PATH, "root".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 63 | } 64 | List paths = this.zk.getChildren(PARENT_PATH, true); 65 | for (String p : paths) { 66 | System.out.println(p); 67 | this.zk.exists(PARENT_PATH + "/" + p, true); 68 | } 69 | } catch (KeeperException | InterruptedException e) { 70 | e.printStackTrace(); 71 | } 72 | } 73 | //创建节点 74 | else if (EventType.NodeCreated == eventType) { 75 | System.out.println("节点创建"); 76 | try { 77 | this.zk.exists(path, true); 78 | } catch (KeeperException | InterruptedException e) { 79 | e.printStackTrace(); 80 | } 81 | } 82 | //更新节点 83 | else if (EventType.NodeDataChanged == eventType) { 84 | System.out.println("节点数据更新"); 85 | try { 86 | //update nodes call function 87 | this.zk.exists(path, true); 88 | } catch (KeeperException | InterruptedException e) { 89 | e.printStackTrace(); 90 | } 91 | } 92 | //更新子节点 93 | else if (EventType.NodeChildrenChanged == eventType) { 94 | System.out.println("子节点 ... 变更"); 95 | try { 96 | List paths = this.zk.getChildren(path, true); 97 | if(paths.size() >= cowaList.size()){ 98 | paths.removeAll(cowaList); 99 | for(String p : paths){ 100 | this.zk.exists(path + "/" + p, true); 101 | //this.zk.getChildren(path + "/" + p, true); 102 | System.out.println("这个是新增的子节点 : " + path + "/" + p); 103 | //add new nodes call function 104 | } 105 | cowaList.addAll(paths); 106 | } else { 107 | cowaList = paths; 108 | } 109 | System.out.println("cowaList: " + cowaList.toString()); 110 | System.out.println("paths: " + paths.toString()); 111 | 112 | } catch (KeeperException | InterruptedException e) { 113 | e.printStackTrace(); 114 | } 115 | } 116 | //删除节点 117 | else if (EventType.NodeDeleted == eventType) { 118 | System.out.println("节点 " + path + " 被删除"); 119 | try { 120 | //delete nodes call function 121 | this.zk.exists(path, true); 122 | } catch (KeeperException | InterruptedException e) { 123 | e.printStackTrace(); 124 | } 125 | } 126 | else ; 127 | } 128 | else if (KeeperState.Disconnected == keeperState) { 129 | System.out.println("与ZK服务器断开连接"); 130 | } 131 | else if (KeeperState.AuthFailed == keeperState) { 132 | System.out.println("权限检查失败"); 133 | } 134 | else if (KeeperState.Expired == keeperState) { 135 | System.out.println("会话失效"); 136 | } 137 | else ; 138 | 139 | System.out.println("--------------------------------------------"); 140 | } 141 | 142 | 143 | 144 | } 145 | -------------------------------------------------------------------------------- /zookeeper/src/bjsxt/zookeeper/watcher/ZooKeeperWatcher.java: -------------------------------------------------------------------------------- 1 | package bjsxt.zookeeper.watcher; 2 | 3 | import java.util.List; 4 | import java.util.concurrent.CountDownLatch; 5 | import java.util.concurrent.atomic.AtomicInteger; 6 | 7 | import org.apache.zookeeper.CreateMode; 8 | import org.apache.zookeeper.WatchedEvent; 9 | import org.apache.zookeeper.Watcher; 10 | import org.apache.zookeeper.Watcher.Event.EventType; 11 | import org.apache.zookeeper.Watcher.Event.KeeperState; 12 | import org.apache.zookeeper.ZooDefs.Ids; 13 | import org.apache.zookeeper.ZooKeeper; 14 | import org.apache.zookeeper.data.Stat; 15 | 16 | /** 17 | * Zookeeper Wathcher 18 | * 本类就是一个Watcher类(实现了org.apache.zookeeper.Watcher类) 19 | * @author(alienware) 20 | * @since 2015-6-14 21 | */ 22 | public class ZooKeeperWatcher implements Watcher { 23 | 24 | /** 定义原子变量 */ 25 | AtomicInteger seq = new AtomicInteger(); 26 | /** 定义session失效时间 */ 27 | private static final int SESSION_TIMEOUT = 10000; 28 | /** zookeeper服务器地址 */ 29 | private static final String CONNECTION_ADDR = "192.168.1.31:2181"; 30 | /** zk父路径设置 */ 31 | private static final String PARENT_PATH = "/testWatch"; 32 | /** zk子路径设置 */ 33 | private static final String CHILDREN_PATH = "/testWatch/children"; 34 | /** 进入标识 */ 35 | private static final String LOG_PREFIX_OF_MAIN = "【Main】"; 36 | /** zk变量 */ 37 | private ZooKeeper zk = null; 38 | /** 信号量设置,用于等待zookeeper连接建立之后 通知阻塞程序继续向下执行 */ 39 | private CountDownLatch connectedSemaphore = new CountDownLatch(1); 40 | 41 | /** 42 | * 创建ZK连接 43 | * @param connectAddr ZK服务器地址列表 44 | * @param sessionTimeout Session超时时间 45 | */ 46 | public void createConnection(String connectAddr, int sessionTimeout) { 47 | this.releaseConnection(); 48 | try { 49 | zk = new ZooKeeper(connectAddr, sessionTimeout, this); 50 | System.out.println(LOG_PREFIX_OF_MAIN + "开始连接ZK服务器"); 51 | connectedSemaphore.await(); 52 | } catch (Exception e) { 53 | e.printStackTrace(); 54 | } 55 | } 56 | 57 | /** 58 | * 关闭ZK连接 59 | */ 60 | public void releaseConnection() { 61 | if (this.zk != null) { 62 | try { 63 | this.zk.close(); 64 | } catch (InterruptedException e) { 65 | e.printStackTrace(); 66 | } 67 | } 68 | } 69 | 70 | /** 71 | * 创建节点 72 | * @param path 节点路径 73 | * @param data 数据内容 74 | * @return 75 | */ 76 | public boolean createPath(String path, String data,boolean ifsetTrue) { 77 | try { 78 | //设置监控(由于zookeeper的监控都是一次性的所以 每次必须设置监控) 79 | this.zk.exists(path, ifsetTrue); 80 | System.out.println(LOG_PREFIX_OF_MAIN + "节点创建成功, Path: " + 81 | this.zk.create( /**路径*/ 82 | path, 83 | /**数据*/ 84 | data.getBytes(), 85 | /**所有可见*/ 86 | Ids.OPEN_ACL_UNSAFE, 87 | /**永久存储*/ 88 | CreateMode.PERSISTENT ) + 89 | ", content: " + data); 90 | } catch (Exception e) { 91 | e.printStackTrace(); 92 | return false; 93 | } 94 | return true; 95 | } 96 | 97 | /** 98 | * 读取指定节点数据内容 99 | * @param path 节点路径 100 | * @return 101 | */ 102 | public String readData(String path, boolean needWatch) { 103 | try { 104 | return new String(this.zk.getData(path, needWatch, null)); 105 | } catch (Exception e) { 106 | e.printStackTrace(); 107 | return ""; 108 | } 109 | } 110 | 111 | /** 112 | * 更新指定节点数据内容 113 | * @param path 节点路径 114 | * @param data 数据内容 115 | * @return 116 | */ 117 | public boolean writeData(String path, String data) { 118 | try { 119 | System.out.println(LOG_PREFIX_OF_MAIN + "更新数据成功,path:" + path + ", stat: " + 120 | this.zk.setData(path, data.getBytes(), -1)); 121 | } catch (Exception e) { 122 | e.printStackTrace(); 123 | } 124 | return false; 125 | } 126 | 127 | /** 128 | * 删除指定节点 129 | * 130 | * @param path 131 | * 节点path 132 | */ 133 | public void deleteNode(String path) { 134 | try { 135 | this.zk.delete(path, -1); 136 | System.out.println(LOG_PREFIX_OF_MAIN + "删除节点成功,path:" + path); 137 | } catch (Exception e) { 138 | e.printStackTrace(); 139 | } 140 | } 141 | 142 | /** 143 | * 判断指定节点是否存在 144 | * @param path 节点路径 145 | */ 146 | public Stat exists(String path, boolean needWatch) { 147 | try { 148 | return this.zk.exists(path, needWatch); 149 | } catch (Exception e) { 150 | e.printStackTrace(); 151 | return null; 152 | } 153 | } 154 | 155 | /** 156 | * 获取子节点 157 | * @param path 节点路径 158 | */ 159 | private List getChildren(String path, boolean needWatch) { 160 | try { 161 | return this.zk.getChildren(path, needWatch); 162 | } catch (Exception e) { 163 | e.printStackTrace(); 164 | return null; 165 | } 166 | } 167 | 168 | /** 169 | * 删除所有节点 170 | */ 171 | public void deleteAllTestPath() { 172 | if(this.exists(CHILDREN_PATH, false) != null){ 173 | this.deleteNode(CHILDREN_PATH); 174 | } 175 | if(this.exists(PARENT_PATH, false) != null){ 176 | this.deleteNode(PARENT_PATH); 177 | } 178 | } 179 | 180 | /** 181 | * 收到来自Server的Watcher通知后的处理。 182 | */ 183 | @Override 184 | public void process(WatchedEvent event) { 185 | 186 | System.out.println("进入 process 。。。。。event = " + event); 187 | 188 | try { 189 | Thread.sleep(200); 190 | } catch (InterruptedException e) { 191 | e.printStackTrace(); 192 | } 193 | 194 | if (event == null) { 195 | return; 196 | } 197 | 198 | // 连接状态 199 | KeeperState keeperState = event.getState(); 200 | // 事件类型 201 | EventType eventType = event.getType(); 202 | // 受影响的path 203 | String path = event.getPath(); 204 | 205 | String logPrefix = "【Watcher-" + this.seq.incrementAndGet() + "】"; 206 | 207 | System.out.println(logPrefix + "收到Watcher通知"); 208 | System.out.println(logPrefix + "连接状态:\t" + keeperState.toString()); 209 | System.out.println(logPrefix + "事件类型:\t" + eventType.toString()); 210 | 211 | if (KeeperState.SyncConnected == keeperState) { 212 | // 成功连接上ZK服务器 213 | if (EventType.None == eventType) { 214 | System.out.println(logPrefix + "成功连接上ZK服务器"); 215 | connectedSemaphore.countDown(); 216 | } 217 | //创建节点 218 | else if (EventType.NodeCreated == eventType) { 219 | System.out.println(logPrefix + "节点创建"); 220 | try { 221 | Thread.sleep(100); 222 | } catch (InterruptedException e) { 223 | e.printStackTrace(); 224 | } 225 | this.exists(path, true); 226 | } 227 | //更新节点 228 | else if (EventType.NodeDataChanged == eventType) { 229 | System.out.println(logPrefix + "节点数据更新"); 230 | System.out.println("我看看走不走这里........"); 231 | try { 232 | Thread.sleep(100); 233 | } catch (InterruptedException e) { 234 | e.printStackTrace(); 235 | } 236 | System.out.println(logPrefix + "数据内容: " + this.readData(PARENT_PATH, true)); 237 | } 238 | //更新子节点 239 | else if (EventType.NodeChildrenChanged == eventType) { 240 | System.out.println(logPrefix + "子节点变更"); 241 | try { 242 | Thread.sleep(3000); 243 | } catch (InterruptedException e) { 244 | e.printStackTrace(); 245 | } 246 | System.out.println(logPrefix + "子节点列表:" + this.getChildren(PARENT_PATH, true)); 247 | } 248 | //删除节点 249 | else if (EventType.NodeDeleted == eventType) { 250 | System.out.println(logPrefix + "节点 " + path + " 被删除"); 251 | } 252 | else ; 253 | } 254 | else if (KeeperState.Disconnected == keeperState) { 255 | System.out.println(logPrefix + "与ZK服务器断开连接"); 256 | } 257 | else if (KeeperState.AuthFailed == keeperState) { 258 | System.out.println(logPrefix + "权限检查失败"); 259 | } 260 | else if (KeeperState.Expired == keeperState) { 261 | System.out.println(logPrefix + "会话失效"); 262 | } 263 | else ; 264 | 265 | System.out.println("--------------------------------------------"); 266 | 267 | } 268 | 269 | /** 270 | * 方法名称:测试zookeeper监控
271 | * 概要说明:主要测试watch功能
272 | * @param args 273 | * @throws Exception 274 | */ 275 | public static void main(String[] args) throws Exception { 276 | 277 | //建立watcher 278 | ZooKeeperWatcher zkWatch = new ZooKeeperWatcher(); 279 | //创建连接 280 | zkWatch.createConnection(CONNECTION_ADDR, SESSION_TIMEOUT); 281 | //System.out.println(zkWatch.zk.toString()); 282 | 283 | Thread.sleep(1000); 284 | 285 | // 清理节点 286 | zkWatch.deleteAllTestPath(); 287 | 288 | if (zkWatch.createPath(PARENT_PATH, System.currentTimeMillis() + "",true)) { 289 | 290 | Thread.sleep(1000); 291 | 292 | 293 | // 读取数据 294 | System.out.println("---------------------- read parent ----------------------------"); 295 | //zkWatch.readData(PARENT_PATH, true); 296 | 297 | // 读取子节点 298 | System.out.println("---------------------- read children path ----------------------------"); 299 | zkWatch.getChildren(PARENT_PATH, true); 300 | 301 | // 更新数据 302 | zkWatch.writeData(PARENT_PATH, System.currentTimeMillis() + ""); 303 | 304 | Thread.sleep(1000); 305 | 306 | // 创建子节点 307 | zkWatch.createPath(CHILDREN_PATH, System.currentTimeMillis() + "",true); 308 | 309 | Thread.sleep(1000); 310 | 311 | zkWatch.writeData(CHILDREN_PATH, System.currentTimeMillis() + ""); 312 | } 313 | 314 | Thread.sleep(50000); 315 | // 清理节点 316 | zkWatch.deleteAllTestPath(); 317 | Thread.sleep(1000); 318 | zkWatch.releaseConnection(); 319 | } 320 | 321 | } -------------------------------------------------------------------------------- /zookeeper/src/bjsxt/zookeeper/auth/ZookeeperAuth.java: -------------------------------------------------------------------------------- 1 | package bjsxt.zookeeper.auth; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.concurrent.CountDownLatch; 6 | import java.util.concurrent.atomic.AtomicInteger; 7 | 8 | import org.apache.zookeeper.CreateMode; 9 | import org.apache.zookeeper.WatchedEvent; 10 | import org.apache.zookeeper.Watcher; 11 | import org.apache.zookeeper.Watcher.Event.EventType; 12 | import org.apache.zookeeper.Watcher.Event.KeeperState; 13 | import org.apache.zookeeper.ZooDefs.Ids; 14 | import org.apache.zookeeper.ZooKeeper; 15 | import org.apache.zookeeper.data.ACL; 16 | import org.apache.zookeeper.data.Stat; 17 | /** 18 | * Zookeeper 节点授权 19 | * @author(alienware) 20 | * @since 2015-6-14 21 | */ 22 | public class ZookeeperAuth implements Watcher { 23 | 24 | /** 连接地址 */ 25 | final static String CONNECT_ADDR = "192.168.1.31:2181"; 26 | /** 测试路径 */ 27 | final static String PATH = "/testAuth"; 28 | final static String PATH_DEL = "/testAuth/delNode"; 29 | /** 认证类型 */ 30 | final static String authentication_type = "digest"; 31 | /** 认证正确方法 */ 32 | final static String correctAuthentication = "123456"; 33 | /** 认证错误方法 */ 34 | final static String badAuthentication = "654321"; 35 | 36 | static ZooKeeper zk = null; 37 | /** 计时器 */ 38 | AtomicInteger seq = new AtomicInteger(); 39 | /** 标识 */ 40 | private static final String LOG_PREFIX_OF_MAIN = "【Main】"; 41 | 42 | private CountDownLatch connectedSemaphore = new CountDownLatch(1); 43 | 44 | @Override 45 | public void process(WatchedEvent event) { 46 | try { 47 | Thread.sleep(200); 48 | } catch (InterruptedException e) { 49 | e.printStackTrace(); 50 | } 51 | if (event==null) { 52 | return; 53 | } 54 | // 连接状态 55 | KeeperState keeperState = event.getState(); 56 | // 事件类型 57 | EventType eventType = event.getType(); 58 | // 受影响的path 59 | String path = event.getPath(); 60 | 61 | String logPrefix = "【Watcher-" + this.seq.incrementAndGet() + "】"; 62 | 63 | System.out.println(logPrefix + "收到Watcher通知"); 64 | System.out.println(logPrefix + "连接状态:\t" + keeperState.toString()); 65 | System.out.println(logPrefix + "事件类型:\t" + eventType.toString()); 66 | if (KeeperState.SyncConnected == keeperState) { 67 | // 成功连接上ZK服务器 68 | if (EventType.None == eventType) { 69 | System.out.println(logPrefix + "成功连接上ZK服务器"); 70 | connectedSemaphore.countDown(); 71 | } 72 | } else if (KeeperState.Disconnected == keeperState) { 73 | System.out.println(logPrefix + "与ZK服务器断开连接"); 74 | } else if (KeeperState.AuthFailed == keeperState) { 75 | System.out.println(logPrefix + "权限检查失败"); 76 | } else if (KeeperState.Expired == keeperState) { 77 | System.out.println(logPrefix + "会话失效"); 78 | } 79 | System.out.println("--------------------------------------------"); 80 | } 81 | /** 82 | * 创建ZK连接 83 | * 84 | * @param connectString 85 | * ZK服务器地址列表 86 | * @param sessionTimeout 87 | * Session超时时间 88 | */ 89 | public void createConnection(String connectString, int sessionTimeout) { 90 | this.releaseConnection(); 91 | try { 92 | zk = new ZooKeeper(connectString, sessionTimeout, this); 93 | //添加节点授权 94 | zk.addAuthInfo(authentication_type,correctAuthentication.getBytes()); 95 | System.out.println(LOG_PREFIX_OF_MAIN + "开始连接ZK服务器"); 96 | //倒数等待 97 | connectedSemaphore.await(); 98 | } catch (Exception e) { 99 | e.printStackTrace(); 100 | } 101 | } 102 | 103 | /** 104 | * 关闭ZK连接 105 | */ 106 | public void releaseConnection() { 107 | if (this.zk!=null) { 108 | try { 109 | this.zk.close(); 110 | } catch (InterruptedException e) { 111 | } 112 | } 113 | } 114 | 115 | /** 116 | * 117 | * 方法名称:测试函数
118 | * 概要说明:测试认证
119 | * @param args 120 | * @throws Exception 121 | */ 122 | public static void main(String[] args) throws Exception { 123 | 124 | ZookeeperAuth testAuth = new ZookeeperAuth(); 125 | testAuth.createConnection(CONNECT_ADDR,2000); 126 | List acls = new ArrayList(1); 127 | for (ACL ids_acl : Ids.CREATOR_ALL_ACL) { 128 | acls.add(ids_acl); 129 | } 130 | 131 | try { 132 | zk.create(PATH, "init content".getBytes(), acls, CreateMode.PERSISTENT); 133 | System.out.println("使用授权key:" + correctAuthentication + "创建节点:"+ PATH + ", 初始内容是: init content"); 134 | } catch (Exception e) { 135 | e.printStackTrace(); 136 | } 137 | try { 138 | zk.create(PATH_DEL, "will be deleted! ".getBytes(), acls, CreateMode.PERSISTENT); 139 | System.out.println("使用授权key:" + correctAuthentication + "创建节点:"+ PATH_DEL + ", 初始内容是: init content"); 140 | } catch (Exception e) { 141 | e.printStackTrace(); 142 | } 143 | 144 | // 获取数据 145 | getDataByNoAuthentication(); 146 | getDataByBadAuthentication(); 147 | getDataByCorrectAuthentication(); 148 | 149 | // 更新数据 150 | updateDataByNoAuthentication(); 151 | updateDataByBadAuthentication(); 152 | updateDataByCorrectAuthentication(); 153 | 154 | // 删除数据 155 | deleteNodeByBadAuthentication(); 156 | deleteNodeByNoAuthentication(); 157 | deleteNodeByCorrectAuthentication(); 158 | // 159 | Thread.sleep(1000); 160 | 161 | deleteParent(); 162 | //释放连接 163 | testAuth.releaseConnection(); 164 | } 165 | /** 获取数据:采用错误的密码 */ 166 | static void getDataByBadAuthentication() { 167 | String prefix = "[使用错误的授权信息]"; 168 | try { 169 | ZooKeeper badzk = new ZooKeeper(CONNECT_ADDR, 2000, null); 170 | //授权 171 | badzk.addAuthInfo(authentication_type,badAuthentication.getBytes()); 172 | Thread.sleep(2000); 173 | System.out.println(prefix + "获取数据:" + PATH); 174 | System.out.println(prefix + "成功获取数据:" + badzk.getData(PATH, false, null)); 175 | } catch (Exception e) { 176 | System.err.println(prefix + "获取数据失败,原因:" + e.getMessage()); 177 | } 178 | } 179 | 180 | /** 获取数据:不采用密码 */ 181 | static void getDataByNoAuthentication() { 182 | String prefix = "[不使用任何授权信息]"; 183 | try { 184 | System.out.println(prefix + "获取数据:" + PATH); 185 | ZooKeeper nozk = new ZooKeeper(CONNECT_ADDR, 2000, null); 186 | Thread.sleep(2000); 187 | System.out.println(prefix + "成功获取数据:" + nozk.getData(PATH, false, null)); 188 | } catch (Exception e) { 189 | System.err.println(prefix + "获取数据失败,原因:" + e.getMessage()); 190 | } 191 | } 192 | 193 | /** 采用正确的密码 */ 194 | static void getDataByCorrectAuthentication() { 195 | String prefix = "[使用正确的授权信息]"; 196 | try { 197 | System.out.println(prefix + "获取数据:" + PATH); 198 | 199 | System.out.println(prefix + "成功获取数据:" + zk.getData(PATH, false, null)); 200 | } catch (Exception e) { 201 | System.out.println(prefix + "获取数据失败,原因:" + e.getMessage()); 202 | } 203 | } 204 | 205 | /** 206 | * 更新数据:不采用密码 207 | */ 208 | static void updateDataByNoAuthentication() { 209 | 210 | String prefix = "[不使用任何授权信息]"; 211 | 212 | System.out.println(prefix + "更新数据: " + PATH); 213 | try { 214 | ZooKeeper nozk = new ZooKeeper(CONNECT_ADDR, 2000, null); 215 | Thread.sleep(2000); 216 | Stat stat = nozk.exists(PATH, false); 217 | if (stat!=null) { 218 | nozk.setData(PATH, prefix.getBytes(), -1); 219 | System.out.println(prefix + "更新成功"); 220 | } 221 | } catch (Exception e) { 222 | System.err.println(prefix + "更新失败,原因是:" + e.getMessage()); 223 | } 224 | } 225 | 226 | /** 227 | * 更新数据:采用错误的密码 228 | */ 229 | static void updateDataByBadAuthentication() { 230 | 231 | String prefix = "[使用错误的授权信息]"; 232 | 233 | System.out.println(prefix + "更新数据:" + PATH); 234 | try { 235 | ZooKeeper badzk = new ZooKeeper(CONNECT_ADDR, 2000, null); 236 | //授权 237 | badzk.addAuthInfo(authentication_type,badAuthentication.getBytes()); 238 | Thread.sleep(2000); 239 | Stat stat = badzk.exists(PATH, false); 240 | if (stat!=null) { 241 | badzk.setData(PATH, prefix.getBytes(), -1); 242 | System.out.println(prefix + "更新成功"); 243 | } 244 | } catch (Exception e) { 245 | System.err.println(prefix + "更新失败,原因是:" + e.getMessage()); 246 | } 247 | } 248 | 249 | /** 250 | * 更新数据:采用正确的密码 251 | */ 252 | static void updateDataByCorrectAuthentication() { 253 | 254 | String prefix = "[使用正确的授权信息]"; 255 | 256 | System.out.println(prefix + "更新数据:" + PATH); 257 | try { 258 | Stat stat = zk.exists(PATH, false); 259 | if (stat!=null) { 260 | zk.setData(PATH, prefix.getBytes(), -1); 261 | System.out.println(prefix + "更新成功"); 262 | } 263 | } catch (Exception e) { 264 | System.err.println(prefix + "更新失败,原因是:" + e.getMessage()); 265 | } 266 | } 267 | 268 | /** 269 | * 不使用密码 删除节点 270 | */ 271 | static void deleteNodeByNoAuthentication() throws Exception { 272 | 273 | String prefix = "[不使用任何授权信息]"; 274 | 275 | try { 276 | System.out.println(prefix + "删除节点:" + PATH_DEL); 277 | ZooKeeper nozk = new ZooKeeper(CONNECT_ADDR, 2000, null); 278 | Thread.sleep(2000); 279 | Stat stat = nozk.exists(PATH_DEL, false); 280 | if (stat!=null) { 281 | nozk.delete(PATH_DEL,-1); 282 | System.out.println(prefix + "删除成功"); 283 | } 284 | } catch (Exception e) { 285 | System.err.println(prefix + "删除失败,原因是:" + e.getMessage()); 286 | } 287 | } 288 | 289 | /** 290 | * 采用错误的密码删除节点 291 | */ 292 | static void deleteNodeByBadAuthentication() throws Exception { 293 | 294 | String prefix = "[使用错误的授权信息]"; 295 | 296 | try { 297 | System.out.println(prefix + "删除节点:" + PATH_DEL); 298 | ZooKeeper badzk = new ZooKeeper(CONNECT_ADDR, 2000, null); 299 | //授权 300 | badzk.addAuthInfo(authentication_type,badAuthentication.getBytes()); 301 | Thread.sleep(2000); 302 | Stat stat = badzk.exists(PATH_DEL, false); 303 | if (stat!=null) { 304 | badzk.delete(PATH_DEL, -1); 305 | System.out.println(prefix + "删除成功"); 306 | } 307 | } catch (Exception e) { 308 | System.err.println(prefix + "删除失败,原因是:" + e.getMessage()); 309 | } 310 | } 311 | 312 | /** 313 | * 使用正确的密码删除节点 314 | */ 315 | static void deleteNodeByCorrectAuthentication() throws Exception { 316 | 317 | String prefix = "[使用正确的授权信息]"; 318 | 319 | try { 320 | System.out.println(prefix + "删除节点:" + PATH_DEL); 321 | Stat stat = zk.exists(PATH_DEL, false); 322 | if (stat!=null) { 323 | zk.delete(PATH_DEL, -1); 324 | System.out.println(prefix + "删除成功"); 325 | } 326 | } catch (Exception e) { 327 | System.out.println(prefix + "删除失败,原因是:" + e.getMessage()); 328 | } 329 | } 330 | 331 | /** 332 | * 使用正确的密码删除节点 333 | */ 334 | static void deleteParent() throws Exception { 335 | try { 336 | Stat stat = zk.exists(PATH_DEL, false); 337 | if (stat == null) { 338 | zk.delete(PATH, -1); 339 | } 340 | } catch (Exception e) { 341 | e.printStackTrace(); 342 | } 343 | } 344 | 345 | } 346 | -------------------------------------------------------------------------------- /zookeeper/.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 13 | 14 | 15 | 16 | 17 | 18 | connectedSemaphore 19 | 20 | 21 | 22 | 25 | 26 | 27 | 45 | 46 | 47 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 |