Akka.net:访问群集中的远程Actor [英] Akka.net: Access remote Actors in Cluster

查看:217
本文介绍了Akka.net:访问群集中的远程Actor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在集群环境中,我有一个种子节点以及node1和node2。

In an clustered environment I have a seed node and node1 and node2.

从node1,我想向在Actor2上创建的Actor发送消息。节点2上此节点的本地路径是akka:MyAkkaSystem / user / AnActor。

From node1 I want to send a message to an Actor which has been created on node2. The local path to this node on node2 is akka:MyAkkaSystem/user/AnActor.

现在,我想通过使用以下方式将消息从节点1发送到该特定参与者:像这样的ActorSelection:

Now I want to send a message from an Actor from node1 to this specific actor by using an ActorSelection like that:

var actorSystem = ActorSystem.Create("MyTestSystem");
var c = actorSystem.ActorSelection("/user/ConsoleReceiver");
c.Tell("Hello World");

在node2上,actor的创建方式如下:

On node2 the actor has been created like that:

var actorSystem = ActorSystem.Create("MyTestSystem");
            var r = actorSystem.ActorOf(Props.Create<MessageReceiver>(), "ConsoleReceiver");
            Console.WriteLine(r.Path);
            Console.ReadLine();
            actorSystem.Terminate().Wait();

不幸的是,由于尝试以无效字母结尾,因此无法解决问题。

Unfortunately this does not work out since the attempt ends in dead letters.

node2上的HOCON配置如下所示:

The HOCON configuration on node2 looks like that:

akka {
    actor {
      provider = "Akka.Cluster.ClusterActorRefProvider, Akka.Cluster"                  
      deployment {                  
      }              
    }

    remote {
      log-remote-lifecycle-events = DEBUG
      log-received-messages = on

      helios.tcp {
        transport-class = "Akka.Remote.Transport.Helios.HeliosTcpTransport, Akka.Remote"
            applied-adapters = []
            transport-protocol = tcp       
        hostname = "127.0.0.1"
        port = 0
      }
    }            

    cluster {
      #will inject this node as a self-seed node at run-time
      seed-nodes = ["akka.tcp://webcrawler@127.0.0.1:4053"] #manually populate other seed nodes here, i.e. "akka.tcp://lighthouse@127.0.0.1:4053", "akka.tcp://lighthouse@127.0.0.1:4044"
      roles = [crawler]
    }
  }

作为种子节点,我正在使用灯塔。从连接的角度来看,一切似乎都可以解决。已经找到了种子,并且获得的每个节点都收到了欢迎消息。

As seed node I am using lighthouse. From connection point of view everything seems to work out. The seed has been found and each node got has received a welcome message.

我以为我在集群上具有位置透明性,可以像访问本地资源一样访问远程资源。

I thought I had location transparency on a Cluster and could reach remote resources as if they where local.

推荐答案


我认为我在集群上具有位置透明性,可以像访问远程资源一样访问远程资源

I thought I had location transparency on a Cluster and could reach remote resources as if they where local.

这不是那么容易。考虑以下情形:如果您在同一路径下的两个节点上都创建了一个actor,该怎么办?如果您尝试使用相对路径-在不显示您想到的节点的情况下-哪个演员应该收到消息?

This is not so easy. Consider following scenario: What if you've created an actor on both nodes under the same path. If you'll try to use relative path - without showing which node you have in mind - which of the actor's should receive the message?.

使用基本群集功能,您可以使用 Context.ActorSelection(_cluster.ReadView.Members.Single(m => / *您要选择的哪个节点* /)。Address + / user / ConsoleReceiver轻松选择节点); 。群集扩展为您提供了一个读取视图数据,其中包含从当前节点可见的所有成员的信息。

Using basic cluster capabilities you can choose node easily using Context.ActorSelection(_cluster.ReadView.Members.Single(m => /* which node you want to choose */).Address + "/user/ConsoleReceiver");. Cluster extension gives you a read view data with info about all members visible from current node.

有很多方法可以将消息发送给另一个参与者,而不必

第一种方法是使用 Akka.Cluster.Tools < href = http://getakka.net/docs/clustering/cluster-singleton rel = noreferrer>集群单例功能-它最多允许您创建集群中存在的actor的一个实例。如果节点发生故障,它将迁移到另一个节点。请注意,如果您想让许多参与者以这种方式工作,则不应使用此解决方案。

First approach is to use Akka.Cluster.Tools cluster singleton feature - it allows you to create at most one instance of an actor present in the cluster. In case of node failures it will migrate to another node. Be aware that this solution shouldn't be used, if you want to have many actors working that way. It's more for distinct, special-case actors.

第二种方法是使用 Akka.Cluster.Tools 分布式Pub / Sub 功能可在订阅了特定主题的集群中的参与者之间广播集群范围的事件主题,而不必担心其实际位置。这是消息广播场景的不错选择。

Second approach is to use Akka.Cluster.Tools Distributed Pub/Sub feature to broadcast cluster-wide events across actors in the cluster subscribed to specific topic without worrying of their actual location. This is good choice for message broadcasting scenarios.

最后一种方法是使用 Akka.Cluster.Sharding 功能可自动管理参与者的生命周期-您无需显式创建参与者-它还能够将消息从群集中的任何位置路由到他们,并可以在他们之间重新平衡需要时有许多群集节点。

Last approach is to use Akka.Cluster.Sharding feature which manages actors lifecycle automatically - you don't need to create actors explicitly - it's also able to route messages to them from anywhere in the cluster and can rebalance them across many cluster nodes when needed.

这篇关于Akka.net:访问群集中的远程Actor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