How to setup and use zookeeper in scala using Apache Curator

In order to use Zookeeper to manage your project’s configurations across the cluster, first we will setup the zookeeper ensemble on our local machine (setup is for testing on a single machine) by following these steps:

1) Download a stable zookeeper release

2) Unpack it at three places and rename it to:

3) In order to use zookeeper we will need to setup configuration files for all servers.

Make a new file zoo.cfg,
/home/user/Desktop/zookeeper1/conf/zoo.cfg

and add following details:

Similarly,
/home/user/Desktop/zookeeper2/conf/zoo.cfg, as:

And,
/home/user/Desktop/zookeeper3/conf/zoo.cfg, as:

4) Now we will have to define each server’s id by making a new file in:
/home/user/Desktop/zookeeperData1/myid
which should have: 1
/home/user/Desktop/zookeeperData2/myid
which should have: 2
/home/user/Desktop/zookeeperData3/myid
which should have: 3

5) Next, we will start zookeeper ensemble for each server in 3 different terminals:

cd /home/user/Desktop/zookeeper1
bin/zkServer.sh start

cd /home/user/Desktop/zookeeper2
bin/zkServer.sh start

cd /home/user/Desktop/zookeeper3
bin/zkServer.sh start

6) Now we will add some data in one of the ZNode of the zookeeper ensemble by following steps:

a) bin/zkCli.sh
b) create /test_node “Some data”

7) Then we will write the following code in order to setup a watcher for zookeeper node so as to get stored data from zookeeper server using apache curator as a library to interact with our zookeeper server.

Add the following dependency in your build.sbt file:

and use this to interact with the zookeeper server:

Written by 

Sidharth is a Lead Consultant, having experience of more than 4.5 years. He has started working on Scala and Clojure and is actively involved in other developmental work. He enjoys working in a team and believes that knowledge is something that should be shared openly and on a large scale. As an avid gamer and passionate player, he likes to be involved in both indoor and outdoor activities.

1 thought on “How to setup and use zookeeper in scala using Apache Curator

  1. If I want to automate server additions and dynamically add them to Zookeeper’s ensemble, then measure the time it takes for all servers to recognize each other, is Curator or Kazoo the right tools to use?

Leave a Reply

%d bloggers like this: