AKKA经典提供了多种方式实现多个接口的连接,以下是其中一种方法。
第一步:在application.conf文件中添加多个端口号,例如:
akka.remote.artery.canonical.port = 2551
akka.remote.artery.bind.hostname = ${HOSTNAME}
akka.remote.artery.bind.port = 10001
akka.remote.artery.canonical.port = 2552
akka.remote.artery.bind.hostname = ${HOSTNAME}
akka.remote.artery.bind.port = 10002
akka.remote.artery.canonical.port = 2553
akka.remote.artery.bind.hostname = ${HOSTNAME}
akka.remote.artery.bind.port = 10003
第二步:在代码中创建ActorSystem时使用配置文件中的三个端口号,例如:
val system1 = ActorSystem("system1", ConfigFactory.load().getConfig("system1"))
val system2 = ActorSystem("system2", ConfigFactory.load().getConfig("system2"))
val system3 = ActorSystem("system3", ConfigFactory.load().getConfig("system3"))
第三步:在Actor中使用多个ActorSystem连接到不同端口,例如:
val actor1 = system1.actorOf(Props[SomeActor], "actor1")
val actor2 = system2.actorSelection(s"akka://${system1.name}@${system1.provider.getDefaultAddress}/user/actor1")
val actor3 = system3.actorSelection(s"akka://${system2.name}@${system2.provider.getDefaultAddress}/user/actor2")
这样,就可以实现多个接口的连接。在Actor的创建和查找时需要使用对应的ActorSystem和端口号。