经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Scala » 查看文章
Akka-Cluster(1)- Cluster Singleton 单例节点
来源:cnblogs  作者:雪川大虫  时间:2018/10/26 10:06:51  对本文有异议

   关于cluster-singleton我在前面的博文已经介绍过,在这篇我想回顾一下它的作用和使用方法。首先,cluster-singleton就是集群某个节点上的一个actor。任何时间在集群内保证只会有一个这种actor的实例。它可以是在任何节点上,具体位置由akka-cluster系统的leader节点根据一定规则选定。当cluster-singleton所处的节点停止运作时leader会选择另一个节点,然后系统会将cluster-singleton迁移到新的节点上来保证集群中一定有一个活着的cluster-singleton实例,不过值得注意的是迁移的actor会丢失它的内部状态。在编程实践中常常会需要保证一项程序功能只能由唯一的actor来运行的情况,比如我们需要保证某种运算的顺序,这时在集群环境里就可以使用cluster-singleton了。下面是cluster-singleton可能的一些使用场景:

1、在集群中的一个单点运算决策角色,或者是集群各节点交互运算的协调角色

2、集群与外界软件唯一的接口点

3、单一主角,多个从属

4、中央命名机制,或者中央路由逻辑

cluster-singleton的工作原理是:在集群的所有节点上都部署一个能产生、启动某singleton类型的ClusterSingletonManager,这样可以保证singleton可以迁移到任何节点。集群中的leader节点动态决定singleton的具体生存节点并指示该节点上的ClusterSingletonManager创建singleton实例。其它actor与singleton的交互是通过这个singleton类型的ClusterSingletonProxy进行的,这是cluster系统提供的一项与singleton交互的渠道服务,在需要接触singleton时可以创建ClusterSingletonProxy实例,然后把它当作目标发送操作消息,这样就可以保证cluster-singleton的位置透明特性了。

下面我们就用个简单的例子来示范cluster-singleton的使用看看它的唯一性和自动迁移特点:

构建一个简单的actor:

  1. class SingletonActor extends Actor with ActorLogging {
  2. import SingletonActor._
  3. import akka.cluster._
  4. override def receive: Receive = {
  5. case Greeting(msg) =>
  6. log.info("*********got {} from {}********", msg, sender().path.address)
  7. case Relocate =>
  8. log.info("*********I'll move from {}********", self.path.address)
  9. val cluster = Cluster(context.system)
  10. cluster.leave(cluster.selfAddress)
  11. case Die =>
  12. log.info("*******I'm shutting down ... ********")
  13. self ! PoisonPill
  14. }
  15. }

把SingletonActor包嵌在ClusterSingletonManager里:

  1. bject SingletonActor {
  2. trait SingletonMsg {}
  3. case class Greeting(msg: String) extends SingletonMsg
  4. case object Relocate extends SingletonMsg
  5. case object Die extends SingletonMsg
  6. def props = Props(new SingletonActor)
  7. def createSingleton(port: Int) = {
  8. val config = ConfigFactory.parseString(s"akka.remote.netty.tcp.port=$port")
  9. .withFallback(ConfigFactory.parseString("akka.cluster.roles=[singleton]"))
  10. .withFallback(ConfigFactory.load())
  11. val singletonSystem = ActorSystem("ClusterSingletonSystem",config)
  12. val singletonManager = singletonSystem.actorOf(ClusterSingletonManager.props(
  13. singletonProps = props,
  14. terminationMessage = Die,
  15. settings = ClusterSingletonManagerSettings(singletonSystem)
  16. .withRole(Some("singleton")) //只部署在角色是singleton的节点上
  17. ),
  18. name = "singletonManager"
  19. )
  20. }
  21. }

注意:singletonManager就是一个actor,所以是用actorOf(...)来构建的。现在这个singletonManager只能部署在singleton角色的节点上。

