簡體   English   中英

枚舉Akka.NET群集中的可用參與者

[英]Enumerating available actors in Akka.NET cluster

我有兩個演員,我們稱它們為ActorA和ActorB。 兩個參與者都作為基於Topshelf的Windows服務駐留在各自獨立的進程中。

基本上,它們看起來像這樣。

public class ActorA : ReceiveActor
{
    public ActorA()
    {
        this.Receive<ActorIdentity>(this.IdentifyMessageReceived);
    }


    private bool IdentifyMessageReceived(ActorIdentity obj)
    {
        return true;
    }
}

public class ActorB : ReceiveActor
{
    private readonly Cluster Cluster = Akka.Cluster.Cluster.Get(Context.System);


    public ActorB()
    {

        this.Receive<ActorIdentity>(this.IdentifyMessageReceived);
        this.ReceiveAsync<ClusterEvent.MemberUp>(this.MemberUpReceived);
    }

    protected override void PreStart()
    {
        this.Cluster.Subscribe(this.Self, ClusterEvent.InitialStateAsEvents, new[]
        {
            typeof(ClusterEvent.IMemberEvent),
            typeof(ClusterEvent.UnreachableMember)                
        });
    }

    protected override void PostStop()
    {
        this.Cluster.Unsubscribe(this.Self);
    }

    private async Task<bool> MemberUpReceived(ClusterEvent.MemberUp obj)
    {
        if (obj.Member.HasRole("actora"))
        {
            IActorRef actorSelection = await Context.ActorSelection("akka.tcp://mycluster@localhost:666/user/actora").ResolveOne(TimeSpan.FromSeconds(1));
            actorSelection.Tell(new Identify(1));
        }

        return true;
    }

    private bool IdentifyMessageReceived(ActorIdentity obj)
    {
        return true;
    }
}

我的配置文件非常簡單

演員A:

akka {
    log-config-on-start = on
    stdout-loglevel = DEBUG
    loglevel = DEBUG
    actor.provider = cluster
    remote {
        dot-netty.tcp {
            port = 666
            hostname = localhost

        }
    }
    cluster {
        seed-nodes = ["akka.tcp://mycluster@localhost:666"]
        roles = [actora]
    }
}

演員B:

akka {
    log-config-on-start = on
    stdout-loglevel = DEBUG
    loglevel = DEBUG
    actor.provider = cluster
    remote {
        dot-netty.tcp {
            port = 0
            hostname = localhost
        }
    }
    cluster {
        seed-nodes = ["akka.tcp://mycluster@localhost:666"]
        roles = [actorb]
    }
}

現在,我想確定連接到我的集群的所有給定參與者。 我通過等待集群節點MEMBER UP事件並嘗試將Identify()消息發送給給定的actor來接收對該事件的引用來完成此操作。

問題是我似乎無法成功將消息發送回ActorA 實際上,在執行上述代碼時(盡管我在ActorSelection方法中具有正確的引用),ActorIdentity消息是在ActorB而不是ActorA調用的。

我嘗試處理ActorA中所有收到的消息,但似乎從未收到Identity消息。 但是,我可以使用相同的ActorSelection參考成功發送任何其他類型的消息ActorA。

誰能提供任何見解? 為什么我的身份信息永遠無法到達目標演員?

ActorIdentity消息是在ActorB中而不是ActorA中調用的。

這是按預期方式工作的,因為您正在從ActorIdentity B→A發送Identify請求, ActorIdentity是響應消息(從A→B自動發送)。

您已經可以觀察到這種行為,因為:

Context.ActorSelection(path).ResolveOne(timeout)

差不多等於

Context.ActorSelection(path).Ask<ActorIdentity>(new Identify(null), timeout: timeout)

Identify是系統消息,始終在調用任何程序員定義的消息處理程序之前進行處理-因此,您可能不會在自己的處理程序中捕獲該消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM