在给定相对路径的情况下,从 Cluster 获取 ActorRef 的最佳方法是什么?

What's the best way to get an ActorRef from a Cluster given a relative path?

我有一个 Akka 应用程序,在集群中有多个节点。每个节点运行各种不同的 Actors,即并非所有节点都是相同的——为了冗余存在一些重复。

我试过这样的代码来获得一个 ref 来与另一个节点上的 Actor 通信:

val myservice = context.actorSelection("akka.tcp://ClusterSystem@127.0.0.1:2552/user/myService")

这是可行的,因为在该地址的节点上有一个名为 myService 运行 的 Actor。这感觉就像简单的 Akka Remoting,而不是集群,因为地址是点对点的。

我想询问集群 "Hey! Anybody out there have an ActorRef at path "/user/myService"?",并取回一个或多个引用(取决于那里有多少冗余副本)。然后我可以使用那个选择器进行通信。

考虑使用 Cluster Sharding,这样就无需确切知道 actors 在集群中的位置:

Cluster sharding is useful when you need to distribute actors across several nodes in the cluster and want to be able to interact with them using their logical identifier, but without having to care about their physical location in the cluster, which might also change over time.

使用集群分片,您不需要知道参与者的路径。相反,您与 ShardRegion 参与者交互,后者将消息委托给适当的节点。例如:

val stoutRegion: ActorRef = ClusterSharding(system).shardRegion("Stout")
stoutRegion ! GetPint("guinness")

如果您不想切换到集群分片而是使用您当前的部署结构,您可以使用 ClusterClient docs 中描述的 ClusterReceptionist。

但是,通过这种方式,您必须先向接待员注册演员,然后客户才能发现他们。