akka.net群集:如何将消息从种子节点发送到非种子节点

时间:2018-07-16 12:46:10

标签: akka.net akka.net-cluster

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

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

请指导。

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

//具有种子节点的.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"); //
    }
  }

1 个答案:

答案 0 :(得分: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”中查找路由,这些路由一旦被接收,则仅发送消息,否则消息被发送,并且未被远程actor接收。