0

我是 akka.net 的新手。我在一个集群中有 2 个 .net core2 控制台应用程序,试图将来自一个控制台应用程序 [这是种子节点] 的参与者的消息发送到另一个控制台应用程序 [非种子节点] 上的远程参与者。

启动/运行两个控制台应用程序后,集群建立,两个节点都启动,种子节点知道非种子节点,反之亦然,但非种子节点上的远程参与者没有收到消息. 我正在种子节点上创建一个循环路由器,但不确定我缺少什么?

请指导。

下面是两个应用程序的示例代码,即种子节点和非种子节点。

// 带有种子节点的 .net core2 控制台应用程序

class Program
{
    public static ActorSystem ClusterSystem;
    private static IActorRef StartActor;
    private static void Main(string[] args)
    {
        var config = ConfigurationFactory.ParseString(@"
akka
{
actor {
    provider=cluster
    deployment {
    /tasker {
      router = round-robin-pool # routing strategy
      nr-of-instances = 5 # max number of total routees
      cluster {
         enabled = on
         allow-local-routees = off
         use-role = tasker
         max-nr-of-instances-per-node = 1
      }
    }
  }
 }
remote
    {
    dot-netty.tcp {
      port = 8081
      hostname = ""localhost""
    }
}
cluster {
    seed-nodes = [""akka.tcp://ClusterSystem@localhost:8081""]
    roles=[""main""]
    }
}

        ClusterSystem = ActorSystem.Create("ClusterSystem", config);
        var taskActor = ClusterSystem.ActorOf(Props.Empty.WithRouter(FromConfig.Instance), "tasker");
        StartActor = ClusterSystem.ActorOf(Props.Create(() => new StartActor(taskActor)), "startactor");
        StartActor.Tell(new Initiate()); // call local actor

// actor on seed node (local actor)

class StartActor : ReceiveActor, ILogReceive
{
    private IActorRef taskActor;
    public StartActor(IActorRef router)
    {
        this.taskActor = router;
        Receive<Initiate>(i => Start(i));
    }
    private void Start(Initiate initiate)
    {
        taskActor.Tell(new Initiate()); // calling remote actor
    }
}

具有非种子节点的 .net core2 控制台应用程序

class Program
{
    public static ActorSystem ClusterSystem;
    public static IActorRef TaskActor;

    private static void Main(string[] args)
    {
        Console.Title = "BackEnd";
        var config = ConfigurationFactory.ParseString(@"
akka
{
actor {
provider=cluster
    }
remote
    {
    dot-netty.tcp {
    port = 0
    hostname = ""localhost""
    }
}
cluster {
    seed-nodes = [""akka.tcp://ClusterSystem@localhost:8081""]
    roles=[""tasker""]
    }
}
");

ClusterSystem = ActorSystem.Create("ClusterSystem", config);
        TaskActor = ClusterSystem.ActorOf(Props.Create<TaskActor>(), "tasker");
        Console.Read();
    }
}

// Actor on Non-seed node (Remote Actor)

class TaskActor : ReceiveActor, ILogReceive
{
    private readonly IActorRef manager;
    public TaskActor()
    {
        this.Receive<Initiate>(i => this.Init(i));
    }
    private void Init(Initiate initiate)
    {
        Console.WriteLine($"Message Received"); //
    }
  }
4

1 回答 1

0

我自己在回答我的问题。所以第一件事是,由于远程参与者是由/在另一个控制台应用程序中创建的,因此需要将部署配置与路由策略更改为“循环组”

/tasker {
      router = round-robin-group # routing strategy
        routees.paths = [""/user/starter""]
        nr-of-instances = 5 # max number of total routees
      cluster {
         enabled = on
         allow-local-routees = off
         use-role = tasker
      }
    }

来自种子节点的“startActor”需要如下

class StartActor : ReceiveActor, ILogReceive
{
    private IActorRef router, self;

    public StartActor(IActorRef router)
    {
        self = Self;
        this.router = router;

        Receive<Initiate>(i =>
        {
            var routee = router.Ask<Routees>(new GetRoutees()).ContinueWith(tr =>
            {
                if (tr.Result.Members.Count() > 0)
                {
                    Start(i);
                }
                else
                {
                    self.Tell(i);
                }
            });
        });
    }

    private void Start(Initiate initiate)
    {
        router.Tell(initiate);
    }
}

“startActor”中的上述代码查找一旦收到然后只发送消息的路由,否则消息被爆破并且不被远程演员接收。

于 2018-07-24T00:58:43.623 回答