Java實現ZooKeeper的zNode監控

程式猿講故事發表於2019-08-15

上一篇文章已經完成了ZooKeeper的基本搭建和使用的介紹,現在開始用程式碼說話。參考 https://zookeeper.apache.org/doc/current/javaExample.html ,但對場景和程式碼都做了簡化,只實現基本的Watcher功能。

1   場景設計

目的是體驗ZooKeeper的Watcher功能。程式監控ZooKeeper的/watcher節點資料變化,當建立或修改資料時,控制檯列印當前的資料內容和版本號;當/watcher被刪除時,程式退出。

/watcher的建立、修改和刪除操作,使用控制檯或zkui操作。

2   搭建Maven專案

程式碼相對比較簡單,就不用SpringBoot這個大殺器了,使用一個普通的Maven專案即可。

ZooKeeper客戶端使用官方提供的Java庫,org.apache.zookeeper: zookeeper: 3.5.5。日誌框架使用習慣的slf4j+log4j2,ZooKeeper預設使用的是log4j V1,因此在引入的時候需要excludes。另外,使用了lombok來簡化一些程式碼。

以下是pom.xml的內容

<?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>tech.codestory.research</groupId>

  <artifactId>zookeeper</artifactId>

  <version>1.0.0-SNAPSHOT</version>

  <dependencies>

    <dependency>

      <groupId>org.apache.zookeeper</groupId>

      <artifactId>zookeeper</artifactId>

      <version>3.5.5</version>

      <exclusions>

        <exclusion>

          <groupId>log4j</groupId>

          <artifactId>log4j</artifactId>

        </exclusion>

        <exclusion>

          <groupId>org.slf4j</groupId>

          <artifactId>slf4j-log4j12</artifactId>

        </exclusion>

      </exclusions>

    </dependency>

    <dependency>

      <groupId>org.apache.logging.log4j</groupId>

      <artifactId>log4j-core</artifactId>

      <version>2.12.1</version>

    </dependency>

    <dependency>

      <groupId>org.apache.logging.log4j</groupId>

      <artifactId>log4j-api</artifactId>

      <version>2.12.1</version>

    </dependency>

    <dependency>

      <groupId>org.apache.logging.log4j</groupId>

      <artifactId>log4j-web</artifactId>

      <version>2.12.1</version>

    </dependency>

    <dependency>

      <groupId>org.apache.logging.log4j</groupId>

      <artifactId>log4j-slf4j-impl</artifactId>

      <version>2.12.1</version>

    </dependency>

    <dependency>

      <groupId>org.slf4j</groupId>

      <artifactId>slf4j-api</artifactId>

      <version>1.7.28</version>

    </dependency>

    <dependency>

      <groupId>org.slf4j</groupId>

      <artifactId>slf4j-ext</artifactId>

      <version>1.7.28</version>

    </dependency>

    <dependency>

      <groupId>org.projectlombok</groupId>

      <artifactId>lombok</artifactId>

      <version>1.18.8</version>

      <scope>provided</scope>

    </dependency>

  </dependencies>

</project>

 

3   log4j2.xml

在專案的 src/main/resources 下建立一個檔案 log4j2.xml,內容為

<?xml version="1.0" encoding="UTF-8"?>

<configuration status="DEBUG" name="codeStoryLogger">

  <appenders>

    <Console name="stdout">

      <ThresholdFilter level="TRACE" onMatch="ACCEPT" onMismatch="DENY"/>

      <PatternLayout pattern="%d{HH:mm:ss:SSS} [%p] - %c{1}.%M(%L) - %m%n"/>

    </Console>

  </appenders>

  <ThresholdFilter level="TRACE"/>

  <loggers>

    <logger name="org.apache.zookeeper.ClientCnxn" level="ERROR" additivity="false">

      <appender-ref ref="stdout" />

    </logger>

    <logger name="org.apache.zookeeper" level="TRACE" additivity="false">

      <appender-ref ref="stdout"/>

    </logger>

    <logger name="tech.codestory" level="TRACE" additivity="false">

      <appender-ref ref="stdout"/>

    </logger>

    <root level="WARN">

      <appender-ref ref="stdout"/>

    </root>

  </loggers>

</configuration>

 

4   建立ZooKeeper連線

建立連線程式碼比較簡單,只需要建立 ZooKeeper物件就行,

ZooKeeper建構函式的定義

