JavaAPI操作Zookeeper
-
首先创建一个maven项目,并编写pom文件,这里需要导入对zookeeper操作的
curator-client
和curator-recipes
版本
jdk — 8u261
Zookeeper — 3.4.9
Curator-client — 2.13.0
curator-recipes — 2.13.0
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.huyue.zookeeper</groupId> <artifactId>Zookeeper-JavaAPI</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.13.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>2.13.0</version> </dependency> <dependency> <groupId>com.google.collections</groupId> <artifactId>google-collections</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.25</version> </dependency> </dependencies> <build> <plugins> <!-- java编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
-
创建针对于zookeeper真删改查的方法,创建测试类来实现这些方法。
package com.huyue.zookeeper; import org.apache.curator.RetryPolicy; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.recipes.cache.ChildData; import org.apache.curator.framework.recipes.cache.TreeCache; import org.apache.curator.framework.recipes.cache.TreeCacheEvent; import org.apache.curator.framework.recipes.cache.TreeCacheListener; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.zookeeper.CreateMode; import org.junit.Test; public class ZookeeperAPITest { /** * 功能描述: 创建临时节点 * @param * @return {@link } * @author HuYueeer * @date 2020/11/22 9:10 下午 */ @Test public void createTmpZnode() throws Exception { RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,1); String connectionStr = "120.53.228.112:2181"; CuratorFramework client = CuratorFrameworkFactory.newClient(connectionStr,3000,3000,retryPolicy); client.start(); client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/hello4","world".getBytes()); Thread.sleep(8000); client.close(); } /** * 功能描述: 创建节点 * @param * @return {@link } * @author HuYueeer * @date 2020/11/22 9:10 下午 */ @Test public void createZnode() throws Exception { /** * 1.定制重试策略 * 2.获取客户端 * 3.开启客户端 * 4.创建节点 * 5.关闭客户端 * */ /* param1:重试的间隔时间 param2:重试的最大次数 */ RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,1); String connectionStr = "120.53.228.113:2181"; /* param1:要连接的Zookeeper服务器列表 param2:会话的超时时间 param3:链接超时时间 param4:重试策略 */ CuratorFramework client = CuratorFrameworkFactory.newClient(connectionStr,3000,3000,retryPolicy); client.start(); client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/hello3","world".getBytes()); client.close(); } /** * 功能描述: 修改节点数据 * @param * @return {@link } * @author HuYueeer * @date 2020/11/22 9:17 下午 */ @Test public void setNodeData() throws Exception{ RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,1); String connectionStr = "120.53.228.114:2181"; CuratorFramework client = CuratorFrameworkFactory.newClient(connectionStr,3000,3000,retryPolicy); client.start(); client.setData().forPath("/hello3","worldxxx".getBytes()); client.close(); } /** * 功能描述: 获取节点中的值 * @param * @return {@link } * @author HuYueeer * @date 2020/11/22 9:27 下午 */ @Test public void getNodeData() throws Exception{ RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,1); String connectionStr = "120.53.228.115:2181"; CuratorFramework client = CuratorFrameworkFactory.newClient(connectionStr,3000,3000,retryPolicy); client.start(); byte[] forPath = client.getData().forPath("/hello3"); System.out.println(new String(forPath)); client.close(); } /** * 功能描述: 删除节点 * @param * @return {@link } * @author HuYueeer * @date 2020/11/22 9:27 下午 */ @Test public void deleteNode() throws Exception{ RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,1); String connectionStr = "120.53.228.119:2181"; CuratorFramework client = CuratorFrameworkFactory.newClient(connectionStr,3000,3000,retryPolicy); client.start(); client.delete().forPath("/hello2"); client.close(); } /** * 功能描述: zookeeper的watch机制 * @param * @return {@link } * @author HuYueeer * @date 2020/11/22 9:43 下午 */ @Test public void nodeWatch() throws Exception{ RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,1); String connectionStr = "120.53.228.110:2181"; CuratorFramework client = CuratorFrameworkFactory.newClient(connectionStr,3000,3000,retryPolicy); client.start(); //创建一个TreeCache对象,指定要监控的节点路径 TreeCache treeCache = new TreeCache(client, "/hello3"); //自定义一个监听器 treeCache.getListenable().addListener(new TreeCacheListener() { @Override public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) throws Exception { ChildData data = treeCacheEvent.getData(); if(data != null){ switch (treeCacheEvent.getType()){ case NODE_ADDED: System.out.println("发现新增节点!"); break; case NODE_REMOVED: System.out.println("发现移除节点!"); break; case NODE_UPDATED: System.out.println("发现更新节点!"); break; default: break; } } } }); //开启监视 treeCache.start(); //监听在于此watch进程一直进行 Thread.sleep(100000); } }