Akka源码分析-Remote-Actor创建
在之前的博客中,我们分析过local模式下Actor的创建过程,最终还是调用了provider的actorOf的函数创建了Actor,在remote模式下provider就是RemoteActorRefProvider,所以这样就知道在哪里最终创建了Actor。
- def actorOf(system: ActorSystemImpl, props: Props, supervisor: InternalActorRef, path: ActorPath,
- systemService: Boolean, deploy: Option[Deploy], lookupDeploy: Boolean, async: Boolean): InternalActorRef =
- if (systemService) local.actorOf(system, props, supervisor, path, systemService, deploy, lookupDeploy, async)
- else {
- if (!system.dispatchers.hasDispatcher(props.dispatcher))
- throw new ConfigurationException(s"Dispatcher [${props.dispatcher}] not configured for path $path")
- /*
- * This needs to deal with “mangled” paths, which are created by remote
- * deployment, also in this method. The scheme is the following:
- *
- * Whenever a remote deployment is found, create a path on that remote
- * address below “remote”, including the current system’s identification
- * as “sys@host:port” (typically; it will use whatever the remote
- * transport uses). This means that on a path up an actor tree each node
- * change introduces one layer or “remote/scheme/sys@host:port/” within the URI.
- *
- * Example:
- *
- * akka.tcp://sys@home:1234/remote/akka/sys@remote:6667/remote/akka/sys@other:3333/user/a/b/c
- *
- * means that the logical parent originates from “akka.tcp://sys@other:3333” with
- * one child (may be “a” or “b”) being deployed on “akka.tcp://sys@remote:6667” and
- * finally either “b” or “c” being created on “akka.tcp://sys@home:1234”, where
- * this whole thing actually resides. Thus, the logical path is
- * “/user/a/b/c” and the physical path contains all remote placement
- * information.
- *
- * Deployments are always looked up using the logical path, which is the
- * purpose of the lookupRemotes internal method.
- */
- @scala.annotation.tailrec
- def lookupRemotes(p: Iterable[String]): Option[Deploy] = {
- p.headOption match {
- case None ⇒ None
- case Some("remote") ⇒ lookupRemotes(p.drop(3))
- case Some("user") ⇒ deployer.lookup(p.drop(1))
- case Some(_) ⇒ None
- }
- }
- val elems = path.elements
- val lookup =
- if (lookupDeploy)
- elems.head match {
- case "user" | "system" ⇒ deployer.lookup(elems.drop(1))
- case "remote" ⇒ lookupRemotes(elems)
- case _ ⇒ None
- }
- else None
- val deployment = {
- deploy.toList ::: lookup.toList match {
- case Nil ⇒ Nil
- case l ⇒ List(l reduce ((a, b) ⇒ b withFallback a))
- }
- }
- Iterator(props.deploy) ++ deployment.iterator reduce ((a, b) ⇒ b withFallback a) match {
- case d @ Deploy(_, _, _, RemoteScope(address), _, _) ⇒
- if (hasAddress(address)) {
- local.actorOf(system, props, supervisor, path, false, deployment.headOption, false, async)
- } else if (props.deploy.scope == LocalScope) {
- throw new ConfigurationException(s"configuration requested remote deployment for local-only Props at [$path]")
- } else try {
- try {
- // for consistency we check configuration of dispatcher and mailbox locally
- val dispatcher = system.dispatchers.lookup(props.dispatcher)
- system.mailboxes.getMailboxType(props, dispatcher.configurator.config)
- } catch {
- case NonFatal(e) ⇒ throw new ConfigurationException(
- s"configuration problem while creating [$path] with dispatcher [${props.dispatcher}] and mailbox [${props.mailbox}]", e)
- }
- val localAddress = transport.localAddressForRemote(address)
- val rpath = (RootActorPath(address) / "remote" / localAddress.protocol / localAddress.hostPort / path.elements).
- withUid(path.uid)
- new RemoteActorRef(transport, localAddress, rpath, supervisor, Some(props), Some(d))
- } catch {
- case NonFatal(e) ⇒ throw new IllegalArgumentException(s"remote deployment failed for [$path]", e)
- }
- case _ ⇒
- local.actorOf(system, props, supervisor, path, systemService, deployment.headOption, false, async)
- }
- }
上面的代码很多,简单起见,先只分析最后一段代码,即使lookupRemotes这个函数也非常重要。最后一段代码的逻辑是根据配置信息判断最终是创建本地actor还是远程actor(也就是RemoteActorRef)。其实这一点还是有点不好理解的,怎么绕来绕去还是用LocalActorRef创建了actor呢?其实关于这一点,我们还需要从官网的文档找到一些细节。官网原文如下:
If you want to use the creation functionality in Akka remoting you have to further amend the application.conf
file in the following way (only showing deployment section):
akka {
actor {
deployment {
/sampleActor {
remote = "akka.tcp://sampleActorSystem@127.0.0.1:2553"
}
}
}
}
The configuration above instructs Akka to react when an actor with path /sampleActor
is created, i.e. using system.actorOf(Props(...), "sampleActor")
. This specific actor will not be directly instantiated, but instead the remote daemon of the remote system will be asked to create the actor, which in this sample corresponds to sampleActorSystem@127.0.0.1:2553
.
Once you have configured the properties above you would do the following in code:
-
val actor = system.actorOf(Props[SampleActor], "sampleActor")actor ! "Pretty slick"
The actor class SampleActor
has to be available to the runtimes using it, i.e. the classloader of the actor systems has to have a JAR containing the class.
这句话的意思大概是,你可以指定某个actor在远程节点创建!感觉真是神一样的操作!仅仅通过配置就能实现!至少我看到这段说明的时候,感到有点不可思议。当然前提是对应的Actor类能够在远程加载到,也就是包含该类的jar在远程被加载过。这就是RemoteActorRef存在的意义。
分析到这里我们发现,remote模式下,actor的创建基本都转发给了LocalActorRef,那么这样创建的actor能够跨网络传输吗。其实我们仔细思考一下大概就明白了,不管是什么模式创建的actor,一定会有一个本地actor的。毕竟我们发消息的代码都在本地,这就必须有一个本地ActorRef实例。如果要跨网络发消息,就必须拿到远程Actor的ActorRef,而ActorRef我们知道是可以序列化后跨网络传输的,获取到了远程的ActorRef,就可以发远程消息了。至于远程部署Actor,其实也就是在本地创建了一个特殊的Actor(RemoteActorRef、ReliableDeliverySupervisor)作为代理,把消息通过对应的网络对象转发到远程而已。关于这一点我们后面再分析。
remote模式下,Actor的创建也就基本结束了,我们发现,这个跟local模式下的大概流程差别也不是很大,只是维护了与网路相关的对象。不过正是这些网络对象,使得我们可以给远程actor发消息。
Akka源码分析-Remote-Actor创建的更多相关文章
- dubbo源码分析1-reference bean创建
dubbo源码分析1-reference bean创建 dubbo源码分析2-reference bean发起服务方法调用 dubbo源码分析3-service bean的创建与发布 dubbo源码分 ...
- Akka源码分析-Actor创建(续)
在上一遍博客中,我们已经分析了actor创建的大致过程,但只是涉及到了Dipatcher/Mailbox/ActorCell/InternalActorRef等对象的创建,并没有介绍我们自定义的继承A ...
- Akka源码分析-Cluster-Singleton
akka Cluster基本实现原理已经分析过,其实它就是在remote基础上添加了gossip协议,同步各个节点信息,使集群内各节点能够识别.在Cluster中可能会有一个特殊的节点,叫做单例节点. ...
- Akka源码分析-Persistence
在学习akka过程中,我们了解了它的监督机制,会发现actor非常可靠,可以自动的恢复.但akka框架只会简单的创建新的actor,然后调用对应的生命周期函数,如果actor有状态需要回复,我们需要h ...
- Akka源码分析-local-DeathWatch
生命周期监控,也就是死亡监控,是akka编程中常用的机制.比如我们有了某个actor的ActorRef之后,希望在该actor死亡之后收到响应的消息,此时我们就可以使用watch函数达到这一目的. c ...
- Akka源码分析-Cluster-ActorSystem
前面几篇博客,我们依次介绍了local和remote的一些内容,其实再分析cluster就会简单很多,后面关于cluster的源码分析,能够省略的地方,就不再贴源码而是一句话带过了,如果有不理解的地方 ...
- Akka源码分析-Akka-Streams-概念入门
今天我们来讲解akka-streams,这应该算akka框架下实现的一个很高级的工具.之前在学习akka streams的时候,我是觉得云里雾里的,感觉非常复杂,而且又难学,不过随着对akka源码的深 ...
- Akka源码分析-Cluster-Metrics
一个应用软件维护的后期一定是要做监控,akka也不例外,它提供了集群模式下的度量扩展插件. 其实如果读者读过前面的系列文章的话,应该是能够自己写一个这样的监控工具的.简单来说就是创建一个actor,它 ...
- Akka源码分析-Cluster-Distributed Publish Subscribe in Cluster
在ClusterClient源码分析中,我们知道,他是依托于“Distributed Publish Subscribe in Cluster”来实现消息的转发的,那本文就来分析一下Pub/Sub是如 ...
- Akka源码分析-Akka Typed
对不起,akka typed 我是不准备进行源码分析的,首先这个库的API还没有release,所以会may change,也就意味着其概念和设计包括API都会修改,基本就没有再深入分析源码的意义了. ...
随机推荐
- reading/writing files in Python
file types: plaintext files, such as .txt .py Binary files, such as .docx, .pdf, iamges, spreadsheet ...
- vs2017 添加引用时 未能完成操作。不支持此接口
打开vs2017开发者命令提示符 切换至安装下的指定目录 执行下面的命令就可以了 需要注意的是一定要用vs2017的开发人员命令提示符 别用cmd gacutil -i Microsoft.V ...
- Python学习笔记 (2.2)Python中的字符编码问题及标准数据类型之String(字符串)
Python3中的String类型 首先,Python中没有字符类型,只有字符串类型.单个字符按照长度为1的字符串处理,这对于曾是OIER的我来说有点不适应啊. 字符串的表示方法 最常用的就是用一对双 ...
- poj 3074
题意:解数独 分析: 完整的数独有四个充要条件: 1.每个格子都有填数字 2.每列都有1~9中的每个数字 3.每行都有1~9中的每个数字 4.每个9宫格都有1~9中的每个数字 可以转化成精确覆盖问题. ...
- O - Treats for the Cows 区间DP
FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast am ...
- 一篮子苹果,每天吃一半多一个吃,第十天吃一半多一个后就剩余一个,求一共多少个苹果,JAVA版
/** * @author xuzhu **/public class TestApple { public static void main(String[] args) { int days = ...
- Spring实现封装自定义注解@Trimmed清除字符串前后的空格
在Spring中实现字符串清除的方法有很多,原生方法String自带trim()方法,或者使用StringUtils提供的trim...方法. 通常可以将上面的方式封装成自定义注解的形式去实现来节省更 ...
- apache2 ubuntu18.04 配置虚拟端口
修改3个文件/etc/apache2/apache2.conf/etc/apache2/ports.conf/etc/apache2/sites-available/000-default.conf ...
- CocoaPods: 制作cocoapods国内镜像
制作cocoapods国内镜像∞ March 15, 2014 Tags:cocoapods 国内访问cocoapods spec repo速度非常慢,网络不好的话做一次pod update要等半个小 ...
- Android 好看的搜索界面,大赞Animation
转载请注明出处王亟亟的大牛之路 一直对Animation属于可有可无的不在意.看到个样例,认为在不切换的情况下,适当的应用还真是蛮好看的. 包结构: 一个类一个控件.内容简单. 执行效果: 下方的下方 ...