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:

/home/user/Desktop/zookeeper1,
/home/user/Desktop/zookeeper2, and
/home/user/Desktop/zookeeper3

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:

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/user/Desktop/zookeeperData1
clientPort=2181
server.1= localhost:2888:3888
server.2= localhost:2889:3889
server.3= localhost:2890:3890

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

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/user/Desktop/zookeeperData2
clientPort=2182
server.1= localhost:2888:3888
server.2= localhost:2889:3889
server.3= localhost:2890:3890

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

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/home/user/Desktop/zookeeperData3
clientPort=2183
server.1= localhost:2888:3888
server.2= localhost:2889:3889
server.3= localhost:2890:3890

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:

libraryDependencies ++= Seq(
"org.apache.curator" % "curator-framework" % "2.6.0",
"org.apache.curator" % "curator-recipes" % "2.6.0"
)

and use this to interact with the zookeeper server:


class ZookeeperClient {

private val logger = LoggerFactory.getLogger(this.getClass.getName)

def main(args: Array[String]) = {
val retryPolicy = new ExponentialBackoffRetry(1000, 3)
val curatorZookeeperClient = CuratorFrameworkFactory.newClient("localhost:2181,localhost:2182,localhost:2183", retryPolicy)
curatorZookeeperClient.start
curatorZookeeperClient.getZookeeperClient.blockUntilConnectedOrTimedOut
val znodePath = "/test_node"
val originalData = new String(curatorZookeeperClient.getData.forPath(znodePath)) // This should be "Some data"

/* Zookeeper NodeCache service to get properties from ZNode */
val nodeCache = new NodeCache(curatorZookeeperClient, znodePath)
nodeCache.getListenable.addListener(new NodeCacheListener {
@Override
def nodeChanged = {
try {
val dataFromZNode = nodeCache.getCurrentData
val newData = new String(currentData.getData) // This should be some new data after it is changed in the Zookeeper ensemble
} catch {
case ex: Exception => logger.error("Exception while fetching properties from zookeeper ZNode, reason " + ex.getCause)
}
}
nodeCache.start
})
}
}

This entry was posted in Java, Scala and tagged , , , , . Bookmark the permalink.

One Response to How to setup and use zookeeper in scala using Apache Curator

  1. Jay says:

    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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s