TOP

Akka(10): 分布式运算:集群-Cluster(三)
2017-10-09 13:50:40 】 浏览:10565
Tags:Akka 分布式 运算 集群 -Cluster

fter graceful leaving and exiting.
*/ final case class MemberRemoved(member: Member, previousStatus: MemberStatus) extends MemberEvent { if (member.status != Removed) throw new IllegalArgumentException("Expected Removed status, got: " + member) } /** * Marker interface to facilitate subscription of * both [[UnreachableMember]] and [[ReachableMember]]. */ sealed trait ReachabilityEvent extends ClusterDomainEvent { def member: Member } /** * A member is considered as unreachable by the failure detector. */ final case class UnreachableMember(member: Member) extends ReachabilityEvent /** * A member is considered as reachable by the failure detector * after having been unreachable. * @see [[UnreachableMember]] */ final case class ReachableMember(member: Member) extends ReachabilityEvent

集群的当前状态值是存放在下面CurrentClusterState结构里的: 

/** * Current snapshot state of the cluster. Sent to new subscriber. */ final case class CurrentClusterState( members: immutable.SortedSet[Member] = immutable.SortedSet.empty, unreachable: Set[Member] = Set.empty, seenBy: Set[Address] = Set.empty, leader: Option[Address] = None, roleLeaderMap: Map[String, Option[Address]] = Map.empty) { /** * Java API: get current member list. */ def getMembers: java.lang.Iterable[Member] = { import scala.collection.JavaConverters._ members.asJava } /** * Java API: get current unreachable set. */ def getUnreachable: java.util.Set[Member] = scala.collection.JavaConverters.setAsJavaSetConverter(unreachable).asJava /** * Java API: get current “seen-by” set. */ def getSeenBy: java.util.Set[Address] = scala.collection.JavaConverters.setAsJavaSetConverter(seenBy).asJava /** * Java API: get address of current leader, or null if none */ def getLeader: Address = leader orNull /** * All node roles in the cluster */ def allRoles: Set[String] = roleLeaderMap.keySet /** * Java API: All node roles in the cluster */ def getAllRoles: java.util.Set[String] = scala.collection.JavaConverters.setAsJavaSetConverter(allRoles).asJava /** * get address of current leader, if any, within the role set */ def roleLeader(role: String): Option[Address] = roleLeaderMap.getOrElse(role, None) /** * Java API: get address of current leader within the role set, * or null if no node with that role */ def getRoleLeader(role: String): Address = roleLeaderMap.get(role).flatten.orNull }

用户可以监听这些事件的发生:

cluster.subscribe(self, initialStateMode = InitialStateAsEvents, classOf[MemberEvent], classOf[UnreachableMember])

另外,我们还可以用callback方式在状态转换前后调用一些运算来进行准备处理和事后处理:

 /** * The supplied thunk will be run, once, when current cluster member is `Up`. * Typically used together with configuration option `akka.cluster.min-nr-of-members` * to defer some action, such as starting actors, until the cluster has reached * a certain size. */ def registerOnMemberUp[T](code: ? T): Unit = registerOnMemberUp(new Runnable { def run() = code }) /** * Java API: The supplied callback will be run, once, when current cluster member is `Up`. * Typically used together with configuration option `akka.cluster.min-nr-of-members` * to defer some action, such as starting actors, until the cluster has reached * a certain size. */ def registerOnMemberUp(callback: Runnable): Unit = clusterDaemons ! InternalClusterAction.AddOnMemberUpListener(callback) /** * The supplied thunk will be run, once, when current cluster member is `Removed`. * If the cluster has already bee  
		
Akka(10): 分布式运算:集群-Cluster(三) https://www.cppentry.com/bencandy.php?fid=90&id=124492

首页 上一页 1 2 3 4 5 6 7 下一页 尾页 3/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Akka(9): 分布式运算:Remotin.. 下一篇Akka(12): 分布式运算:Cluste..