【问题标题】:Akka 2.1 minimal remote actor exampleAkka 2.1 最小远程actor示例
【发布时间】:2013-02-02 19:03:16
【问题描述】:

编辑请注意,我需要对此 https://github.com/akka/akka/commit/ce014ece3568938b2036c4ccfd21b92faba69607#L13L6 进行反向更改,以使接受的答案与 AKKA 2.1 一起工作,AKKA 2.1 是在 akkas 主页上找到的稳定分布!


我已经阅读了我可以在 AKKA 上找到的所有教程,但没有找到任何“开箱即用”的教程。

使用eclipse,我想创建2个程序。

程序1: 启动演员“joe”并以某种方式使其在 127.0.0.1:some_port 上可用

程序2: 在 127.0.0.1:some_port 获得对演员“joe”的引用。向“joe”发送问候消息。

程序 1 应该在收到消息时打印一些内容。我想使用 AKKA 2.1 在 Eclipse 中运行这个示例。有人可以列出 2 个程序(程序 1 和程序 2)以及执行此操作的工作 application.conf 文件吗?


编辑> 让我告诉你到目前为止我得到了什么:

演员

case class Greeting(who: String) extends Serializable

class GreetingActor extends Actor with ActorLogging {
  def receive = {
    case Greeting(who) ⇒ println("Hello " + who);log.info("Hello " + who)
  }
}

程序1

package test

import akka.actor.ActorSystem

object Machine1 {

  def main(args: Array[String]): Unit = {
    val system = ActorSystem("MySystem")
  }

}

程序2

package test

import akka.actor.ActorSystem
import akka.actor.Props
import akka.actor.actorRef2Scala

object Machine2 {
  def main(args: Array[String]): Unit = {
    val system = ActorSystem("MySystem")
    val greeter = system.actorOf(Props[GreetingActor], name = "greeter")
    greeter ! Greeting("Felix")
  }
}

application.conf

akka {
  actor {
    deployment {
      /greeter {
        remote = "akka://MySystem@127.0.0.1:2553"
      }
    }
  }
}

但是,当我只启动 Program2 并且它输出时,该程序可以工作:

Hello Felix
[INFO] [02/18/2013 12:27:29.999] [MySystem-akka.actor.default-dispatcher-2] [akka://MySystem/user/greeter] Hello Felix

它似乎没有接收到我的 application.conf。我尝试将它放在我的 Eclipse 项目的 ./src/ 和 ./ 文件夹中。没有不同。另外,我知道这确实是降级部署,但我只需要一个 hello world 程序就可以使用 AKKA。我花了这么多时间,却没有得到一个简单的工作应用程序。

【问题讨论】:

标签: scala akka actor remote-actors


【解决方案1】:

正如 korefn 提到的,remote documentation 详细解释了它的工作原理。它还链接到example application。该示例应该为您提供入门所需的一切。


编辑

要让示例应用程序运行,请执行以下步骤:

从 GitHub 克隆

eecolor@BLACK:~/GihHub$ git clone https://github.com/akka/akka.git

进入akka目录并运行sbt

eecolor@BLACK:~/GihHub/akka$ sbt

切换到akka-sample-project

akka > project akka-sample-remote

在项目上调用run并选择CalcApp

Multiple main classes detected, select one to run:

 [1] sample.remote.calculator.java.JCreationApp
 [2] sample.remote.calculator.LookupApp
 [3] sample.remote.calculator.CalcApp
 [4] sample.remote.calculator.java.JLookupApp
 [5] sample.remote.calculator.CreationApp
 [6] sample.remote.calculator.java.JCalcApp

Enter number: 3

[info] Running sample.remote.calculator.CalcApp 
[INFO] [02/19/2013 19:22:09.055] [run-main] [Remoting] Starting remoting
[INFO] [02/19/2013 19:22:09.230] [run-main] [Remoting] Remoting started; listening on addresses :[akka.tcp://CalculatorApplication@127.0.0.1:2552]
Started Calculator Application - waiting for messages

切换到另一个控制台并重复前几个步骤

eecolor@BLACK:~/GihHub/akka$ sbt
akka > project akka-sample-remote

致电run 并选择LookupApp

akka-sample-remote > run

Multiple main classes detected, select one to run:

 [1] sample.remote.calculator.java.JCreationApp
 [2] sample.remote.calculator.LookupApp
 [3] sample.remote.calculator.CalcApp
 [4] sample.remote.calculator.java.JLookupApp
 [5] sample.remote.calculator.CreationApp
 [6] sample.remote.calculator.java.JCalcApp

Enter number: 2

[info] Running sample.remote.calculator.LookupApp 
[INFO] [02/19/2013 19:23:39.358] [run-main] [Remoting] Starting remoting
[INFO] [02/19/2013 19:23:39.564] [run-main] [Remoting] Remoting started; listening on addresses :[akka.tcp://LookupApplication@127.0.0.1:2553]
Started Lookup Application
Sub result: 14 - 16 = -2
Sub result: 13 - 22 = -9
Add result: 56 + 93 = 149
Add result: 18 + 19 = 37

切换回另一个控制台,您应该会看到如下内容:

Calculating 14 - 16
Calculating 13 - 22
Calculating 56 + 93
Calculating 18 + 19

【讨论】:

  • 该示例没有教我如何 a) 在没有简单构建工具的情况下运行一个最小的 akka 远程示例,b) akka 如何定位配置文件
  • This part of the documentation 告诉您配置的来源。与另一个 java 应用程序相比,使用 Akka 运行应用程序没有区别。您可以使用 OneJar 之类的东西将您的应用程序打包到一个 jar 中。确保您阅读了配置文档中关于将 OneJar 与 Akka 一起使用的警告。
  • 我无法运行您链接我的远程示例。我完全按照指南进行操作,得到了这个:felix@felix-UX32VD:~$ cd $AKKA_HOME felix@felix-UX32VD:~/akka-2.1.0$ sbt [info] Set current project to default-aac74b (in build file :/home/felix/akka-2.1.0/) > 项目 akka-sample-remote [错误] 不是有效的项目 ID:akka-sample-remote [错误] 项目 akka-sample-remote [错误]
  • 我不知道您所做的与示例项目中自述文件中的解释有何不同,但遵循您的指南是可行的。非常感谢!
  • 您能否给我评论一下为什么 application.conf 驻留在“/resources/”中,因为文档明确指出它将在几乎不应该在里面的类路径的根目录中查找它“ /资源/”?
【解决方案2】:

Akka 2.2.3 更新

可以按如下方式创建一个最小的远程应用程序:

在 Eclipse 中创建 2 个项目:客户端和服务器

服务器:

服务器的代码是

package server

import akka.actor.Actor
import akka.actor.ActorLogging
import akka.actor.ActorSystem
import akka.actor.Props

class Joe extends Actor {
  def receive = {
    case msg: String => println("joe received " + msg + " from " + sender)
    case _ => println("Received unknown msg ")
  }
}

object Server extends App {
  val system = ActorSystem("GreetingSystem")
  val joe = system.actorOf(Props[Joe], name = "joe")
  println(joe.path)
  joe ! "local msg!"
  println("Server ready")
}

服务器的 applincation.conf 是

akka {
  loglevel = "DEBUG"
  actor {
     provider = "akka.remote.RemoteActorRefProvider"
  }
  remote {
     enabled-transports = ["akka.remote.netty.tcp"]
     netty.tcp {
         hostname = "127.0.0.1"
         port = 2552
     }
     log-sent-messages = on
     log-received-messages = on
  }
}

客户:

客户端代码是

package client

import akka.actor._
import akka.actor.ActorDSL._

object Greet_Sender extends App {

   println("STARTING")

   implicit val system = ActorSystem("GreetingSystem-1")

   val joe = system.actorSelection("akka.tcp://GreetingSystem@127.0.0.1:2552/user/joe")

   println("That 's Joe:" + joe)

   val a = actor(new Act {
      whenStarting { joe ! "Hello Joe from remote" }
   })

   joe ! "Hello"

   println("Client has sent Hello to joe")
}

客户端application.conf是:

akka {
  #log-config-on-start = on
  stdout-loglevel = "DEBUG"
  loglevel = "DEBUG"
  actor {
      provider = "akka.remote.RemoteActorRefProvider"
  }
  remote {
    enabled-transports = ["akka.remote.netty.tcp"]
    log-sent-messages = on
    log-received-messages = on
    netty.tcp {
          hostname = "127.0.0.1"
          port = 0
    }
  }  
}

配置必须放在两个名为 application.conf 的文件中,都位于两个项目的 bin 目录中。

【讨论】:

    【解决方案3】:

    嗯,在你的例子中,客户端代码从不引用配置文件,它不会工作。

    【讨论】:

      【解决方案4】:

      默认情况下,akka 将使用 application.conf 文件 - 因此不需要明确选择。

      如果有人愿意,那么代码将是(以上面的代码为例):

      val configFile = getClass.getClassLoader.getResource("akka_local_application.conf").getFile
      val config = ConfigFactory.parseFile(new File(configFile))
      val system = ActorSystem("GreetingSystem",config)
      val joe = system.actorOf(Props[Joe], name = "joe")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-10
        • 2016-08-21
        • 2015-09-09
        • 1970-01-01
        • 2013-01-25
        • 1970-01-01
        • 2019-07-07
        • 2012-04-24
        相关资源
        最近更新 更多