/**

 * 建立一個 ZooKeeper 客戶端物件

 * @param connectString 逗號分隔的 host:port 字串,

 *    單點如 127.0.0.1:2181,

 *    叢集如 192.168.5.128:2181,192.168.5.129:2181,192.168.5.130:2181,

 *    還可以指定根節點,如 127.0.0.1:2181/foo/bar

 * @param sessionTimeout 毫秒為單位的超時時間

 * @param watcher Watcher物件,用於接收 MatcherEvent

 * @throws IOException 網路錯誤時丟擲異常

 * @throws IllegalArgumentException 如果Root路徑設定錯誤

 */

public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher)

  throws IOException;

寫一段測試程式碼,建立zk物件後判斷某一個zNode是否存在。

public class ZooKeeperWatcher implements Watcher {

  /** ZooKeeper的客戶端連線 */

  ZooKeeper zk;

 

  public ZooKeeperWatcher(String hostPort, String zNode) throws KeeperException, IOException {

    zk = new ZooKeeper(hostPort, 3000, this);

    try {

      Stat exists = zk.exists(zNode, true);

      if(exists == null){

        log.info(“{} 不存在”, zNode)

      }

    } catch (InterruptedException e) {

      log.error("InterruptedException", e);

    }

  }

}

執行這段程式碼,發現會拋異常

java.net.SocketException: Socket is not connected

  at sun.nio.ch.Net.translateToSocketException(Net.java:162) ~[?:?]

  at sun.nio.ch.Net.translateException(Net.java:196) ~[?:?]

  at sun.nio.ch.Net.translateException(Net.java:202) ~[?:?]

  at sun.nio.ch.SocketAdaptor.shutdownInput(SocketAdaptor.java:400) ~[?:?]

  at org.apache.zookeeper.ClientCnxnSocketNIO.cleanup(ClientCnxnSocketNIO.java:198) [zookeeper-3.5.5.jar:3.5.5]

  at org.apache.zookeeper.ClientCnxn$SendThread.cleanup(ClientCnxn.java:1338) [zookeeper-3.5.5.jar:3.5.5]

  at org.apache.zookeeper.ClientCnxn$SendThread.cleanAndNotifyState(ClientCnxn.java:1276) [zookeeper-3.5.5.jar:3.5.5]

  at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1254) [zookeeper-3.5.5.jar:3.5.5]

Caused by: java.nio.channels.NotYetConnectedException

  at sun.nio.ch.SocketChannelImpl.shutdownInput(SocketChannelImpl.java:917) ~[?:?]

  at sun.nio.ch.SocketAdaptor.shutdownInput(SocketAdaptor.java:398) ~[?:?]

  ... 4 more

NotYetConnectedException的字面意思是連線還沒有建立好,網路搜尋了一下,建立連線需要一些時間,建立zk物件後馬上呼叫exists命令,這時候連線還沒有建立好,就會拋異常。ZooKeeper在連線建立成功之後,會傳送一個WatchedEvent事件,我們可以利用這個事件完成建立連線的過程。修改後的程式碼如下,順便新增了slf4j-ext中的Profiler,用於記錄所消耗的時間。

public class ZooKeeperWatcher implements Watcher {

  /** 等待連線建立成功的訊號 */

  private CountDownLatch connectedSemaphore = new CountDownLatch(1);

 

  /** ZooKeeper的客戶端連線 */

  ZooKeeper zk;

 

  public ZooKeeperWatcher(String hostPort, String zNode) throws KeeperException, IOException {

    Profiler profiler = new Profiler("連線到ZooKeeper");

    profiler.start("開始連結");

    zk = new ZooKeeper(hostPort, 3000, this);

    try {

          profiler.start("等待連線成功的Event");

      connectedSemaphore.await();

      Stat exists = zk.exists(zNode, true);

      if(exists == null){

        log.info(“{} 不存在”, zNode)

      }

    } catch (InterruptedException e) {

      log.error("InterruptedException", e);

    }

    profiler.stop();

    profiler.setLogger(log);

    profiler.log();

  }

 

  /** 收到ZooKeeper的WatchedEvent */

  @Override

  public void process(WatchedEvent event) {

    log.info("event = {}", event);

    if (Event.EventType.None.equals(event.getType())) {

      // 連線狀態發生變化

      if (Event.KeeperState.SyncConnected.equals(event.getState())) {

        // 連線建立成功

        connectedSemaphore.countDown();

      }

    }

  }

}

