scala akka remote actor —— Hello World
已经写了2个hello world的例子,包括以jar包方式运行和以微内核方式运行两种,下面写一个remote actor的例子,更贴近下现实的应用
1. 创建两个maven工程,一个是Remote端,一个Local端的,相当于Server/Client模式,maven依赖如下:
<akka.version>2.3.2</akka.version> <dependency> <groupId>com.typesafe.akka</groupId> <artifactId>akka-actor_2.10</artifactId> <version>${akka.version}</version> </dependency> <dependency> <groupId>com.typesafe.akka</groupId> <artifactId>akka-remote_2.10</artifactId> <version>${akka.version}</version> </dependency>
2. 编写Remote端代码,sender表示消息发送者,这里要记住ActorSystem的名字——HelloRemoteSystem和Actor的名字——RemoteActor,在Local端会使用到
HelloRemote.scala
object HelloRemote extends App { val system = ActorSystem("HelloRemoteSystem") val remoteActor = system.actorOf(Props[RemoteActor], name = "RemoteActor") remoteActor ! Message("The RemoteActor is alive") } class RemoteActor extends Actor { def receive = { case Message(msg) => println(s"RemoteActor received message '$msg'") sender ! Message("Hello from the RemoteActor") // 回复消息 case _ => println("RemoteActor got something unexpected.") } }
在src/main/resources中创建application.conf,这个文件要在运行的classpath下
akka { actor { provider = "akka.remote.RemoteActorRefProvider" } remote { transport = "akka.remote.netty.NettyRemoteTransport" netty.tcp { hostname = "127.0.0.1" # Remote端IP地址 port = 5150 # Remote端端口号 } } }
3. 编写Local端代码, 注意下面这行代码,调用Remote端的方式
akka.tcp://Remote端ActorSystem的名字@Remote端配置的IP:Remote端配置的端口号/user/Remote端的Actor名字
context.actorFor("akka.tcp://HelloRemoteSystem@127.0.0.1:5150/user/RemoteActor")
HelloLocal.scala
import akka.actor.ActorSystem import org.wuming.akka.hello.common.Message import org.wuming.akka.hello.common.Start import akka.actor.Actor import akka.actor.Props object HelloLocal extends App { System.setProperty("akka.remote.netty.port", "5152") implicit val system = ActorSystem("LocalSystem") val localActor = system.actorOf(Props[LocalActor], name = "LocalActor") // the local actor localActor ! Start } class LocalActor extends Actor { // create the remote actor val remote = context.actorFor("akka.tcp://HelloRemoteSystem@127.0.0.1:5150/user/RemoteActor") var counter = 0 def receive = { case Start => remote ! Message("Hello from the LocalActor") case Message(msg) => println(s"LocalActor received message: '$msg'") if (counter < 5) { sender ! Message("Hello back to you") counter += 1 } case _ => println("LocalActor got something unexpected.") } }
在src/main/resources中创建application.conf,这个文件要在运行的classpath下
akka { actor { provider = "akka.remote.RemoteActorRefProvider" } remote { transport = "akka.remote.netty.NettyRemoteTransport" netty.tcp { hostname = "127.0.0.1" # Local端IP地址 port = 0 # Local端端口号,0表示随机分配 } } }
4. Remote端和Local端共用了消息类型代码
Common.scala:
case object Start case class Message(msg: String)
5. 运行,首先运行Remote端,然后再运行Local端
RemoteActor received message 'The RemoteActor is alive'
RemoteActor received message 'Hello from the LocalActor'
RemoteActor received message 'Hello back to you'
RemoteActor received message 'Hello back to you'
RemoteActor received message 'Hello back to you'
RemoteActor received message 'Hello back to you'
RemoteActor received message 'Hello back to you'
LocalActor received message: 'Hello from the RemoteActor'
LocalActor received message: 'Hello from the RemoteActor'
LocalActor received message: 'Hello from the RemoteActor'
LocalActor received message: 'Hello from the RemoteActor'
LocalActor received message: 'Hello from the RemoteActor'
LocalActor received message: 'Hello from the RemoteActor'