Apache zookeeper Apache策展人领导者选举人:如何通过不退出takeLeadership()方法来避免放弃领导力?
我正在尝试实现一个简单的基于领导人选举的系统,其中应用程序的主要业务逻辑运行在选举的领导人节点上。作为获得领导地位的一部分,主业务逻辑启动各种其他服务。我正在使用ApacheCuratorLeaderSelectorRecipe来实现领导者选择过程 在我的系统中,被选为领导的节点保留领导权,直到失败迫使选择另一个领导。换句话说,一旦我获得了领导权,我就不想放弃它 根据馆长领导选举文档,当Apache zookeeper Apache策展人领导者选举人:如何通过不退出takeLeadership()方法来避免放弃领导力?,apache-zookeeper,apache-curator,leader,Apache Zookeeper,Apache Curator,Leader,我正在尝试实现一个简单的基于领导人选举的系统,其中应用程序的主要业务逻辑运行在选举的领导人节点上。作为获得领导地位的一部分,主业务逻辑启动各种其他服务。我正在使用ApacheCuratorLeaderSelectorRecipe来实现领导者选择过程 在我的系统中,被选为领导的节点保留领导权,直到失败迫使选择另一个领导。换句话说,一旦我获得了领导权,我就不想放弃它 根据馆长领导选举文档,当takeLeadership()方法返回时,领导权就会被放弃。我想避免它,我现在只是通过引入等待循环来阻止返回
takeLeadership()
方法返回时,领导权就会被放弃。我想避免它,我现在只是通过引入等待循环来阻止返回
我的问题是:
public class MainBusinessLogic extends LeaderSelectorListenerAdapter {
private static final String ZK_PATH_LEADER_ROOT = "/some-path";
private final CuratorFramework client;
private final LeaderSelector leaderSelector;
public MainBusinessLogic() {
client = CuratorService.getInstance().getCuratorFramework();
leaderSelector = new LeaderSelector(client, ZK_PATH_LEADER_ROOT, this);
leaderSelector.autoRequeue();
leaderSelector.start();
}
@Override
public void takeLeadership(CuratorFramework client) throws IOException {
// Start various other internal services...
ServiceA serviceA = new ServiceA(...);
ServiceB serviceB = new ServiceB(...);
...
...
serviceA.start();
serviceB.start();
...
...
// We are done but need to keep leadership to this instance, else all the business
// logic and services will start on another node.
// Is this the right way to prevent relinquishing leadership???
while (true) {
synchronized (this) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
LeaderLatch代替wait(),您只需执行以下操作:
Thread.currentThread().join();
但是,是的,这是正确的
顺便说一句,如果您喜欢另一种方法,您可以将LeaderLatch与LeaderLatchListener一起使用