代码之家  ›  专栏  ›  技术社区  ›  Maxim Gershkovich

枚举akka.net集群中的可用参与者

  •  0
  • Maxim Gershkovich  · 技术社区  · 7 年前

    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;
        }
    }
    

    我的配置文件非常简单

    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]
        }
    }
    

    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()

    问题是我似乎无法成功地将消息发送回 ActorA ActorB 而不是 .

    Identity

    那么有人能提供什么见解吗?为什么我的身份信息从未到达我的目标演员?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Bartosz Sypytkowski    7 年前

    actoridentity消息是在actorb而不是actora中调用的。

    Identify 演员B→A的要求,其中 ActorIdentity

    Context.ActorSelection(path).ResolveOne(timeout)
    

    或多或少等于

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

    推荐文章