[英]Not able to invoke Remote Actor
我写了这段代码来创建一个远程actor
object Main extends App {
val system = ActorSystem("keyvalue")
system.actorOf(Props[KeyValueActor], name = "keyvalue-db")
}
class KeyValueActor extends Actor {
val map = new util.HashMap[String, Object]
val log = Logging(context.system, this)
override def receive = {
case SetRequest(key, value) => {
log.info(s"received set request key ${key} value ${value}")
map.put(key, value)
sender() ! Status.Success
}
case GetRequest(key) => log.info(s"recieved get request ${key}")
sender() ! KeyValue(map.get(key))
case _=> log.info("unknown message")
}
}
我使用activator run
启动了服务器,并打印了消息
[info] Running com.abhi.akka.Main
[INFO] [01/10/2016 20:21:52.461] [run-main-0] [Remoting] Starting remoting
[INFO] [01/10/2016 20:21:52.617] [run-main-0] [Remoting] Remoting started;
listening on addresses :[akka.tcp://keyvalue@127.0.0.1:2552]
[INFO] [01/10/2016 20:21:52.619] [run-main-0] [Remoting]
Remoting now listens on addresses: [akka.tcp://keyvalue@127.0.0.1:2552]
但是现在当我尝试使用此客户端代码致电我的远程演员时
object KeyValueClient {
def main(args: Array[String]) : Unit = {
implicit val system = ActorSystem("LocalFileSystem")
implicit val timeout = Timeout(2 seconds)
val keyValueActorRef = system.actorSelection("akka.tcp://keyvalue@127.0.0.1:2552/user/keyvalue-db")
keyValueActorRef ! SetRequest("foo", "bar")
(keyValueActorRef ? GetRequest("foo")).onSuccess({
case x : KeyValue => println(s"got value ${x.value}")
})
}
}
抛出错误信息
[INFO] [01/10/2016 20:25:33.345] [LocalFileSystem-akka.actor.default-dispatcher-2] [akka://LocalFileSystem/deadLetters] Message [com.abhi.akka.messages.SetRequest] from Actor[akka://LocalFileSystem/deadLetters] to Actor[akka://LocalFileSystem/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/10/2016 20:25:33.346] [LocalFileSystem-akka.actor.default-dispatcher-2] [akka://LocalFileSystem/deadLetters] Message [com.abhi.akka.messages.GetRequest] from Actor[akka://LocalFileSystem/temp/$a] to Actor[akka://LocalFileSystem/deadLetters] was not delivered. [2] 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'.
我的完整代码位于
https://github.com/abhitechdojo/keyvaluedb.git
https://github.com/abhitechdojo/keyvalueclient.git
编辑:我根据穆斯塔法·西莫夫(Mustafa Simov)的建议解决了这个问题。 客户端需要此配置文件
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
deployment {
/keyvalue-db {
remote = "akka.tcp://keyvalue@127.0.0.1:2552"
}
}
}
}
然后您使用创建演员
val actor = system.actorOf(Props[KeyValueActor], "keyvalue-db")
我查看了您的客户端代码,我认为这只是一个配置问题。 为了使用akka远程处理,您还必须配置客户端。 因此,您必须像为服务器创建的那样为客户端ActorSystem创建application.conf
。
Akka远程处理是点对点的, 并不十分适合客户端服务器
因此,远程处理中的双方都必须能够连接到另一方,因为它们中的任何一方都可以启动通信。 因此,您的两个应用程序都需要配置一个远程actor-ref提供程序和另一个节点可以通过其连接的主机+端口对。 您可以在此处的akka文档中阅读有关如何执行此操作的详细信息 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.