修改程式碼之後的執行記錄日誌如下,可以看到等待連線成功的Event耗時9秒多。網路上有文章說關閉防火牆可以秒連,但我測試過,沒發現有什麼變化,使用systemctl stop firewalld之後重新執行程式,仍然需要9秒多。

[INFO] - ZooKeeperWatcher.process(61) - event = WatchedEvent state:SyncConnected type:None path:null

[DEBUG] - ZooKeeperWatcher.log(201) -

+ Profiler [連線到ZooKeeper]

|-- elapsed time                   [開始連結]    78.912 milliseconds.

|-- elapsed time           [等待連線成功的Event]  9330.606 milliseconds.

|-- Total                  [連線到ZooKeeper]  9409.926 milliseconds.

 

[INFO] - ZooKeeperWatcher.readNodeData(95) - /watcher 不存在

 

5   讀取WatchedEvent

前面的程式碼,只是處理了建立連線成功時的Event,下面再來看看讀取資料的過程。關鍵程式碼如下:

if (Event.EventType.NodeDataChanged.equals(event.getType())

    || Event.EventType.NodeCreated.equals(event.getType())) {

  String path = event.getPath();

  if (path != null && path.equals(zNode)) {

    // 節點資料被修改

    readNodeData();

  }

}

 

/** 讀節點資料 */

private void readNodeData() {

  try {

    Stat stat = new Stat();

    byte[] data = zk.getData(zNode, true, stat);

    if (data != null) {

      log.info("{}, value={}, version={}", zNode, new String(data), stat.getVersion());

    }

  } catch (KeeperException e) {

    log.info("{} 不存在", zNode);   

  } catch (InterruptedException e) {

    log.error("InterruptedException", e);

  }

}

當接收到建立節點和修改節點的WatchedEvent,都會將資料讀出並列印在控制檯。

6   調整後的完整程式清單

對前面的程式碼做了部分調整,同時新增了退出系統的機制:節點被刪除。

package tech.codestory.zookeeper.watcher;

 

import java.io.IOException;

import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.*;

import org.apache.zookeeper.data.Stat;

import org.slf4j.profiler.Profiler;

import lombok.extern.slf4j.Slf4j;

 

/**

 * 用於測試 ZooKeeper的 WatchedEvent用法

 * @author code story

 * @date 2019/8/13

 */

@Slf4j

public class ZooKeeperWatcher implements Watcher, Runnable {

  /** 等待連線建立成功的訊號 */

  private CountDownLatch connectedSemaphore = new CountDownLatch(1);

  /** 退出系統的訊號 */

  static Integer quitSemaphore = Integer.valueOf(-1);

 

  String zNode;

  ZooKeeper zk;

 

  public ZooKeeperWatcher(String hostPort, String zNode) throws KeeperException, IOException {

    this.zNode = zNode;

 

    Profiler profiler = new Profiler("連線到ZooKeeper");

    profiler.start("開始連結");

    zk = new ZooKeeper(hostPort, 3000, this);

    try {

      profiler.start("等待連線成功的Event");

      connectedSemaphore.await();

    } catch (InterruptedException e) {

      log.error("InterruptedException", e);

    }

    profiler.stop();

    profiler.setLogger(log);

    profiler.log();

 

    // 先讀當前的資料

    readNodeData();

  }

 

  /** 收到ZooKeeper的WatchedEvent */

  @Override

  public void process(WatchedEvent event) {

    log.info("event = {}", event);

    if (Event.EventType.None.equals(event.getType())) {

      // 連線狀態發生變化

      if (Event.KeeperState.SyncConnected.equals(event.getState())) {

        // 連線建立成功

        connectedSemaphore.countDown();

      }

    } else if (Event.EventType.NodeDataChanged.equals(event.getType())

        || Event.EventType.NodeCreated.equals(event.getType())) {

      String path = event.getPath();

      if (path != null && path.equals(zNode)) {

        // 節點資料被修改

        readNodeData();

      }

    } else if (Event.EventType.NodeDeleted.equals(event.getType())) {

      String path = event.getPath();

      if (path != null && path.equals(zNode)) {

        synchronized (quitSemaphore) {

          // 節點被刪除,通知退出執行緒

          quitSemaphore.notify();

        }

      }

    }

  }

 