调用SingletonActor是通过ClusterSingletonProxy进行的:

  1. object SingletonUser {
  2. import SingletonActor._
  3. def sendToSingleton(msg: SingletonMsg) = {
  4. val config = ConfigFactory.parseString("akka.cluster.roles=[greeter]")
  5. .withFallback(ConfigFactory.load())
  6. val system = ActorSystem("ClusterSingletonSystem",config)
  7. val singletonProxy = system.actorOf(ClusterSingletonProxy.props(
  8. "user/singletonManager",
  9. ClusterSingletonProxySettings(system).withRole(None)
  10. ))
  11. singletonProxy ! msg
  12. }
  13. }

withRole(None)代表singletonProxy可以部署在任何节点上。下面是测试代码:

  1. import SingletonActor._
  2. import SingletonUser._
  3. object SingletonDemo extends App {
  4. createSingleton(2551) //seednode
  5. createSingleton(2552)
  6. createSingleton(2553)
  7. createSingleton(2554)
  8. scala.io.StdIn.readLine()
  9. sendToSingleton(Greeting("hello from tiger"))
  10. scala.io.StdIn.readLine()
  11. sendToSingleton(Relocate)
  12. scala.io.StdIn.readLine()
  13. sendToSingleton(Greeting("hello from cat"))
  14. scala.io.StdIn.readLine()
  15. sendToSingleton(Die)
  16. scala.io.StdIn.readLine()
  17. }

检验一下输出简要:

  1. [INFO] [10/25/2018 13:41:25.642] [ClusterSingletonSystem-akka.actor.default-dispatcher-3] [akka.tcp://ClusterSingletonSystem@127.0.0.1:51660/user/$a] Singleton identified at [akka.tcp://ClusterSingletonSystem@127.0.0.1:2551/user/singletonManager/singleton]
  2. [INFO] [10/25/2018 13:41:25.654] [ClusterSingletonSystem-akka.actor.default-dispatcher-20] [akka.tcp://ClusterSingletonSystem@127.0.0.1:2551/user/singletonManager/singleton] *********got hello from tiger from akka.tcp://ClusterSingletonSystem@127.0.0.1:51660********
  3. [INFO] [10/25/2018 13:43:10.839] [ClusterSingletonSystem-akka.actor.default-dispatcher-3] [akka.tcp://ClusterSingletonSystem@127.0.0.1:2551/user/singletonManager/singleton] *********I'll move from akka://ClusterSingletonSystem********
  4. INFO] [10/25/2018 13:43:18.885] [ClusterSingletonSystem-akka.actor.default-dispatcher-2] [akka.tcp://ClusterSingletonSystem@127.0.0.1:51670/user/$a] Singleton identified at [akka.tcp://ClusterSingletonSystem@127.0.0.1:2552/user/singletonManager/singleton]
  5. [INFO] [10/25/2018 13:43:18.886] [ClusterSingletonSystem-akka.actor.default-dispatcher-3] [akka.tcp://ClusterSingletonSystem@127.0.0.1:2552/user/singletonManager/singleton] *********got hello from cat from akka.tcp://ClusterSingletonSystem@127.0.0.1:51670********
  6. [INFO] [10/25/2018 13:43:18.156] [ClusterSingletonSystem-akka.actor.default-dispatcher-16] [akka.tcp://ClusterSingletonSystem@127.0.0.1:2551/user/singletonManager/singleton] *******I'm shutting down ... ********
  7. [INFO] [10/25/2018 13:43:18.177] [ClusterSingletonSystem-akka.remote.default-remote-dispatcher-18] [akka.tcp://ClusterSingletonSystem@127.0.0.1:2551/system/remoting-terminator] Shutting down remote daemon.
  8. [INFO] [10/25/2018 13:43:18.178] [ClusterSingletonSystem-akka.remote.default-remote-dispatcher-18] [akka.tcp://ClusterSingletonSystem@127.0.0.1:2551/system/remoting-terminator] Remote daemon shut down; proceeding with flushing remote transports.
  9. [INFO] [10/25/2018 13:43:18.215] [ClusterSingletonSystem-akka.remote.default-remote-dispatcher-14] [akka.tcp://ClusterSingletonSystem@127.0.0.1:2551/system/remoting-terminator] Remoting shut down.

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号