Installing Zookeepr on Windows
Step 1: Download Zookeeper from http://zookeeper.apache.org/. At the time of writing downloading zookeeper-3.4.11.tar.gz.
Step 2: Using 7-zip on windows unpack the gzipped tar file into a folder. E.g. c:\development\zookeeper-3.4.11. you can see “zkServer.cmd” in the bin folder for windows & “zkServer.sh” for Unix.
Starting the Zookeeper Server
Step 3: Copy the conf/zoo-sample.cfg to conf/zoo.cfg and the contents should look like the following for the standalone mode.
|
1 2 3 4 5 6 7 8 9 10 |
tickTime=2000 initLimit=10 syncLimit=5 dataDir=c:/data/zookeeper clientPort=2181 |
In production, you should run ZooKeeper in replicated mode. A replicated group of servers in the same application is called a quorum, and in replicated mode, all servers in the quorum have copies of the same configuration file. The file is similar to the one used in standalone mode, but with a few differences. Here is an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
tickTime=2000 initLimit=10 syncLimit=5 dataDir=c:/data/zookeeper clientPort=2181 server.1=localhost:2888:3888 server.2=localhost:2889:3889 server.3=localhost:2890:3890 |
two port numbers after each server name: ” 2888″ and “3888” means the former is used to connect to other peers. More specifically, a ZooKeeper server uses this port to connect followers to the leader. When a new leader arises, a follower opens a TCP connection to the leader using this port.
Step 4: Open a DOS command prompt run the zkserver with the standalone mode config.
|
1 2 3 4 |
cd c:\development\zookeeper-3.4.11\bin c:\development\zookeeper-3.4.11\bin>zkServer.cmd |
This will start the Zookeeper server with a single node.
If you want to use a replicated mode, then you need to create a “myid” file in “c:/data/zookeeper”, and also separate dataDirs and distinct clientPorts are also necessary. Running on a single localhost requires three config files.
Connecting to ZooKeeper
Once ZooKeeper server is running, clients can connect to it.
1. Open a new DOS command-line and type
|
1 2 3 4 |
cd c:\development\zookeeper-3.4.11\bin c:\development\zookeeper-3.4.11\bin>zkcli localhost:2181 |
2. You can use the Java programming API
Step 1: Firstly, the pom.xml file needs to have the Zookeeper dependency.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<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.mytutorial</groupId> <artifactId>myproject1</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>myproject1</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.11</version> </dependency> </dependencies> </project> |
Step 2: The Java program using the Zookeeper API.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
package com.zoo; import java.time.LocalDateTime; import java.util.List; import java.util.concurrent.CountDownLatch; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.Watcher.Event.KeeperState; import org.apache.zookeeper.ZooDefs.Ids; import org.apache.zookeeper.ZooKeeper; public class ZkConnect { private ZooKeeper zk; private CountDownLatch connSignal = new CountDownLatch(1); //host should be 127.0.0.1:2181 public ZooKeeper connect(String host) throws Exception { zk = new ZooKeeper(host, 3000, new Watcher() { public void process(WatchedEvent event) { if (event.getState() == KeeperState.SyncConnected) { connSignal.countDown(); } } }); connSignal.await(); return zk; } public void close() throws InterruptedException { zk.close(); } public void createNode(String path, byte[] data) throws Exception { zk.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } public void updateNode(String path, byte[] data) throws Exception { zk.setData(path, data, zk.exists(path, true).getVersion()); } public void deleteNode(String path) throws Exception { zk.delete(path, zk.exists(path, true).getVersion()); } public static void main (String args[]) throws Exception { ZkConnect connector = new ZkConnect(); ZooKeeper zk = connector.connect("localhost"); String newNode = "/test-"+ LocalDateTime.now(); connector.createNode(newNode, "Initial data: ABCD".getBytes()); List<String> zNodes = zk.getChildren("/", true); for (String zNode: zNodes) { System.out.println("ChildrenNode " + zNode); } byte[] data = zk.getData(newNode, true, zk.exists(newNode, true)); System.out.println("GetData before setting"); for ( byte dataPoint : data) { System.out.print ((char)dataPoint); } System.out.println("\nGetData after setting"); connector.updateNode(newNode, "Modified data: WXYZ".getBytes()); data = zk.getData(newNode, true, zk.exists(newNode, true)); for ( byte dataPoint : data) { System.out.print ((char)dataPoint); } connector.deleteNode(newNode); } } |
Step 3: Make sure that the zkserver is running via a DOS command line as explained earlier. Now run ZkConnect as a Java application.
|
1 2 3 4 5 6 7 |
ChildrenNode zookeeper ChildrenNode test-2018-02-18T15:39:55.544 GetData before setting Initial data: ABCD GetData after setting Modified data: WXYZ |