`
xiefeifeihu
  • 浏览: 97230 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Akka2使用探索1(Remoting)

阅读更多

 

akka从1.2升级到现在的2.0.2后有了很大的改变。现在摸索一下如何使用。

 

Remoting可以方便地用于服务器之间通信。akka1.2可以使用clientActor.sendRequestReply将消息发送到服务器端,并且同步获取服务器端的返回消息。但是akka2已经不能这么用了,akka2使用tell方法给另一个Actor发消息。

tell有两个重载方法:

/**
* Sends the specified message to the sender, i.e. fire-and-forget semantics.<p/>
* <pre>
* actor.tell(message);
* </pre>
*/
final def tell(msg: Any): Unit = this.!(msg)(null: ActorRef)

只发送,不管其他,也不接收相应。actor.tell(msg)----给actor发送msg消息

 

/**
* Java API. <p/>
* Sends the specified message to the sender, i.e. fire-and-forget
* semantics, including the sender reference if possible (not supported on
* all senders).<p/>
* <pre>
* actor.tell(message, context);
* </pre>
*/
final def tell(msg: Any, sender: ActorRef): Unit = this.!(msg)(sender)

只发送消息,并告诉对方你可以给我指定的sender回复消息。actor.tell(msg,sender)----给actor发送msg消息,actor处理时可以给sender回复消息。

所以akka2中actor是对等的。有消息来回发送的akka应用最好部署在对等互通的网络环境中。如果一个应用部署在内网,一个部署在外网,那么内网的actor给外网发消息没问题,但是外网的actor想给内网的actor回复消息就不行了。

 

另外,因为remoting的host最好指定成具体的ip或域名或hostname,不要指定成”0.0.0.0”,这样才方便定位远程actor。host和port在配置文件中指定。

注意区别Deploy,Deploy是指将本地Actor发布到远程akka Server上,withDeploy的参数RemoteScope指的是远程Server的ip、端口、sysName。并不是启动本地Server。

serverActor = system.actorOf(new Props(Server.class).withDeploy(new Deploy(new RemoteScope(new Address("akka", Server.AkkaSystemName, "127.0.0.1", 8888);))), "simple");

客户端可以直接用serverActor发消息,也可以通过路径查找,用actorFor(akka://${远程sysName}@${远程akka服务地址}/remote/${被发布的sysName}@${被发布的akka的地址(ip、端口)}/user/${actorOf第二个参数指定的路径})发消息。注意消息是在服务端收到。

 

现在来看看具体的例子:

 

首先引用typesafe的仓库:

<repository>
<id>typesafe-releases</id>
<url>http://repo.typesafe.com/typesafe/releases</url>
</repository>

 

加入依赖包:<akka.version>2.0.2</akka.version>

<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor</artifactId>
<version>${akka.version}</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-remote</artifactId>
<version>${akka.version}</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-kernel</artifactId>
<version>${akka.version}</version>
</dependency>

 

首先定义ServerActor:

import akka.remote.RemoteScope
import akka.actor.*
import com.typesafe.config.ConfigFactory
class Server extends UntypedActor {
static final String AkkaSystemName = "xw"
LoggingAdapter log = Logging.getLogger(getContext().system(), this);

@Override
void onReceive(Object message) {
log.debug("server收到消息----${message}----self:${getSelf()},sender:${getSender()}")
getSender().tell(message)//给客户端actor发送消息
}

}

然后定义服务端应用,来启动ServerActor

import akka.kernel.Bootable
import akka.actor.ActorSystem
import akka.actor.Props
import akka.actor.ActorRef
import akka.actor.Address
import akka.actor.Deploy
import akka.remote.RemoteScope
import com.typesafe.config.ConfigFactory

class ServerApp implements Bootable {
ActorSystem system
ActorRef serverActor

ServerApp() {
system = ActorSystem.create(Server.AkkaSystemName, ConfigFactory.load().getConfig("server"))
Address addr = new Address("akka", Server.AkkaSystemName, "10.68.15.16", 8888);
serverActor = system.actorOf(new Props(Server.class), "server");

// serverActor = system.actorOf(new Props(Server.class).withDeploy(new Deploy(new RemoteScope(addr))), "simple");
}

void startup() {

}

void shutdown() {
system.shutdown()
}

}
指定它加载“server”配置。需要在classpath下的application.conf中添加:

server {
akka {
//loglevel = "DEBUG"
actor {

provider = "akka.remote.RemoteActorRefProvider"//这里指定使用RemoteActor

}
remote {
transport = "akka.remote.netty.NettyRemoteTransport"
netty {
hostname = "10.68.15.16"//"0.0.0.0"//指定系统绑定的host
port = 8888//指定系统绑定的端口
}
}
}
}
现在只需要new ServerApp()将服务启动即可。

 

客户端很简单:

ActorSystem.create(Server.AkkaSystemName, ConfigFactory.load().getConfig("client")).actorFor("akka://xw@10.68.15.16:8888/user/server").tell("hello,I'm client")

客户端也需要加载配置文件:

client {
akka {

actor {
provider = "akka.remote.RemoteActorRefProvider"

}

}
}

这里需要注意:

1、必须指定provider为RemoteActorXXX

2、客户端只发不收消息时,ActorSystem的name没有要求。需要收消息时,如果和服务端相同则可能出问题。遇到问题时改下ActorSystem的那么试一下。

 

经过试验,如果服务端启多个ActorSystem和akka端口是没问题的。akka1.2中同一个进程不能启多个akka端口。

0
1
分享到:
评论
1 楼 蜗居之家 2016-05-18  
akka2.0的如何实现1.2的功能 onReceive中接收另一个actor返回的数据 把数据返回给上级调用

相关推荐

    ChatBox:使用 Akka Actors 的简单聊天系统(Akka Remoting)

    使用 Akka Actors 的简单聊天系统(Akka Remoting) 聊天框演员 管理客户端参与者和客户端参与者注册到聊天框参与者。 ChatBox 演员的 IP 可供客户端演员使用。 ChatBox actor 监视每个向其注册的 Client actor,并...

    Learning Akka(PACKT,2015)

    Akka is a distributed computing toolkit that enables developers to build correct concurrent and distributed applications using Java and Scala with ease, applications that scale across servers and ...

    akka2.0使用方法

    just neet some points to download

    akka-kryo-serialization, 基于Kryo的Akka序列化.zip

    akka-kryo-serialization, 基于Kryo的Akka序列化 akka-kryo-serialization-- Scala 和Akka基于kryo的序列化程序这个库为 Scala 和Akka提供定制的基于kryo的序列化程序。 它可以用于更高效的akka远程处理。它还可以...

    Akka 基础学习pdf中文文档

    第 1 章 初识 Actor:Akka 工具集以及 Actor 模型的介绍。 第 2 章 Actor 与并发:响应式编程。Actor 与 Future 的使用。 第 3 章 传递消息:消息传递模式。 第 4 章 Actor 的生命周期—处理状态与错误:Actor 生命...

    akka_2.10-2.314

    akka_2.10

    AKKA 本质 《Akka Essentials》

    Akka Essentials,学习akka很好的一本书

    Akka入门与实践

    第 1 章 初识 Actor:Akka 工具集以及 Actor 模型的介绍。 第 2 章 Actor 与并发:响应式编程。Actor 与 Future 的使用。 第 3 章 传递消息:消息传递模式。 第 4 章 Actor 的生命周期—处理状态与错误:Actor 生命...

    akka实例参考

    初学akka使用实例,有很好的帮助啊,可实际运行

    Akka.in.Action.2016.9.pdf

    Akka in Action shows you how to build message-oriented systems with Akka. This comprehensive, hands-on tutorial introduces each concept with a working example. You’ll start with the big picture of ...

    akka-http-rest, 在 akka http上使用灵活REST服务编写示例.zip

    akka-http-rest, 在 akka http上使用灵活REST服务编写示例 Akka平滑REST服务模板 例如展示如何使用Akka和Slick在Lightbend堆栈上创建反应性REST服务。示例包含实体交互的完整REST服务。插件功能:CRUD操作实体部分...

    akka java实现tcp远程调用

    akka实例 java实现tcp远程调用,一个服务端,一个客户端

    akka-quartz, 因为用Camel来安排Akka演员是愚蠢.zip

    akka-quartz, 因为用Camel来安排Akka演员是愚蠢 akka石英Akka调度程序有限,并且使用 Apache camel 运行计时器是愚蠢的。 特性石英调度程序Akka演员们Fin版本使用 Akka 2.1.x 在 Scala 2.10.x/2.11.x 上使用

    Learning Akka

    Learning Akka Learning Akka Learning AkkaLearning Akka

    Akka 实战 akka in action v13 2014版本

    akka 实战。akka in action。v13 2014新版。 互联网技术入门必备 清晰,非扫描。

    java餐饮管理源码-akka:使用gradle搭建akkademo,语言是scala

    二、Akka简单使用 1. 从创建一个scala项目说起 在D盘新建一个项目目录,比如说叫AkkaDemo。 进入AkkaDemo目录,新建一个build.gradle文件,并在文件中输入一下内容: apply plugin: 'idea' apply plugin: 'scala' ...

    Applied.Akka.Patterns

    Chapter 1. The Actor Model Chapter 2. Introducing Akka Chapter 3. Distributed Domain-Driven Design Chapter 4. Good Actor Design Chapter 5. Good Data Flow Chapter 6. Consistency and Scalability Chapter...

    akkajava.pdf

    Akka is Open Source and available under the Apache 2 License. Download from http://akka.io/downloads. Please note that all code samples compile, so if you want direct access to the sources, have a ...

    akka-actor-2.11-2.5.19-API文档-中文版.zip

    赠送jar包:akka-actor_2.11-2.5.19.jar; 赠送原API文档:akka-actor_2.11-2.5.19-javadoc.jar; 赠送源代码:akka-actor_2.11-2.5.19-sources.jar; 赠送Maven依赖信息文件:akka-actor_2.11-2.5.19.pom; 包含...

    Akka Concurrency

    Akka Concurrency

Global site tag (gtag.js) - Google Analytics