我们如何在一台电脑中运行多个akka节点?目前,我已在我的application.conf
文件中关注。对于每个系统,我添加了不同的端口号,但是,我不能启动多个实例。错误说,Address already in use failed to bind
。
application.conf
档案
remotelookup {
include "common"
akka {
remote.server.port = 2500
cluster.nodename = "n1"
}
}
更新:多个akka节点意味着,我有不同的独立服务器应用程序,它将使用akka与远程主节点通信。
答案 0 :(得分:12)
我们使用的方法是:
在application.conf
为每个系统创建不同的设置:
systemOne {
akka {
remote {
enabled-transports = ["akka.remote.netty.tcp"]
netty.tcp {
hostname = ${public-hostname}
port = 2552
}
}
}
}
systemTwo {
akka {
remote {
enabled-transports = ["akka.remote.netty.tcp"]
netty.tcp {
hostname = ${public-hostname}
port = 2553
}
}
}
}
Application.conf
是默认配置文件,因此在您的设置模块中为系统添加配置:
object Configs {
private val root = ConfigFactory.load()
val one = root.getConfig("systemOne")
val two = root.getConfig("systemTwo")
}
然后使用此配置创建系统:
val one = ActorSystem("SystemName", one)
val two = ActorSystem("AnotherSystemName", two)
不要忘记系统名称必须不同
答案 1 :(得分:2)
如果您不想将信息硬编码到application.conf
,可以执行以下操作:
def remoteConfig(hostname: String, port: Int, commonConfig: Config): Config = {
val configStr = s"""
|akka.remote.netty.hostname = $hostname
|akka.remote.netty.port = $port
""".stripMargin
ConfigFactory.parseString(configStr).withFallback(commonConfig)
}
然后使用它:
val appConfig = ConfigFactory.load
val sys1 = ActorSystem("sys1", remoteConfig(args(0), args(1).toInt, appConfig))
val sys2 = ActorSystem("sys2", remoteConfig(args(0), args(2).toInt, appConfig))
如果您使用0作为端口,Akka会将随机端口#分配给ActorSystem
。
答案 2 :(得分:1)
问题在于端口定义。它应该像
remotelookup {
include "common"
akka {
remote.netty.port = 2500
cluster.nodename = "n1"
}
}
其他方面,akka将采用默认端口。