akka cluster singleton

cluster singleton 需要注意的一点是

ClusterSingletonProxy 必须和 ClusterSingletonManager 一起工作

尝试过通过 path 来获得 singleton actor 的 actorRef,但是结果和期望的不同,具体的表现是,在 main 方法中,创建完 singleton actor 后,无法通过 path 获得实例

val system = ActorSystem("ClusterSystem", config)

val singletonManager: Props = ClusterSingletonManager.props(Master.props(Duration(99, "second")), "active", PoisonPill, None)

system.actorOf(singletonManager, "master")

val res = system.actorOf(ClusterSingletonManager.props(Master.props(Duration(99, "second")), "active", PoisonPill, None), "master")


val path = ActorPath.fromString("akka.tcp://ClusterSystem@127.0.0.1:2551/user/master/active")

askSingletonActor(system, path)

  

askSingletonActor 会报错,说找不到此 actor,但是 ClusterSingletonManager 的 actor 倒是找得到

 

在 singleton actor 的内部通过 path 找自己就找得到

 

正解是,通过 ClusterSingletonProxy 来寻找 singleton actor

val singletonActor = system.actorOf(ClusterSingletonProxy.props(
        singletonPath = "/user/master/active",
        role = None),
        name = "consumerProxy")
      
singletonActor ! ParentGreetings

  

这种方式,总是能够找的到 singleton actor。

 

对于一般的 actor,在 main 方法创建之后可以直接找到 actor

 

此外,我在 actor 做了下测试,通过消息传递的方式,1 秒内 能够从 0 加到 4 million,而 for 循环,达到千万只用了不到 0.05 秒

posted @ 2015-07-02 17:02  SangS  阅读(845)  评论(0编辑  收藏  举报