我需要创建一个由 3 个节点组成的集群,其中每个节点将运行一个 actor 的一个实例 - TestActor
。我希望路由器自己创建节点(池),但它创建的第一个节点将是种子节点,因为路由将是集群中创建的第一个参与者。
这是我的代码 TestActor.scala :
import akka.actor.{Actor, ActorLogging, ActorSystem, Props}
import akka.cluster.Cluster
import akka.routing.FromConfig
import com.typesafe.config.ConfigFactory
import akka.pattern.ask
import akka.util.Timeout
import scala.concurrent.ExecutionContext.Implicits.global
object TestActor {
def main(args: Array[String]) {
val system = ActorSystem("mySys", ConfigFactory.load("test"))
val testActor = system.actorOf(Props(classOf[TestActor]).withRouter(FromConfig()),"TestActor")
implicit val t = Timeout(60 * 1000)
println((testActor ? "hello").onSuccess{
case x=> println(x)
})
}
}
class TestActor extends Actor with ActorLogging {
Cluster(system)
override def postStop(): Unit = {
log.info(s"Stopping actor on path ${self.path}")
super.postStop()
}
override def preStart(): Unit ={
log.info(s"Starting actor on path ${self.path}")
super.preStart()
}
override def receive : Receive = {
case x => {
log.info(s"actor : ${self.path} GOT MESSAGE $x")
sender() ! x
}
}
}
和 test.conf :
akka {
cluster {
auto-down-unreachable-after = 20s
seed-nodes = [
"akka.tcp://mySys@127.0.0.1:2552"
]
}
actor {
provider = "akka.cluster.ClusterActorRefProvider"
deployment {
/TestActor {
router = round-robin-pool
nr-of-instances = 3
cluster {
enabled = on
max-nr-of-instances-per-node = 1
allow-local-routees = off
}
}
}
}
}
然后,当我运行程序时,我看不到任何打印,并且消息转到 deadLetters。
这是日志:
[INFO] [01/08/2017 13:29:17.967] [main] [Remoting] Starting remoting
[INFO] [01/08/2017 13:29:18.290] [main] [Remoting] Remoting started; listening on addresses :[akka.tcp://mySys@127.0.0.1:2552]
[INFO] [01/08/2017 13:29:18.292] [main] [Remoting] Remoting now listens on addresses: [akka.tcp://mySys@127.0.0.1:2552]
[INFO] [01/08/2017 13:29:18.301] [main] [Cluster(akka://mySys)] Cluster Node [akka.tcp://mySys@127.0.0.1:2552] - Starting up...
[INFO] [01/08/2017 13:29:18.366] [main] [Cluster(akka://mySys)] Cluster Node [akka.tcp://mySys@127.0.0.1:2552] - Registered cluster JMX MBean [akka:type=Cluster]
[INFO] [01/08/2017 13:29:18.366] [main] [Cluster(akka://mySys)] Cluster Node [akka.tcp://mySys@127.0.0.1:2552] - Started up successfully
[INFO] [01/08/2017 13:29:18.374] [mySys-akka.actor.default-dispatcher-2] [Cluster(akka://mySys)] Cluster Node [akka.tcp://mySys@127.0.0.1:2552] - Metrics will be retreived from MBeans, and may be incorrect on some platforms. To increase metric accuracy add the 'sigar.jar' to the classpath and the appropriate platform-specific native libary to 'java.library.path'. Reason: java.lang.ClassNotFoundException: org.hyperic.sigar.Sigar
[INFO] [01/08/2017 13:29:18.375] [mySys-akka.actor.default-dispatcher-2] [Cluster(akka://mySys)] Cluster Node [akka.tcp://mySys@127.0.0.1:2552] - Metrics collection has started successfully
()
[INFO] [01/08/2017 13:29:18.382] [mySys-akka.actor.default-dispatcher-2] [akka://mySys/deadLetters] Message [java.lang.String] from Actor[akka://mySys/temp/$a] to Actor[akka://mySys/deadLetters] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
[INFO] [01/08/2017 13:29:18.387] [mySys-akka.actor.default-dispatcher-5] [Cluster(akka://mySys)] Cluster Node [akka.tcp://mySys@127.0.0.1:2552] - Node [akka.tcp://mySys@127.0.0.1:2552] is JOINING, roles []
[INFO] [01/08/2017 13:29:19.399] [mySys-akka.actor.default-dispatcher-3] [Cluster(akka://mySys)] Cluster Node [akka.tcp://mySys@127.0.0.1:2552] - Leader is moving node [akka.tcp://mySys@127.0.0.1:2552] to [Up]