  /** 讀節點資料 */

  private void readNodeData() {

    try {

      Stat stat = new Stat();

      byte[] data = zk.getData(zNode, true, stat);

      if (data != null) {

        log.info("{}, value={}, version={}", zNode, new String(data), stat.getVersion());

      }

    } catch (KeeperException e) {

      log.info("{} 不存在", zNode);

      try {

        // 目的是新增Watcher

        zk.exists(zNode, true);

      } catch (KeeperException ex) {

      } catch (InterruptedException ex) {

      }

    } catch (InterruptedException e) {

      log.error("InterruptedException", e);

    }

  }

 

  @Override

  public void run() {

    try {

      synchronized (quitSemaphore) {

        quitSemaphore.wait();

        log.info("{} 被刪除,退出", zNode);

      }

    } catch (InterruptedException e) {

      log.error("InterruptedException", e);

    }

  }

 

  /** 主程式,程式碼中寫死了Server地址和zNode名,也可以改成從args中讀取 */

  public static void main(String[] args) {

    String hostPort = "192.168.5.128:2181";

    String zNode = "/watcher";

    try {

      new ZooKeeperWatcher(hostPort, zNode).run();

    } catch (Exception e) {

      log.error("new ZooKeeperExecutor()", e);

    }

  }

}

做一個測試,應用啟動後建立節點,修改多次節點,最後刪除節點,日誌輸出如下:

10:13:31:979 [INFO] - ZooKeeperWatcher.process(50) - event = WatchedEvent state:SyncConnected type:None path:null

10:13:31:982 [DEBUG] - ZooKeeperWatcher.log(201) -

+ Profiler [連線到ZooKeeper]

|-- elapsed time                   [開始連結]   210.193 milliseconds.

|-- elapsed time           [等待連線成功的Event]  9385.467 milliseconds.

|-- Total                  [連線到ZooKeeper]  9596.196 milliseconds.

 

10:13:31:996 [INFO] - ZooKeeperWatcher.readNodeData(84) - /watcher 不存在

10:15:43:451 [INFO] - ZooKeeperWatcher.process(50) - event = WatchedEvent state:SyncConnected type:NodeCreated path:/watcher

10:15:43:463 [INFO] - ZooKeeperWatcher.readNodeData(81) - /watcher, value=hello zk 00, version=0

10:15:50:906 [INFO] - ZooKeeperWatcher.process(50) - event = WatchedEvent state:SyncConnected type:NodeDataChanged path:/watcher

10:15:50:910 [INFO] - ZooKeeperWatcher.readNodeData(81) - /watcher, value=hello zk 01, version=1

10:15:56:004 [INFO] - ZooKeeperWatcher.process(50) - event = WatchedEvent state:SyncConnected type:NodeDataChanged path:/watcher

10:15:56:007 [INFO] - ZooKeeperWatcher.readNodeData(81) - /watcher, value=hello zk 02, version=2

10:16:00:246 [INFO] - ZooKeeperWatcher.process(50) - event = WatchedEvent state:SyncConnected type:NodeDataChanged path:/watcher

10:16:00:249 [INFO] - ZooKeeperWatcher.readNodeData(81) - /watcher, value=hello zk 03, version=3

10:16:06:399 [INFO] - ZooKeeperWatcher.process(50) - event = WatchedEvent state:SyncConnected type:NodeDataChanged path:/watcher

10:16:06:402 [INFO] - ZooKeeperWatcher.readNodeData(81) - /watcher, value=hello zk 10, version=4

10:16:10:217 [INFO] - ZooKeeperWatcher.process(50) - event = WatchedEvent state:SyncConnected type:NodeDataChanged path:/watcher

10:16:10:220 [INFO] - ZooKeeperWatcher.readNodeData(81) - /watcher, value=hello zk 11, version=5

10:16:14:444 [INFO] - ZooKeeperWatcher.process(50) - event = WatchedEvent state:SyncConnected type:NodeDataChanged path:/watcher

10:16:14:447 [INFO] - ZooKeeperWatcher.readNodeData(81) - /watcher, value=hello zk 12, version=6

10:16:20:118 [INFO] - ZooKeeperWatcher.process(50) - event = WatchedEvent state:SyncConnected type:NodeDeleted path:/watcher

10:16:20:118 [INFO] - ZooKeeperWatcher.run(101) - /watcher 被刪除,退出

相關文章