Zookeeper provides a Java client API. This blog focuses on creating conversations.
Create project
First, create a simple java project based on maven management. Introduce zookeeper in the pom file.
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
</dependency>
Writing test classes
The preferred example is the simplest API.
public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher)
Connecting String represents a list of zookeeper server addresses to be connected in 192.168.0.1:2181. Support multiple address splicing, separated by commas. The operation path of zookeeper can also be spliced after the address, such as 192.168.0.1:2181/zk/test.
Session Timeout: Session timeout in milliseconds. Monitor the effectiveness of conversations through heartbeat.
Watcher: Monitor the state change of the node, notify the watcher if it changes, and make corresponding processing. If you don't need to listen, you can set it to null.
Test code:
package com.secbro.learn;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
/**
* Created by zhuzs on 2017/3/9.
*/
public class TestSession implements Watcher{
private static CountDownLatch countDownLatch = new CountDownLatch(1);
public static void main(String[] args) throws IOException {
Long startTime = new Date().getTime();
ZooKeeper zooKeeper = new ZooKeeper("192.168.0.1:2181",5000,new TestSession());
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Creating connections takes time:" + (new Date().getTime() - startTime) + "ms");
System.out.println("Connection status:" + zooKeeper.getState());
}
public void process(WatchedEvent event) {
System.out.println("Receive watcher event:" + event);
if(Event.KeeperState.SyncConnected == event.getState()){
countDownLatch.countDown();
}
}
}
Because Zookeeper client and server creation sessions are asynchronous processes, CountDownLatch is used to block threads, wait for the server creation to complete, and send event notifications.
The printing results are as follows:
Receive watcher event:WatchedEvent state:SyncConnected type:None path:null Creating connections takes time: 9155ms Connection status: CONNECTED
Other interfaces
public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher,
boolean canBeReadOnly)
This method adds a canBeReadOnly parameter, which indicates whether the current session supports "read-only" mode.
public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher,
long sessionId, byte[] sessionPasswd)
This method allows sessionId and session Passwd to be passed in, in order to reuse the session. Obtained by the following methods:
zooKeeper.getSessionId();
zooKeeper.getSessionPasswd()
Then create a new connection as a parameter. When session Id and session Passwd are incorrect, the server returns Expired events.