我的远程演员演示哪里错了?

Where is wrong in my remote actor demo?

我正在尝试向远程参与者发送消息,但失败了。

我的主要代码是:

RemoteActorDemo.scala

import akka.actor.{Actor, ActorSystem, Props}

object RemoteActorDemo extends App {
  val system = ActorSystem("RemoteActorSystem")
  val actor = system.actorOf(Props[RemoteActor], name = "RemoteActor")
  actor ! "Remote Actor is alive"
}

class RemoteActor extends Actor {
  override def receive: Receive = {
    case msg =>
      println("### RemoteActor received message: " + msg)
      sender ! "Hello from RemoteActor"
  }
}

application.conf:

akka {
  actor {
    provider = "akka.remote.RemoteActorRefProvider"
  }
  remote {
    enabled-transports = ["akka.remote.netty.tcp"]
    netty.tcp {
      hostname = "127.0.0.1"
      port = 5150
    }
  }
}

LocalActorDemo.scala:

import akka.actor.{Actor, ActorSystem, Props}

object LocalActorDemo extends App {
  val system = ActorSystem("ActorDemo")

  val localActor = system.actorOf(Props[LocalActor])
  localActor ! "Start"
}

class LocalActor extends Actor {
  val remote = context.actorSelection("akka.tcp://RemoteActorSystem@127.0.0.1:5150/user/RemoteActor")

  override def receive: Receive = {
    case "Start" =>
      println("### LocalActor started")
      remote ! "Hello from LocalActor"
    case msg => println("*** LocalActor receives msg: " + msg)
  }
}

问题是本地演员无法连接远程演员。它在控制台中打印:

### LocalActor started
[INFO] [10/05/2015 20:57:57.334] [ActorDemo-akka.actor.default-dispatcher-4] [akka://ActorDemo/deadLetters] 
Message [java.lang.String] from Actor[akka://ActorDemo/user/$a#-11944341] to Actor[akka://ActorDemo/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'.

我是akka的新手,不知道哪里错了。

您可以在此处查看演示项目:https://github.com/freewind/remote-actors-demo,您可以按照 "README" 的描述直接克隆和 运行 它。

在您的 local 子项目中添加一个 application.conf,内容如下:

akka {
  actor {
    provider = "akka.remote.RemoteActorRefProvider"
  }
  remote {
    enabled-transports = ["akka.remote.netty.tcp"]
    netty.tcp {
      hostname = "127.0.0.1"
      port = 0
    }
  }
}

正如the official document所说:

To enable remote capabilities in your Akka project you should, at a minimum, add the following changes to your application.conf file

这也适用于远程系统的客户端