2
votes

I just implemented a distibuted lock using Apache Curator und ZooKeeper in standalone mode. I initialzed the CuratorFramework as follows:

CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2182", retryPolicy);

Everything worked fine, so I tried to use ZooKeeper in cluster mode. I started three instances and initialzed the CuratorFramework as follows:

CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2182,localhost:2182,localhost:2183", retryPolicy);

As you can see, I just added the addresses of the two new nodes. So far so good.
But how do I initialize the client, when I don't know the addresses of each node respectively the size of the cluster, because I want to scale it dynamically?
I could initialize it by only specifying the address of the first node which will always be started. But if that node goes down, Curator loses the connection to the whole cluster (I just tried it).

2
If i remember correctly,curator can find available zookeeper node. - TongChen

2 Answers

0
votes

You should always know where your Zookeeper instances are. There's no way to connect to something when you don't know where it is - how could you?

If you can connect to any instance, you can get the configuration details and poll it regularly to keep your connection details up-to-date, perhaps?

maybe take a look at https://zookeeper.apache.org/doc/r3.5.5/zookeeperReconfig.html#ch_reconfig_rebalancing

0
votes

CuratorFrameworkFactory has a builder that allows you to specify an EnsembleProvider instead of a connectionString and to include an EnsembleTracker. This will keep your connectionString up to date, but you will need to persist the data somehow to ensure your application can find the ensemble when it restarts. I recommend implementing a decorating EnsembleProvider that encapsulates a FixedEnsembleProvider and writes the config to a properties file.

Example:

EnsembleProvider ensemble = new MyDecoratingEnsembleProvider(new FixedEnsembleProvider("localhost:2182", true));
CuratorFramework client = CuratorFrameworkFactory.builder()
    .ensembleProvider(ensemble)
    .retryPolicy(retryPolicy)
    .ensembleTracker(true)
    .build();