简体   繁体   中英

How do I initialize a CuratorFramework for a ZooKeeper cluster with dynamic size?

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).

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

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM