如何从akka中的不同群集节点检索分片区域?

时间:2016-08-30 03:38:21

标签: akka akka-cluster

在下面的代码中,集群节点FrontendNode如何引用分片区域" SomeShardRegion" 已经在其他群集节点BackendNode中启动了? 当我尝试下面的代码时,我得到了这个例外:必须首先启动Shard类型[SomeShardRegion]。

注意:我不想开始另一个" SomeShardRegion"在FrontendNode中!

object BackendNode extends App {
  implicit val system = ActorSystem("ClusterSystem", config)

  ClusterSharding(system).start(
    typeName = "SomeShardRegion",
    entityProps = someProps,
    settings = ClusterShardingSettings(system),
    extractEntityId = idExtractor,
    extractShardId = shardResolver)
}

object FrontendNode extends App {
  implicit val system = ActorSystem("ClusterSystem", config)

  // throws an exception:  Shard type [SomeShardRegion] must be started first
  val shardRegion = ClusterSharding(system).shardRegion("SomeShardRegion")
}

2 个答案:

答案 0 :(得分:1)

似乎我总是要在检索它之前启动该区域。但是,如果我不想在前端创建实体,我可以 start shard region as a proxy

答案 1 :(得分:0)

我发现很多人在没有准确指出位置的情况下引用文档,所以这里是文档的链接,是answer的最近点。

引用文档

  

始终通过本地ShardRegion发送到实体的消息。   返回指定实体类型的ShardRegion actor引用   ClusterSharding.start也可以使用它进行检索   ClusterSharding.shardRegion。 ShardRegion将查找该位置   如果实体尚未知道其位置,则为该实体的分片。

这里有一些代码

// Selecting the ShardRegion with name Counter
val counterRegion: ActorRef = ClusterSharding(system).shardRegion("Counter")
counterRegion ! Message