Blog

Apache Zookeeper as Configuration Manager

August 4, 2018

Blog

Zookeeper website defines it as:

ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services.

There have been many debates on using a dedicated configuration server against having a config file for managing the configurations. One of the main argument against configuration servers is, having to manage additional application for just storing the configurations. This makes sense in smaller applications. However, one of the biggest challenges in a distributed environment with multiple applications is the difficulty to manage the configurations. In these scenarios, a configuration server/manager has very important role to play.

Zookeeper uses UNIX-like directory structure called as ZNodes for saving the data. ‘/’ is the parent ZNode, under which any structure can be created. Every ZNode can have some data associated with it, called as ZNode Properties (can be loosely related to directory and files in a file system). Clients can connect to the zookeeper server and request for the data by passing the full ZNode path.

Reasons for us to use Zookeeper as Config Server

  • Centralised Storage: All the required configurations are stored in a centralised location which makes it easy to manage
  • Clean Structure: ZNodes allow to logically arrange your configurations. eg: Global Config, Application wise config, environment wise config etc.
  • Dynamic configurations: It lets us change the configuration dynamically, without rebuilding the application.

Things to take care while using Zookeeper:

  • Single-Point-of-Failure: By using stand-alone zookeeper server, any failure can cause the whole system to stop. To avoid it, use the ensemble. Multiple instances of Zookeeper server can be setup and one of them will be selected as leader. If the majority of the nodes of an ensemble are running, the system will work fine.
  • Recommended size for any ZNode is up to 1MB. More than 1MB may cause performance issues.
  • Zookeeper is on slower side when it comes to writing operations. Reads are faster, while writes are slow. Too many frequent writes may not be optimal.

The below code snippet shows how to get the data from a zookeeper property.

123456def getValue(path: String) = {  val zk = createZKConnection(ZK_URL)  val result = zk.getData(getFullPath(path), false, new Stat)  closeZooKeeper(zk)  result}

The above method will return the zookeeper property value for the given path as Array[Byte]. We can use an implicit conversion to convert it to String.

123def getString(path: String) = {  getValue(path).asString    //implicit conversion from Array[Byte] to String value}
12345implicit class ZookeeperDataConverter(val nodeValue: Array[Byte]) {  def asString: String = new String(nodeValue)   def asInt: Int = new String(nodeValue).toInt}

Here is how we can use the getValue() method in our code.

12val prefix = s”config.$environment.”final val RECIPIENT_EMAIL_PATH = “emailTo”
123def getRecipientEmailId() = { ZookeeperHelper.getString(prefix + RECIPIENT_EMAIL_PATH)}

A very basic implementation for zookeeper as configuration server is uploaded in GitHub.

Zookeeper version used is: 3.4.9

Zookeeper installation and running steps are available on the official website(https://zookeeper.apache.org/doc/r3.1.2/zookeeperStarted.html).

For the sample code, I am running the zookeeper on my local machine as localhost

Related Post

15360

3

No Thoughts on Apache Zookeeper as Configuration Manager

Leave A Comment

© Copyright 2013- Reactore. All rights reserved.

Call Now

Wordpress Social Share Plugin powered by Ultimatelysocial