为了完成本关任务,你需要掌握: 1.ZooKeeper 的实例化; 2.实例化中不同参数的作用。
客户端连接ZooKeeper服务器
要连接 ZooKeeper 的服务器,实例化一个 ZooKeeper 对象即可,使用以下 API:
ZooKeeper(String connectString, int sessionTimeout, Watcher watcher)
其中 connectString 表示主机名和 ZooKeeper 服务器的端口。 sessionTimeout 表示 ZooKeeper 等待客户端通信的最长时间,该参数以毫秒为单位,一般设置超时时间为 5000-15000 毫秒。watcher 用于监听节点的状态变化,如果发生变化则通知此 watcher ,做出相应处理。如果不需要监听,则可设置为 null。
声明一个 ZooKeeper 的对象示例:
ZooKeeper zk = ("localhost:2181", 15000, null);
该对象会去连接本地 (localhost)zookeeper 服务器,设置会话超时时间为 15000 毫秒,且不监视节点状态变化。
断开ZooKeeper的连接
断开客户端和服务器之间的连接,直接使用实例化之后的 zk 对象,调用 close()方法即可,如下所示:
示例如下:
- // 创建方法 createZKInstance,用于连接 ZooKeeper 服务器
- private void createZKInstance() throws IOException {
- // 创建 zk 实例
- zk = new ZooKeeper("127.0.0.1:2181", 15000, null);
- }
-
- // 创建方法 ZKClose,用于关闭连接
- private void ZKClose() throws InterruptedException {
- zk.close();
- }
实现一个Watcher
为了从 ZooKeeper 接收消息,需要实现监视点。监视点主要是 Watcher 接口,该接口的定义如下:
- public interface Watcher{
- void process(WatcherEvent event);
- }
Watcher 接口中没有实现任何内容,因此,我们可以在 process 方法中实现自己的逻辑。
- public void process(WatchedEvent event) {
- System.out.println(event);
- }
在上面的代码中,我们简单的将事件输出,当然了,也可以根据实际需要,更改代码。
本节实训的示例程序及运行结果输出如下所示:
- import org.apache.zookeeper.WatchedEvent;
- import org.apache.zookeeper.Watcher;
- import org.apache.zookeeper.ZooKeeper;
- public class test1 {
- // 实现一个 Watcher
- private Watcher wh = new Watcher() {
- public void process(WatchedEvent event) {
- System.out.println(event);
- }
- };
-
- // 创建方法 createZKInstance,用于连接 ZooKeeper 服务器
- private void createZKInstance() throws IOException {
- // 创建 zk 实例
- zk = new ZooKeeper("127.0.0.1:2181", 15000, this.wh);
-
- }
-
- // 创建方法 ZKClose,用于关闭连接
- private void ZKClose() throws InterruptedException {
- // 关闭连接
- zk.close();
- }
-
- public static void main(String[] args) throws IOException, InterruptedException{
- // 调用 reateZKInstance 方法,连接 ZooKeeper 服务器
- reateZKInstance();
- // 调用 ZKClose 方法,关闭连接
- ZKClose();
- }
- }
执行结果:
WatchedEvent state:SyncConnected type:None path:null
该结果是 Watcher 监视器返回的结果,各个属性代表的意思会在后续讲解。
编程要求
仔细阅读右侧编辑区内给出的代码框架及注释,在 Begin-End 间编写程序代码,实现客户端连接 ZooKeeper 服务器(需要实现 Watcher 监视)和断开的功能。
- import java.io.IOException;
- import org.apache.zookeeper.WatchedEvent;
- import org.apache.zookeeper.Watcher;
- import org.apache.zookeeper.ZooKeeper;
- public class test1 {
- // 初始化 ZooKeeper 实例
- private ZooKeeper zk;
- // 实现一个 Watcher
- // 请在此处编写代码
- /********* Begin *********/
- private Watcher wh = new Watcher() {
- public void process(WatchedEvent event) {
- System.out.println(event);
- }
- };
- /********* End *********/
- // 连接 ZooKeeper 服务器
- private void createZKInstance() throws IOException {
- // 请在此处编写代码
- /********* Begin *********/
- zk = new ZooKeeper("127.0.0.1:2181", 15000, this.wh);
- /********* End *********/
- }
- // 断开连接
- private void ZKClose() throws InterruptedException {
- // 请在此处编写代码
- /********* Begin *********/
- zk.close();
- /********* End *********/
- }
- public static void main(String[] args) throws IOException, InterruptedException{
- test1 test = new test1();
- // 连接 ZooKeeper 服务器
- test.createZKInstance();
- // 断开连接
- test.ZKClose();
- }
- }