使用的 IDE 为 IntelliJ IDEA,创建一个 maven 工程,命名为 zookeeper-demo,并且引入如下依赖,可以自行在maven*仓库选择合适的版本,介绍原生 API 和 Curator 两种方式。
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.8</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> <version>4.0.0</version> </dependency>
一、客户端的 zookeeper 原生 API 使用 zookeeper 原生 API,连接上一教程搭建的三台服务组成的集群,因为连接需要时间,用 countDownLatch 阻塞,等待连接成功,控制台输出连接状态! 实例 public static void main(String[] args) { try { final CountDownLatch countDownLatch=new CountDownLatch(1); ZooKeeper zooKeeper= new ZooKeeper("192.168.3.33:2181," + "192.168.3.35:2181,192.168.3.37:2181", 4000, new Watcher() { @Override public void process(WatchedEvent event) { if(Event.KeeperState.SyncConnected==event.getState()){ //如果收到了服务端的响应事件,连接成功 countDownLatch.countDown(); } } }); countDownLatch.await(); //CONNECTED System.out.println(zooKeeper.getState()); } }
简单示例添加节点 API: zooKeeper.create("/runoob","0".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
二、客户端的curator连接 Curator 是 Netflix 公司开源的一套 zookeeper 客户端框架,解决了很多 Zookeeper 客户端非常底层的细节开发工作,包括连接重连、反复注册 Watcher 和 NodeExistsException 异常等。 Curator 包含了几个包: curator-framework:对 zookeeper 的底层 api 的一些封装。 curator-client:提供一些客户端的操作,例如重试策略等。 curator-recipes:封装了一些高级特性,如:Cache 事件监听、选举、分布式锁、分布式计数器、分布式 Barrier 等。 实例 public class CuratorDemo { public static void main(String[] args) throws Exception { CuratorFramework curatorFramework=CuratorFrameworkFactory. builder().connectString("192.168.3.33:2181," + "192.168.3.35:2181,192.168.3.37:2181"). sessionTimeoutMs(4000).retryPolicy(new ExponentialBackoffRetry(1000,3)). namespace("").build(); curatorFramework.start(); Stat stat=new Stat(); //查询节点数据 byte[] bytes = curatorFramework.getData().storingStatIn(stat).forPath("/runoob"); System.out.println(new String(bytes)); curatorFramework.close(); } }
curator 相关参考链接: http://curator.apache.org/。
package com.runoob.zookeeper; import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import java.io.IOException; import java.util.concurrent.CountDownLatch; public class ConnectionDemo { public static void main(String[] args) { try { final CountDownLatch countDownLatch=new CountDownLatch(1); ZooKeeper zooKeeper= new ZooKeeper("192.168.3.33:2181," + "192.168.3.35:2181,192.168.3.37:2181", 4000, new Watcher() { @Override public void process(WatchedEvent event) { if(Event.KeeperState.SyncConnected==event.getState()){ //如果收到了服务端的响应事件,连接成功 countDownLatch.countDown(); } } }); countDownLatch.await(); //CONNECTED System.out.println(zooKeeper.getState()); //添加节点 zooKeeper.create("/runoob","0".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (KeeperException e) { e.printStackTrace(); } } }
package com.runoob.zookeeper; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.zookeeper.data.Stat; public class CuratorDemo { public static void main(String[] args) throws Exception { CuratorFramework curatorFramework=CuratorFrameworkFactory. builder().connectString("192.168.3.33:2181," + "192.168.3.35:2181,192.168.3.37:2181"). sessionTimeoutMs(4000).retryPolicy(new ExponentialBackoffRetry(1000,3)). namespace("").build(); curatorFramework.start(); Stat stat=new Stat(); byte[] bytes = curatorFramework.getData().storingStatIn(stat).forPath("/runoob"); System.out.println(new String(bytes)); curatorFramework.close(); } }