8

我在一个 JVM 中使用 HelloActor 启动了 akka 系统,并尝试从另一个 JVM 中的客户端向它发送消息。没有任何效果。我应该如何正确地做到这一点?这是代码:

简单服务器

package akkaSample.severalSystems

    import akka.actor.{Props, Actor, ActorSystem}
    import com.typesafe.config.ConfigFactory

    class HelloActor extends Actor {
        override def preStart(): Unit = {
            println("Hello actor started")
        }

        def receive = {
            case "mew" => println("I said mew")
            case "hello" => println("hello back at you")
            case "shutdown" => context.stop(self)
            case _       => println("huh?")
        }
    }

    object Server extends App {
        val root = ConfigFactory.load()
        val one  = root.getConfig("systemOne")
        val system = ActorSystem("HelloSystem", one)
        val helloActor = system.actorOf(Props[HelloActor], "HelloActor")
        println (system)
        println("Remote application started.")
    }

简单客户端

package akkaSample.severalSystems.client

import akka.actor.ActorSystem
import com.typesafe.config.ConfigFactory
import scala.util.control.Breaks._

class Client {

}

object Client extends App {
    println("started")

    val root = ConfigFactory.load()
    val two  = root.getConfig("systemTwo")
    val system = ActorSystem("mySystem", two)
    //What should be there to access HelloActor?
    val selection = ...
    selection ! "mew"
    println(selection.anchorPath)
}

配置文件

systemOne {
  akka {
    remote {
      enabled-transports = ["akka.remote.netty.tcp"]
      netty.tcp {
        port = 2552
      }
    }
  }
}

systemTwo {
  akka {
    remote {
      enabled-transports = ["akka.remote.netty.tcp"]
      netty.tcp {
        port = 2553
      }
    }
  }
}

它假设Server在Client之前启动。println(selection.anchorPath)现在我在尝试获得这样的演员时收到“akka://mySystem/deadLetters”( )system.actorSelection("akka://HelloSystem@127.0.0.1:2552/HelloActor")

那么如何在另一个 JVM 中从 ActorSystem 中检索演员?还是在我创建集群、指定种子、领导者等之前是不可能的?

4

1 回答 1

6

您似乎错过了在配置中添加RemoteActorRefProvider。您的演员配置应如下所示:

systemOne {
   akka {
     actor {
      provider = "akka.remote.RemoteActorRefProvider"
    }
     remote {
       enabled-transports = ["akka.remote.netty.tcp"]
       netty.tcp {
         port = 2552
       }
     }
  }
}

systemTwo {
  akka {
    actor {
      provider = "akka.remote.RemoteActorRefProvider"
    }
    remote {
      enabled-transports = ["akka.remote.netty.tcp"]
      netty.tcp {
        port = 2553
      }
    }
  }
}

演员选择是使用完成的

context.actorSelection("akka.tcp://actorSystemName@10.0.0.1:2552/user/actorName")

您无需创建Akka 集群即可启用远程处理功能。

此配置更改RemoteActorRefProvider需要akka-remote作为依赖项。

于 2014-09-03T10:51:48.810 回答