6

How to setup and use zookeeper in scala using Apache Curator

 3 years ago
source link: https://blog.knoldus.com/how-to-setup-and-use-zookeeper-in-scala-using-apache-curator/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to setup and use zookeeper in scala using Apache Curator

Reading Time: 2 minutes

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:

xxxxxxxxxx
/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:

xxxxxxxxxx
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:

xxxxxxxxxx
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:

xxxxxxxxxx
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:

xxxxxxxxxx
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:

xxxxxxxxxx
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
})
}
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK