Akka-Cluster(1)- Cluster Singleton 单例节点
关于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:
class SingletonActor extends Actor with ActorLogging {
import SingletonActor._
import akka.cluster._
override def receive: Receive = {
case Greeting(msg) =>
log.info("*********got {} from {}********", msg, sender().path.address)
case Relocate =>
log.info("*********I'll move from {}********", self.path.address)
val cluster = Cluster(context.system)
cluster.leave(cluster.selfAddress)
case Die =>
log.info("*******I'm shutting down ... ********")
self ! PoisonPill
}
}
把SingletonActor包嵌在ClusterSingletonManager里:
bject SingletonActor {
trait SingletonMsg {}
case class Greeting(msg: String) extends SingletonMsg
case object Relocate extends SingletonMsg
case object Die extends SingletonMsg
def props = Props(new SingletonActor)
def createSingleton(port: Int) = {
val config = ConfigFactory.parseString(s"akka.remote.netty.tcp.port=$port")
.withFallback(ConfigFactory.parseString("akka.cluster.roles=[singleton]"))
.withFallback(ConfigFactory.load())
val singletonSystem = ActorSystem("ClusterSingletonSystem",config)
val singletonManager = singletonSystem.actorOf(ClusterSingletonManager.props(
singletonProps = props,
terminationMessage = Die,
settings = ClusterSingletonManagerSettings(singletonSystem)
.withRole(Some("singleton")) //只部署在角色是singleton的节点上
),
name = "singletonManager"
)
}
}
注意:singletonManager就是一个actor,所以是用actorOf(...)来构建的。现在这个singletonManager只能部署在singleton角色的节点上。
调用SingletonActor是通过ClusterSingletonProxy进行的:
object SingletonUser {
import SingletonActor._
def sendToSingleton(msg: SingletonMsg) = {
val config = ConfigFactory.parseString("akka.cluster.roles=[greeter]")
.withFallback(ConfigFactory.load())
val system = ActorSystem("ClusterSingletonSystem",config)
val singletonProxy = system.actorOf(ClusterSingletonProxy.props(
"user/singletonManager",
ClusterSingletonProxySettings(system).withRole(None)
))
singletonProxy ! msg
}
}
withRole(None)代表singletonProxy可以部署在任何节点上。下面是测试代码:
import SingletonActor._
import SingletonUser._
object SingletonDemo extends App { createSingleton() //seednode
createSingleton()
createSingleton()
createSingleton() scala.io.StdIn.readLine() sendToSingleton(Greeting("hello from tiger"))
scala.io.StdIn.readLine() sendToSingleton(Relocate)
scala.io.StdIn.readLine() sendToSingleton(Greeting("hello from cat"))
scala.io.StdIn.readLine() sendToSingleton(Die)
scala.io.StdIn.readLine() }
检验一下输出简要:
[INFO] [// ::25.642] [ClusterSingletonSystem-akka.actor.default-dispatcher-] [akka.tcp://ClusterSingletonSystem@127.0.0.1:51660/user/$a] Singleton identified at [akka.tcp://ClusterSingletonSystem@127.0.0.1:2551/user/singletonManager/singleton]
[INFO] [// ::25.654] [ClusterSingletonSystem-akka.actor.default-dispatcher-] [akka.tcp://ClusterSingletonSystem@127.0.0.1:2551/user/singletonManager/singleton] *********got hello from tiger from akka.tcp://ClusterSingletonSystem@127.0.0.1:51660******** [INFO] [// ::10.839] [ClusterSingletonSystem-akka.actor.default-dispatcher-] [akka.tcp://ClusterSingletonSystem@127.0.0.1:2551/user/singletonManager/singleton] *********I'll move from akka://ClusterSingletonSystem********
INFO] [// ::18.885] [ClusterSingletonSystem-akka.actor.default-dispatcher-] [akka.tcp://ClusterSingletonSystem@127.0.0.1:51670/user/$a] Singleton identified at [akka.tcp://ClusterSingletonSystem@127.0.0.1:2552/user/singletonManager/singleton]
[INFO] [// ::18.886] [ClusterSingletonSystem-akka.actor.default-dispatcher-] [akka.tcp://ClusterSingletonSystem@127.0.0.1:2552/user/singletonManager/singleton] *********got hello from cat from akka.tcp://ClusterSingletonSystem@127.0.0.1:51670******** [INFO] [// ::18.156] [ClusterSingletonSystem-akka.actor.default-dispatcher-] [akka.tcp://ClusterSingletonSystem@127.0.0.1:2551/user/singletonManager/singleton] *******I'm shutting down ... ********
[INFO] [// ::18.177] [ClusterSingletonSystem-akka.remote.default-remote-dispatcher-] [akka.tcp://ClusterSingletonSystem@127.0.0.1:2551/system/remoting-terminator] Shutting down remote daemon.
[INFO] [// ::18.178] [ClusterSingletonSystem-akka.remote.default-remote-dispatcher-] [akka.tcp://ClusterSingletonSystem@127.0.0.1:2551/system/remoting-terminator] Remote daemon shut down; proceeding with flushing remote transports.
[INFO] [// ::18.215] [ClusterSingletonSystem-akka.remote.default-remote-dispatcher-] [akka.tcp://ClusterSingletonSystem@127.0.0.1:2551/system/remoting-terminator] Remoting shut down.
Akka-Cluster(1)- Cluster Singleton 单例节点的更多相关文章
- Singleton(单例)模式
Singleton(单例)模式:保证一个类仅有一个实例,并提供一个访问它的全局访问点. public class Singleton { private static Singleton ourIns ...
- Java设计模式:Singleton(单例)模式
概念定义 Singleton(单例)模式是指在程序运行期间, 某些类只实例化一次,创建一个全局唯一对象.因此,单例类只能有一个实例,且必须自己创建自己的这个唯一实例,并对外提供访问该实例的方式. 单例 ...
- Java基础 static限定符的使用 以及【 static实现的 singleton(单例)设计模式】
static实现的 singleton(单例)设计模式 /** static实现的 singleton设计模式 , 使得一个类只能够创建一个static对象 */ 模板设计结构: package Co ...
- Singleton 单例模板
// singleton.h #ifndef SINGLETON_H #define SINGLETON_H // 单例基类模板 template <class T> class Sing ...
- lintcode:Singleton 单例
题目: 单例 单例是最为最常见的设计模式之一.对于任何时刻,如果某个类只存在且最多存在一个具体的实例,那么我们称这种设计模式为单例.例如,对于 class Mouse (不是动物的mouse哦),我们 ...
- 从别人写的 Object-C 中 Singleton (单例) 模式 中的一些理解--备
关于 面向对象的设计模式 对于面向对象的设计模式,想必大家并不陌生吧. 纵观23种设计模式中,数单例模式(Singleton)和工厂模式(Factory Method)最为熟悉和基础吧.当然,本文总结 ...
- Unity Singleton 单例类(Unity3D开发之二十)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/47335197 ...
- C++ Singleton (单例) 模式最优实现
参考:http://blog.yangyubo.com/2009/06/04/best-cpp-singleton-pattern/ 索引 静态化并不是单例 (Singleton) 模式 饿汉模式 懒 ...
- Singleton单例类模式
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
随机推荐
- Django之Auth模块 实现登录,退出,自带session 与认证功能的一个重要的模块
Auth模板 1. 什么是Auth模块,有什么用? django的auth的模块的使用: auth 是集合注册,登录,注销,session 多个功能集合在一起的模块 2. 使用Auth组件的默认aut ...
- Mybatis中DAO层接口没有写实现类,Mapper中的方法和DAO接口方法是怎么绑定到一起的,其内部是怎么实现的
其实也就是通过接口名与mapper的id绑定在一起(即相同),通过SQL去写实现类,返回数据.
- Django01-Django基础
一.什么是web框架? 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统.对于所有的Web应用,本质上其实就是一个socket服 ...
- html:class名命名规范
1 前端开发命名规范 1.1 为什么要制定CSS命名规范 统一的命名规范,便于多人开发维护时代码统一,减少项目沟通和交接的成本,增加代码的语义化. 1.2 CSS命名规则 样式类名全部用小写,首字符必 ...
- 一个RTSP/RTP over TCP 的丢包引起的问题
背景知识:可以查看https://www.cnblogs.com/lidabo/p/4483497.html RTSP/RTP over TCP TCP承载RTSP/RTP When you us ...
- C#面向对象架构总结
优秀的程序从命名开始,命名规则1.类名方法名需要见名知意.2.类的字段统统首字母小写并且封装仅供本类使用,并提供属性供外界使用,属性名开头大写,如果名字与类名冲突,那么在前面加下划线. 面向对象的7大 ...
- intellij idea 配置gitlab ssh key
1 安装git,登录官网https://www.git-scm.com/download/ ,选择相应系统版本,下载后安装好. 公司网慢的可以用第三方的软件管家下载. 2 打开git bash,不需要 ...
- E. Segment Sum(数位dp)
题意:求一个区间内满足所有数位不同数字个数小于K的数字总和.比如:k=2 1,2,3所有数位的不同数字的个数为1满足,但是123数位上有三个不同的数字,即123不满足. 我们可以使用一个二进制的数 ...
- abp xunit Can not register IHostingEnvironment. It should be a non-abstract class. If not, it should be registered before.”
在测试项目的ServiceCollectionRegistrar类提前注册.
- 跑python用ThinkPad好还是MacBook好?
跑Python,那肯定是服务器操作系统最好,找个方便安装Linux的本子. 我想题主的意图应该是做Python开发吧,如果是Python开发,还要看一下开发方向,如果是网络爬虫.服务器后端编程类的,那 ...