文件:build.sbt name := "TestIM" version := "1.0" scalaVersion := "2.11.7" libraryDependencies ++= Seq( "com.typesafe.akka" %% "akka-actor" % "2.4.1", "com.typesafe.akka" %% "akka-remote" % "2.4.1", "com.typesafe.akka" %% "akka-kernel" % "2.4.1", "com.typesafe.akka" %% "akka-stream-experimental" % "1.0", "com.typesafe.akka" %% "akka-http-core-experimental" % "1.0", "com.typesafe.akka" %% "akka-http-experimental" % "1.0", "com.typesafe.akka" %% "akka-http-spray-json-experimental" % "1.0", "org.reactivemongo" %% "reactivemongo" % "0.11.7", "org.scalatest" %% "scalatest" % "2.2.1" % "test" ) resolvers += "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/" resolvers += "Typesafe" at "https://repo.typesafe.com/typesafe/releases/"
application.confreact
RemoteSys { akka { actor { provider = "akka.remote.RemoteActorRefProvider" } remote { enabled-transports = ["akka.remote.netty.tcp"] netty.tcp { hostname = "127.0.0.1" port = 2552 } } } } LocalSys { akka { actor { provider = "akka.remote.RemoteActorRefProvider" } remote { enabled-transports = ["akka.remote.netty.tcp"] netty.tcp { hostname = "127.0.0.1" port = 2553 } } } }
RemoteActorjson
/** * Created by zzg on 16-4-24. */ class RemoteActor extends Actor{ def receive = { case message : String => sender ! message + "got someting" } }
RemoteNodeApp(遠程節點actor啓動)app
import akka.actor.{Props, ActorSystem} import com.typesafe.config.ConfigFactory /** * Created by zzg on 16-4-24. */ object RemoteNodeApp extends App{ val config = ConfigFactory.load.getConfig("RemoteSys") val system = ActorSystem("RemoteNodeApp",config) system.actorOf(Props[RemoteActor],"remoteActor") }
LocalActortcp
import akka.actor.{ActorLogging, Actor} import akka.util.Timeout import scala.concurrent.duration._ import scala.concurrent.Await import akka.pattern.ask /** * Created by zzg on 16-4-24. */ class LocalActor extends Actor with ActorLogging { val remoteActor = context.actorSelection("akka.tcp://RemoteNodeApp@127.0.0.1:2552/user/remoteActor") implicit val timeout = Timeout(5 seconds) def receive = { case message => val future = (remoteActor ? message).mapTo[String] val result = Await.result(future, timeout.duration) log.info("Message received from server -> {}", result) } }
LocalNodeApp(當前節點actor啓動)ide
import akka.actor.{Props, ActorSystem} import com.typesafe.config.ConfigFactory /** * Created by zzg on 16-4-24. */ object LocalNodeApp extends App{ val config = ConfigFactory.load.getConfig("LocalSys") val system = ActorSystem("LocalNodeApp",config) val localActor = system.actorOf(Props[LocalActor],"localActor") localActor ! "hello demo actor" }