使用
依賴
compile group: 'com.typesafe.akka', name: 'akka-actor_2.12', version: '2.5.21'
compile group: 'com.typesafe.akka', name: 'akka-remote_2.12', version: '2.5.21'
定義消息協(xié)議
object Messages {
case class Hello(content: String)
case class World(content: String)
}
定義常量
object Cons {
val ResourceManagerName = "ResourceManagerName"
val NodeManagerName = "NodeManagerName"
val ResourceActor = "ResourceActor"
val NodeActor = "NodeActor"
}
服務(wù)器
import akka.actor._
import com.typesafe.config.{Config, ConfigFactory}
class MyResourceManager() extends Actor {
override def receive: Receive = {
case Messages.Hello(content: String) => {
sender() ! Messages.World("服務(wù)器回調(diào)")
}
}
}
object MyResourceManagerMain {
def main(args: Array[String]): Unit = {
val str: String =
"""
|akka.actor.provider = "akka.remote.RemoteActorRefProvider"
|akka.actor.warn-about-java-serializer-usage = off
|akka.remote.netty.tcp.hostname = localhost
|akka.remote.netty.tcp.port = 20000
""".stripMargin
val conf: Config = ConfigFactory.parseString(str)
val actorSystem = ActorSystem(Cons.ResourceManagerName, conf)
actorSystem.actorOf(Props(new MyResourceManager()), Cons.ResourceActor)
}
}
節(jié)點(diǎn)
import akka.actor.{Actor, ActorSelection, ActorSystem, Props}
import com.typesafe.config.{Config, ConfigFactory}
class MyNodeManager(resourceHost: String = "localhost", resourcePort: Int = 20000) extends Actor {
var resourceManager: ActorSelection = _
override def preStart(): Unit = {
resourceManager = context.actorSelection(s"akka.tcp://${Cons.ResourceManagerName}@$resourceHost:$resourcePort/user/${Cons.ResourceActor}")
resourceManager ! Messages.Hello("haha")
}
override def receive: Receive = {
case Messages.World(content: String) => {
println(content)
}
}
}
object MyNodeManagerMain {
def main(args: Array[String]): Unit = {
val str: String =
"""
|akka.actor.provider = "akka.remote.RemoteActorRefProvider"
|akka.actor.warn-about-java-serializer-usage = off
|akka.remote.netty.tcp.hostname = localhost
|akka.remote.netty.tcp.port = 20001
""".stripMargin
val conf: Config = ConfigFactory.parseString(str)
val actorSystem = ActorSystem(Cons.NodeManagerName, conf)
actorSystem.actorOf(Props(new MyNodeManager()), Cons.NodeActor)
}
}
- 啟動(dòng) MyResourceManager
- 啟動(dòng) MyNodeManager
[INFO] [02/21/2019 01:13:02.734] [main] [akka.remote.Remoting] Starting remoting
[INFO] [02/21/2019 01:13:02.878] [main] [akka.remote.Remoting] Remoting started; listening on addresses :[akka.tcp://NodeManagerName@localhost:20001]
[INFO] [02/21/2019 01:13:02.879] [main] [akka.remote.Remoting] Remoting now listens on addresses: [akka.tcp://NodeManagerName@localhost:20001]
服務(wù)器回調(diào)