原文地址:Clustering squbs Services using ZooKeeper
概覽
squbs通過zkcluster模塊實(shí)現(xiàn)服務(wù)集群。zkcluster是一個(gè) Akka 擴(kuò)展 ,利用Zookeeper來(lái)管理akka集群和分區(qū)。這與 Akka Cluster的領(lǐng)導(dǎo)&成員管理系統(tǒng)的功能近似。更強(qiáng)大的是它提供了分區(qū)支持,并消除了 entry-nodes的需要。
配置
我們需要一個(gè)squbsconfig/zkcluster.conf文件在運(yùn)行目錄下。他需要提供以下屬性:
- connectionString:一個(gè)逗號(hào)分隔的zookeeper節(jié)點(diǎn)字符串
- namespace: 有效的znode路徑名稱,這將是其之后所有znode創(chuàng)建節(jié)點(diǎn)的父代。
- segments: 劃分的分片數(shù)
以下是一個(gè)zkcluster.conf文件內(nèi)容的例子:
zkCluster {
connectionString = "zk-node-01.squbs.org:2181,zk-node-02.squbs.org:2181,zk-node-03.squbs.org:2181"
namespace = "clusteredservicedev"
segments = 128
}
用戶指導(dǎo)
從簡(jiǎn)單的注冊(cè)擴(kuò)展開始,介紹所有常見的akka擴(kuò)展。然后你訪問 zkClusterActor 并使用如下:
val zkClusterActor = ZkCluster(system).zkClusterActor
// Query the members in the cluster
zkClusterActor ! ZkQueryMembership
// Matching the response
case ZkMembership(members:Set[Address]) =>
// Query leader in the cluster
zkClusterActor ! ZkQueryLeadership
// Matching the response
case ZkLeadership(leader:Address) =>
// Query partition (expectedSize = None), create or resize (expectedSize = Some[Int])
zkClusterActor ! ZkQueryPartition(partitionKey:ByteString, notification:Option[Any] = None, expectedSize:Option[Int] = None, props:Array[Byte] = Array[Byte]())
// Matching the response
case ZkPartition(partitionKey:ByteString, members: Set[Address], zkPath:String, notification:Option[Any]) =>
case ZkPartitionNotFound(partitionKey: ByteString) =>
// Monitor or stop monitoring the partition change
zkClusterActor ! ZkMonitorPartition
zkClusterActor ! ZkStopMonitorPartition
// Matching the response
case ZkPartitionDiff(partitionKey: ByteString, onBoardMembers: Set[Address], dropOffMembers: Set[Address], props: Array[Byte] = Array.empty) =>
// Removing partition
zkClusterActor ! ZkRemovePartition(partitionKey:ByteString)
// Matching the response
case ZkPartitionRemoval(partitionKey:ByteString) =>
// List the partitions hosted by a certain member
zkClusterActor ! ZkListPartitions(address: Address)
// Matching the response
case ZkPartitions(partitionKeys:Seq[ByteString]) =>
// monitor the zookeeper connection state
val eventStream = context.system.eventStream
eventStream.subscribe(self, ZkConnected.getClass)
eventStream.subscribe(self, ZkReconnected.getClass)
eventStream.subscribe(self, ZkLost.getClass)
eventStream.subscribe(self, ZkSuspended.getClass)
// quit the cluster
zkCluster(system).zkClusterActor ! PoisonPill
// add listener when quitting the cluster
zkCluster(system).addShutdownListener(listener: () => Unit)
依賴
將以下依賴加入到你的build.sbt或scala構(gòu)建文件中:
"org.squbs" %% "squbs-zkcluster" % squbsVersion
設(shè)計(jì)
想要變更zkcluster的需讀取這個(gè):
- Membership 基于
zookeeper臨時(shí)節(jié)點(diǎn),關(guān)閉的session將會(huì)通過ZkMembershipChanged更改leader - Leadership基于
curator框架的LeaderLatch,新的選舉結(jié)果將會(huì)廣播ZkLeaderElected到所有節(jié)點(diǎn)。 - 分區(qū)通過leader計(jì)算,并通過leader節(jié)點(diǎn)的
ZkPartitionsManager寫入znode。 - 分區(qū)修改只能通過leader,通過ZkPartitionsManager 來(lái)強(qiáng)制執(zhí)行修改。
-
ZkPartitionsManager的follower節(jié)點(diǎn)將watch Zookeeper中的變更節(jié)點(diǎn)。一旦leader在rebalance之后改變分區(qū),ZkPartitionsManager的follower節(jié)點(diǎn)將會(huì)獲得通知并更新他們內(nèi)存中分區(qū)信息快照。 - 想被分區(qū)變更
ZkPartitionDiff通知到的,需發(fā)送ZkMonitorPartition至集群actor來(lái)注冊(cè)。
ZkMembershipMonitor是處理成員和領(lǐng)導(dǎo)的actor類型
ZkPartitionsManager 是處理分區(qū)管理的actor
ZkClusterActor 是一個(gè)提供用戶查詢的接口actor