Apache Curator Leaderectorector:TakeLeadership()メソッドから出ていないことでリーダーシップをあきらめるのを避ける方法
-
21-12-2019 - |
質問
申請の主なビジネスロジックが選択されたリーダーノードで実行される単純なリーダー選挙ベースのシステムを実装しようとしています。リーダーシップを習得する一環として、主なビジネスロジックは他のさまざまなサービスを開始します。リーダー選択プロセスを実装するには、Apache CuratorのLeaderSelectorレシピを使用しています。
私のシステムでは、リーダーとして選択されるノードは、障害が他のリーダーを選択されるまでリーダーシップを保持します。言い換えれば、私がリーダーシップを手に入れたら、私はそれを放棄したくありません。
Curatorのリーダー選択のマニュアルによると、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(); } } } } }
解決
wait()のLeaderLatchInStead、あなたはただやることができます:
Thread.currentThread().join();
.
しかし、はい、それは正しいです。
BTW - 別の方法を好む場合は、LeadlatchListenerを持つリーダーラッチを使用できます。
所属していません StackOverflow