问题
In my Akka.Net cluster I have several nodes. There is communication that I'd like to initiate when I've successfully joined the cluster. I can see in the log that I'm welcome to the cluster:
Welcome from [akka.tcp://Animatroller@hakan-el:8899]
but I can't see any events I can subscribe to that are raised for this.
回答1:
Usually what you want in that case is to subscribe to ClusterEvent.MemberUp
, which current actor will receive, once new node will become part of the cluster.
Example of actor handling this kind of event:
class MyActor : ReceiveActor
{
private readonly Cluster cluster = Cluster.Get(Context.System);
public MyActor()
{
Receive<ClusterEvent.MemberUp>(memberUp => memberUp.Member.Address == cluster.SelfAddress, memberUp =>
{
// handle current node up
});
}
protected override void PreStart()
{
cluster.Subscribe(Self, new []{ typeof(ClusterEvent.MemberUp)});
}
protected override void PostStop()
{
cluster.Unsubscribe(Self);
base.PostStop();
}
}
In this case memberUp => memberUp.Member.Address == cluster.SelfAddress
is additional filter for the handler which means, that it will be handled only, when current node, this actor resides on, will join the cluster. Up events send from other nodes will be ignored.
来源:https://stackoverflow.com/questions/34193701/is-there-an-event-in-akka-net-cluster-for-when-my-node-has-joined-a-cluster