简体   繁体   English

如何为具有动态大小的 ZooKeeper 集群初始化 CuratorFramework?

[英]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.我刚刚在独立模式下使用 Apache Curator 和 ZooKeeper 实现了一个分布式锁。 I initialzed the CuratorFramework as follows:我初始化了 CuratorFramework 如下:

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

Everything worked fine, so I tried to use ZooKeeper in cluster mode.一切正常,所以我尝试在集群模式下使用 ZooKeeper。 I started three instances and initialzed the CuratorFramework as follows:我启动了三个实例并按如下方式初始化 CuratorFramework:

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).但是,如果该节点出现故障,Curator 将失去与整个集群的连接(我刚刚尝试过)。

You should always know where your Zookeeper instances are.您应该始终知道您的 Zookeeper 实例在哪里。 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也许看看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 . CuratorFrameworkFactory一个构建器,允许您指定EnsembleProvider而不是connectionString并包含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.这将使您的connectionString保持最新,但您需要以某种方式保留数据以确保您的应用程序在重新启动时可以找到该集合。 I recommend implementing a decorating EnsembleProvider that encapsulates a FixedEnsembleProvider and writes the config to a properties file.我建议实现一个装饰EnsembleProvider ,它封装了FixedEnsembleProvider并将配置写入属性文件。

Example:例子:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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