akka(7)Akka Remote Actor and Akka in Spray
Dispatcher is to pick up the thread to execute the message box and actor.
Router is an actor to route the message to different actors.
1. Akka System
router to jvm Akka System.
A very Simple Actor, just receive the Message and Print, EventMessageActor.
package com.sillycat.akka.actor
import akka.actor.Actor
import com.typesafe.scalalogging.slf4j.Logging
import com.sillycat.akka.model.EventMessage
class EventMessageActor extends Actor with Logging {
logger.info("Created a EventMessage Actor")
def receive = {
case item: EventMessage => {
//handle the eventMessage
logger.debug("Logging I receive one object:" + item)
//this will send the response back
//sender ! item + " Server is running this task"
}
case item: String => {
logger.debug("Logging I receive one object:" + item)
}
case _ => logger.error("Received a message I don't understand.")
}
}
The WatcherActor
package com.sillycat.akka.actor
import akka.actor.{ Terminated, Actor, ActorRef }
import com.typesafe.scalalogging.slf4j.Logging
class ActorWatcher(watched: ActorRef) extends Actor with Logging {
context.watch(watched)
def receive = {
case Terminated(watched) => {
logger.info("The watched actor was terminated: " + watched.toString())
context.system.shutdown()
}
case _ => logger.info("ActorWatcher got a message not intended for it!")
}
}
The configuration file about this actor with router in localjvm.conf
akka {
# Options: ERROR, WARNING, INFO, DEBUG
loglevel = "DEBUG"
# Log the complete configuration at INFO level when the actor system is started.
# This is useful when you are uncertain of what configuration is used.
#log-config-on-start = on
actor.deployment {
/EventMessageLocalRouter {
router = round-robin
resizer {
lower-bound = 5
upper-bound = 100
}
}
}
}
Simple Application class, EventService for Testing
package com.sillycat.akka.server
import com.sillycat.akka.actor.{ ActorWatcher, EventMessageActor }
import com.typesafe.config.ConfigFactory
import com.typesafe.scalalogging.slf4j.Logging
import akka.actor._
import akka.routing.{ FromConfig, RoundRobinRouter }
import akka.routing.Broadcast
import com.sillycat.akka.model.EventMessage
class EventService extends Logging {}
object EventService extends Logging {
//private val logger = (new EventService()).logger
logger.info("Starting EventService...")
def startOne(item: EventMessage) = {
router ! item
}
def shutdown() = {
logger.info("Broadcast PoisonPill...")
router ! Broadcast(PoisonPill)
logger.info("EventService shut down.")
}
private lazy val actorSystem = ActorSystem("EventServiceLocalSystem", ConfigFactory.load("localjvm"))
private lazy val router = actorSystem.actorOf(Props[EventMessageActor].withRouter(FromConfig()), name = "EventMessageLocalRouter")
private lazy val routerWatcher =
actorSystem.actorOf(Props(new ActorWatcher(router)), name = "EventMessageLocalRouterWatcher")
}
Testing Class based on EventService
package com.sillycat.akka.server
import com.sillycat.akka.model.EventMessage
import org.joda.time.DateTime
import org.scalatest.BeforeAndAfter
import org.scalatest.FunSuite
class EventServiceTest extends FunSuite with BeforeAndAfter {
before {
}
after {
}
test("Testing EventService start one...") {
def item = EventMessage(1, "request1", "request2", "admin", DateTime.now())
Range(1, 10) foreach { i =>
EventService.startOne(item)
}
Thread.sleep(1000)
EventService.shutdown()
}
}
2. Remote Akka System
Users/carl/work/akka/akka/akka-samples/akka-sample-remote-scala/tutorial/index.html
Server Side EventServiceRemoteApp.
package com.sillycat.akka.server
import akka.actor.{ Props, ActorSystem }
import akka.kernel.Bootable
import com.sillycat.akka.actor.EventMessageActor
import com.typesafe.config.ConfigFactory
class EventServiceRemoteApp extends Bootable {
val system = ActorSystem("EventServiceRemoteSystem", ConfigFactory.load("remotesystem"))
def startup = {
system.actorOf(Props[EventMessageActor], name = "EventMessageRemoteActor")
}
def shutdown = {
system.shutdown()
}
}
The configuration file common.conf
akka {
# Options: ERROR, WARNING, INFO, DEBUG
loglevel = "DEBUG"
# Log the complete configuration at INFO level when the actor system is started.
# This is useful when you are uncertain of what configuration is used.
#log-config-on-start = on
actor {
serialize-messages = on
serializers {
java = "akka.serialization.JavaSerializer"
proto = "akka.remote.serialization.ProtobufSerializer"
}
serialization-bindings {
"java.lang.String" = java
"com.sillycat.akka.model.EventMessage" = java
}
}
}
configuration file remotesystem.conf
include "common"
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
netty.tcp {
hostname = "10.190.191.15"
port = 2552
}
}
}
After that, we need to build the assembly jar and place the jar under
/opt/akka/deploy
The binary Akka is downloaded from http://akka.io/downloads/.
Command to start the remote Akka system
> bin/akka com.sillycat.akka.server.EventServiceRemoteApp
Client System
EventServiceClientApp
package com.sillycat.akka.server
import akka.actor._
import akka.routing.{ Broadcast, FromConfig }
import com.sillycat.akka.actor.{ ActorWatcher, EventMessageActor }
import com.sillycat.akka.model.EventMessage
import com.typesafe.config.ConfigFactory
import com.typesafe.scalalogging.slf4j.Logging
import org.joda.time.DateTime
object EventServiceClientApp extends App {
val system = ActorSystem("EventServiceLocalSystem", ConfigFactory.load("clientsystem"))
val clientActor = system.actorOf(Props[EventMessageActor].withRouter(FromConfig()), "EventMessageClientActor")
private lazy val routerWatcher =
system.actorOf(Props(new ActorWatcher(clientActor)), name = "EventMessageClientRouterWatcher")
Range(1, 10) foreach { i =>
def item = EventMessage(1, "request1", "request2", "admin", DateTime.now())
clientActor ! "fire works."
clientActor ! item
}
Thread.sleep(5000)
clientActor ! Broadcast(PoisonPill)
system.shutdown()
}
The Client Conf, clientsystem.conf
include "common"
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
}
akka {
actor.deployment {
/EventMessageClientActor {
remote = "akka.tcp://EventServiceRemoteSystem@10.190.191.15:2552/user/EventMessageRemoteActor"
router = round-robin
resizer {
lower-bound = 10
upper-bound = 100
}
}
}
}
Directly run the command in project to testing.
> sbt "run com.sillycat.akka.server.EventServiceClientApp"
3. Akka with Spray
Build the actor in app when start the spray HTTP server
system.actorOf(Props[AttributeDBImportActor].withRouter(FromConfig()), name = "AttributeDBImportRouter")
Conf
akka {
# Options: ERROR, WARNING, INFO, DEBUG
loglevel = "ERROR"
# Log the complete configuration at INFO level when the actor system is started.
# This is useful when you are uncertain of what configuration is used.
#log-config-on-start = on
actor.deployment {
/AttributeDBImportRouter {
router = round-robin
resizer {
lower-bound = 8
upper-bound = 40
}
}
}
In the http service class, select the Actor from the Akka system
implicit val attributeActorRouter = actorRefFactory.actorSelection("/user/AttributeDBImportRouter") //actor name
4. Cluster Akka System
todo...
Users/carl/work/akka/akka/akka-samples/akka-sample-cluster-scala/tutorial/index.html
References:
old blog
相关推荐
7. **Akka Streams**:Akka不仅仅限于Actor,还包括了Streams模块,用于处理数据流。Akka Streams提供了低延迟、容错的数据处理流水线,与Actor系统无缝集成,适用于批处理和实时数据流处理场景。 8. **案例研究**...
Akka 的核心优势在于其对 Actor 模型的支持,通过将系统分解为大量独立运行的 Actor,Akka 能够有效地利用现代多核处理器的优势,并通过简单的消息传递机制来协调 Actor 之间的交互。 #### 二、Akka 分片集群基础...
##概述#####Hyrax 是Scala 版本,使用 scala Futures/Promises 和 Akka Actors/ActorSystems 作为依赖弹性层。 它使用 Akka IO 来实现远程依赖。 它最初只支持 Web 服务,并使用 Spray 来处理 HTTP 请求/响应。 ...
项目中融入了多个关键的技术组件,包括Akka、Spray和CamelActiveMQ,这些技术在分布式系统和企业级应用开发中扮演着重要角色。接下来,我们将深入探讨这些技术以及它们在微服务架构中的应用。 首先,Scala是一种...
- **akka-http-spray-json** 和其他 JSON 库集成:提供了与常见 JSON 库的互操作性。 - **akka-stream** 和 **akka-stream-testkit**:Akka 流处理库及其测试工具。 通过对源码的深入阅读,我们可以学习到 Akka ...
《构建基于Spray、Akka、Slick与Postgres的简单Web服务》 在现代的Web开发中,高效、可扩展且易于维护的架构至关重要。本项目“spray-akka-slick-postgres”提供了一个实用的例子,展示了如何使用Scala语言,结合...
微服务原型 A 具有 Scala、Akka、Spray 和 Camel/ActiveMQ 的微服务原型。 基于 Typesafe Activator 模板。 项目包含: 使用示例 actor 轻松测试 Akka 系统具有完整 CORS 支持的基于喷雾的 RESTful API Actor 和 API...
Akka的核心概念是Actor模型,它提供了一种处理并发问题的方式,将复杂性封装在独立的Actor中,每个Actor都有自己的状态和消息队列,通过异步消息传递进行通信。这种模型使得sprawler可以并行处理大量HTTP请求,提高...
Akka HTTP是一个强大的HTTP服务器和客户端库,由Lightbend公司开发,它是基于Akka Actor系统构建的。在Scala世界中,JSON处理是常见的数据交换格式,因此将JSON库与Akka HTTP集成对于构建Web服务至关重要。"akka-...
Akka 是一个用于构建高并发、分布式和容错系统的工具包,基于 Actor 模型。它提供了轻量级线程(Actors)和消息传递机制,帮助开发者构建可扩展、健壮的应用程序。在 "crudproductosASM" 中,Akka 可能用于处理并发...
6. **工具与集成**:Akka 与多种技术,如 Scala、Java、 Spray(用于构建 RESTful 服务)和 Play 框架等,有着良好的集成,提供了丰富的工具链。 **JavaScript 和 Akka** 虽然 Akka 主要是在 JVM 上运行,但 ...
Akka是基于actor模型的并行和分布式计算框架,用Scala编写,但也可以与Java和Scala一起使用。在登录系统中,akka可以处理高并发和异步请求,确保系统的稳定性和可扩展性。而"spray"是构建在Akka之上的HTTP服务工具包...
在Akka Tracing中,每个消息传递或者actor交互都会被记录为一个span,这些span组成了一条完整的调用链。每个span包含了时间戳、操作名、元数据等信息,通过这些信息,我们可以清晰地看到请求从源头到目的地的完整...
3. **非阻塞I/O**:利用Akka框架的Actor模型,实现非阻塞I/O,提高了系统吞吐量。 4. **与Scala无缝集成**:作为Scala库,spray能够充分利用Scala的类型安全性和表达能力,简化开发过程。 然而,业界还有其他类似的...
具有 application/json 内容类型标头的 /entity POST 请求将使用 Spray 的 json4s marhsalling 支持创建一个 JObject,该 JObject 传递给另一个 Actor,HttpResponse 通过未来返回给 Spray。 以下是一些您可以尝试...
1. **Akka框架**:Akka是基于Actor模型的并发处理框架,由Lightbend公司开发。它提供了强大的工具来管理并发和分布式系统,使开发者能够构建高度可扩展、容错性强的应用程序。在`akka-trading`中,Akka被用来处理...
Akka HTTP是基于Akka Actor模型构建的,它提供了低级的HTTP和WebSocket支持,可用来构建高性能、响应式的Web服务。其异步I/O模型和流处理能力使得处理大量并发请求变得轻而易举。然而,仅仅处理HTTP请求和响应还不够...
带有Akka Streams / HTTP的简单WebSocket服务预定的Actor每5秒检查一次外部Web服务的转换率仅在上次更改汇率后,才将汇率存储在数据库中WebSocket流所有存储的值。 一旦传输了所有可用的存储数据,它将继续流式传输...
"knol-spray-auth" 项目可能是基于 Akka 和 Spray 构建的一个示例或模板,专注于身份验证(Authentication)功能。在 REST API 中,身份验证是确保只有授权用户能够访问受保护资源的关键步骤。该项目可能演示了如何...
此外,了解如何在Akka中设计Actor系统,以及如何使用Spray定义RESTful API,都是重要的技能。同时,熟悉构建工具如Activator和SBT,以及理解身份管理系统的设计原理和实现,对提升开发者在Web开发领域的专业能力...