Is there an event in Akka.net cluster for when my node has joined a cluster?

有些话、适合烂在心里 提交于 2019-12-11 12:47:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!