JGroups:无需额外基础架构的领导人选举
嗨,您好,
在這篇文章中,我將展示如何在不使用任何其他基礎(chǔ)架構(gòu)(例如Apache Zookeeper或Consul)的情況下解決領(lǐng)導(dǎo)人選舉的問題。
領(lǐng)導(dǎo)者選舉是解決以下問題的一種常見方法:在分布式系統(tǒng)中,只有一個實例必須完成特定工作。
例如,這可能是
- 觸發(fā)計劃的工作
- 當系統(tǒng)充當外部系統(tǒng)的客戶端時,協(xié)調(diào)建立連接
- 運行清理任務(wù)
- …
通常,這可以通過使用提到的或類似的基礎(chǔ)結(jié)構(gòu)來解決。 但是,如果您不想將它們引入系統(tǒng)環(huán)境,則還有另一種方法: JGroups 。
JGroups是一個
用于可靠消息傳遞的工具包。 它可用于創(chuàng)建其節(jié)點可以相互發(fā)送消息的集群。
[http://jgroups.org/]
使用JGroups設(shè)置領(lǐng)導(dǎo)者,可以通過使用JGroups的View概念非常容易地進行選舉。
視圖是關(guān)于群集狀態(tài)的實際視圖(猜測是什么)。 由于每個集群都只有一個協(xié)調(diào)器,即視圖中成員列表的第一個成員,因此該協(xié)調(diào)器可以解釋為集群的領(lǐng)導(dǎo)者。
如果領(lǐng)導(dǎo)者死亡/重新啟動/出現(xiàn)故障,列表中的下一個成員將成為新領(lǐng)導(dǎo)者。 這是一種非常方便且確定性的方法。
例
讓我們看一些非常簡單的代碼,演示一下:
public class JGroupsLeaderElectionExample {private static final int MAX_ROUNDS = 1_000;private static final int SLEEP_TIME_IN_MILLIS = 1000;public static void main(String[] args) throws Exception {JChannel channel = new JChannel();channel.connect("The Test Cluster");for (int round = 0; round < MAX_ROUNDS; round++) {checkLeaderStatus(channel);sleep();}channel.close();}private static void sleep() {try {Thread.sleep(SLEEP_TIME_IN_MILLIS);} catch (InterruptedException e) {// Ignored}}private static void checkLeaderStatus(JChannel channel) {View view = channel.getView();Address address = view.getMembers().get(0);if (address.equals(channel.getAddress())) {System.out.println("I'm (" + channel.getAddress() + ") the leader");} else {System.out.println("I'm (" + channel.getAddress() + ") not the leader");}} } 上面的代碼使用默認的堆棧設(shè)置創(chuàng)建一個新的JChannel。 也可以使用XML文件或編程方法來配置堆棧。
然后將通道連接到“測試群集” 。 JGroups通過在連接上廣播發(fā)現(xiàn)集群。 如果當前實例是第一個,它將創(chuàng)建集群。
在循環(huán)中,代碼現(xiàn)在僅從通道獲取實際視圖,并檢查實際實例是否也是第一個成員。 您記得第一位成員可以被視為領(lǐng)導(dǎo)者。
如果現(xiàn)在啟動多個實例并停止第一個實例,則第二個將接管。 像這個例子:
------------------------------------------------------------------- GMS: address=Ygdrassil-21922, cluster=The Test Cluster, physical address=2003:4e:a904:2d67:55c:2653:7e28:8634:59721 ------------------------------------------------------------------- I'm (Ygdrassil-21922) the leader I'm (Ygdrassil-21922) the leader I'm (Ygdrassil-21922) the leader I'm (Ygdrassil-21922) the leader------------------------------------------------------------------- GMS: address=Ygdrassil-57947, cluster=The Test Cluster, physical address=2003:4e:a904:2d67:55c:2653:7e28:8634:59724 ------------------------------------------------------------------- I'm (Ygdrassil-57947) not the leader I'm (Ygdrassil-57947) not the leader I'm (Ygdrassil-57947) not the leader I'm (Ygdrassil-57947) not the leader I'm (Ygdrassil-57947) not the leader I'm (Ygdrassil-57947) the leader I'm (Ygdrassil-57947) the leader和往常一樣,可以在我的GitHub帳戶上找到該代碼。
摘要
在本文中,我們了解了如何在不引入任何其他基礎(chǔ)結(jié)構(gòu)的情況下設(shè)置領(lǐng)導(dǎo)者選舉。 JGroups提供了很多有趣的東西,例如分布式計數(shù)器,分布式任務(wù)執(zhí)行等。
翻譯自: https://www.javacodegeeks.com/2016/06/jgroups-leader-election-without-additional-infrastructure.html
總結(jié)
以上是生活随笔為你收集整理的JGroups:无需额外基础架构的领导人选举的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新浪手机版官方下载(新浪安卓版)
- 下一篇: 南京买房备案查询(南京买房备案)