Skip to content

3. Host management

Pierre-Antoine Ganaye edited this page Jul 29, 2014 · 3 revisions

Add some hosts

You can simply add some new hosts while connected to a server by calling :

def addHosts(hostList: String): Unit

Example :

val client = Zookeeper.newRichClient("192.168.0.1:2181")
client.addHosts("192.168.0.10:2181,192.168.0.21:2181")

Remove some hosts

To remove some hosts from your server list, use def removeHosts(hostList: String): Future[Unit]. If you are trying to remove the host you are currently connected to, the client will try to find a new host, NoServerFound exception is thrown if no server is found.

Example :

val client = Zookeeper.newRichClient("192.168.0.1:2181,192.168.0.10:2181,192.168.0.21:2181")
client.removeHosts("192.168.0.10:2181,192.168.0.21:2181")

Change current host

While connected, you can decide to connect to a new server of the ensemble with the same session, this can be done by using def changeHost(host: Option[String] = None): Future[Unit]. You can provide a new host to connect to, or let the client choose one for you, if no host is found NoServerFound exception is thrown.

Use case: You decide to restart a ZooKeeper server for maintenance, problem you have a few clients connected to it, you could decide to shutdown and let the clients reconnect to a new server, but this is not very clean, instead you ask all your clients to connect to a new server. Example :

val client = Zookeeper.newRichClient("192.168.0.1:2181,192.168.0.10:2181,192.168.0.21:2181")
// with a specified existing host
client.changeHost(Some("192.168.0.10:2181"))
// with a specified new host
client.changeHost(Some("192.168.0.5:2181"))
// without specified host
client.changeHost()

Note : if you decide to connect to a host not included in the hostlist, it will be added to the list once connected to it.

Clone this wiki locally