设为首页 加入收藏

TOP

Akka(9): 分布式运算:Remoting-远程构建式(一)
2017-10-09 13:50:41 】 浏览:9040
Tags:Akka 分布式 运算 Remoting- 远程 构建

   上篇我们讨论了Akka-Remoting。我们说Akka-Remoting是一种点对点的通讯方式,能使两个不同JVM上Akka-ActorSystem上的两个Actor之间可以相互沟通。Akka-Remoting还没有实现完全的Actor位置透明(location transparency),因为一个Actor还必须在获得对方Actor确切地址信息后才能启动与之沟通过程。Akka-Remoting支持“远程查找”和“远程构建”两种沟通方式。由于篇幅所限,我们只介绍了“远程查找”。在这一篇里我们将会讨论“远程构建”方式。

同样,我们先通过项目结构来分析:

lazy val local = (project in file(".")) .settings(commonSettings) .settings( name := "remoteCreateDemo" ).aggregate(calculator,remote).dependsOn(calculator) lazy val calculator = (project in file("calculator")) .settings(commonSettings) .settings( name := "calculator" ) lazy val remote = (project in file("remote")) .settings(commonSettings) .settings( name := "remoteSystem" ).aggregate(calculator).dependsOn(calculator)

远程构建的过程大致是这样的:由local通知remote启动构建Actor;remote从本地库中查找Actor的类定义(class)并把它载入内存。由于驱动、使用远程Actor是在local进行的,所以local,remote项目还必须共享Calculator,包括Calculator的功能消息。这项要求我们在.sbt中用aggregate(calculator)来协同编译。

我们把Calculator的监管supervisor也包括在这个源码文件里。现在这个calculator是个包括监管、功能、消息的完整项目了。Calculator源代码如下:

package remoteCreation.calculator import akka.actor._ import scala.concurrent.duration._ object Calcultor { sealed trait MathOps case class Num(dnum: Double) extends MathOps case class Add(dnum: Double) extends MathOps case class Sub(dnum: Double) extends MathOps case class Mul(dnum: Double) extends MathOps case class Div(dnum: Double) extends MathOps sealed trait CalcOps case object Clear extends CalcOps case object GetResult extends CalcOps def props = Props(new Calcultor) def supervisorProps = Props(new SupervisorActor) } class Calcultor extends Actor with ActorLogging { import Calcultor._ var result: Double = 0.0   //internal state

  override def receive: Receive = { case Num(d) => result = d case Add(d) => result += d case Sub(d) => result -= d case Mul(d) => result *= d case Div(d) => val _ = result.toInt / d.toInt   //yield ArithmeticException
      result /= d case Clear => result = 0.0
    case GetResult => sender() ! s"Result of calculation is: $result" } override def preRestart(reason: Throwable, message: Option[Any]): Unit = { log.info(s"Restarting calculator: ${reason.getMessage}") super.preRestart(reason, message) } } class SupervisorActor extends Actor { def decider: PartialFunction[Throwable,SupervisorStrategy.Directive] = { case _: ArithmeticException => SupervisorStrategy.Resume } override def supervisorStrategy: SupervisorStrategy = OneForOneStrategy(maxNrOfRetries = 5, withinTimeRange = 5 seconds){ decider.orElse(SupervisorStrategy.defaultDecider) } val calcActor = context.actorOf(Calcultor.props,"calculator") override def receive: Receive = { case msg@ _ => calcActor.forward(msg) } }

与上一个例子的”远程查找式“相同,remote需要为Remoting公开一个端口。我们可以照搬.conf配置文件内容:remote/src/main/resources/application.conf

akka {
  actor {
    provider = remote
  }
  remote {
    enabled-transports = ["akka.remote.netty.tcp"]
    netty.tcp {
      hostname = "127.0.0.1"
      port = 2552
    }
    log-sent-messages = on
    log-received-messages = on
  }
}

由于远程构建和使用是在local上进行的,在remote上我们只需要启动ActorSystem就行了:

import com.typesafe.config.ConfigFactory import akka.actor._ object CalculatorRunner extends App { val remoteSystem = ActorSystem("remoteSystem",ConfigFact
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇sbt 学习笔记(1)sbt安装和交互.. 下一篇Akka(10): 分布式运算:集群-C..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目