How to programmatically detect which server in a ZooKeeper ensemble a client is connected to?
I'm using the Apache Curator API and I am listening for state changes in connection by registering ConnectionStateListener. I would like to know which server in the ensemble a client is connected to when the client reconnects if the server it was connected to goes down.
You can see this in the logs produced by Curator. In the example output below, the
CuratorFramework
client has been given 4 different ZooKeeper instances in theconnectionString
that it can connect to. As can be seen in the log, it choses the first:In case the ZooKeeper server that the client has connected to crashes, you will also see the new server that the client connects to in the logs: