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 秒

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值