Spring Boot集成Akka remoting快速入门Demo

news/2024/9/23 2:33:13/

1.什么是Akka remoting?

Akka-Remoting一种ActorSystem之间Actor对Actor点对点的沟通协议.通过Akka-Remoting来实现一个ActorSystem中的一个Actor与另一个ActorSystem中的另一个Actor之间的沟通

Akka Remoting限制:

  • 不支持NAT(Network Address Translation)
  • 不支持负载均衡器(Load Balancers)

Akka提供了种方式来使用Remoting功能:

  • 通过调用actorSelection方法搜索一个actor,该方法输入的参数的模式为:akka.<protocol>://<actor system>@<hostname>:<port>/<actor path>
  • 通过actorOf方法创建一个actor

下面看一下Remoting系统中故障恢复模型(Failure Recovery Model),如下图所示:

<a class=akka-remoting" height="606" src="https://img-blog.csdnimg.cn/img_convert/980fd5f18faab1b12b3f25e9c9c44ac2.png" width="632" />

上图中,连接到一个远程系统的过程中,包括上面4种状态:在进行任何通信之前,系统处于Idle状态;当第一次一个消息尝试向远程系统发送,或者当远程系统连接过来,这时系统状态变为Active;当两个系统通信失败,连接丢失,这时系统变为Gated状态;当系统通信过程中,由于参与通信的系统的状态不一致导致系统无法恢复,这时远程系统变为Quarantined状态,只有重新启动系统才能恢复,重启后系统变为Active状态。

Akka Remoting功能:

Akkaremoting完全配置化了,使用时几乎只需要修改配置文件,除非自定义,否则不需要动一行代码。 remoting包提供了两个功能:

  • 查找一个已存在的远程Actor
  • 在指定的远程路径上创建一个远程Actor

2.代码管理

实验目标

实现客户端和服务端通信

pom.xml

<!-- Akka Streams -->
<dependency><groupId>com.typesafe.akka</groupId><artifactId>akka-stream_2.13</artifactId><version>2.6.0</version>
</dependency>

server

server.conf

akka {actor {provider = "akka.remote.RemoteActorRefProvider"}remote {artery {enabled = ontransport = tcpcanonical.hostname = "127.0.0.1"canonical.port = 2552}}
}
package com.et.akka.remoting;import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import com.typesafe.config.ConfigFactory;public class ServerApp {static class ServerActor extends AbstractActor {@Overridepublic Receive createReceive() {return receiveBuilder().match(String.class, msg -> {System.out.println("Server received message: " + msg);getSender().tell("Hello from Server", getSelf());}).build();}}public static void main(String[] args) {ActorSystem system = ActorSystem.create("RemoteSystem", ConfigFactory.load("server"));ActorRef serverActor = system.actorOf(Props.create(ServerActor.class), "serverActor");System.out.println("Server is running...");}
}

client

client.conf

akka {actor {provider = "akka.remote.RemoteActorRefProvider"}remote {artery {enabled = ontransport = tcpcanonical.hostname = "127.0.0.1"canonical.port = 0}}
}
package com.et.akka.remoting;import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.actor.ActorSystem;
import akka.actor.Props;
import com.typesafe.config.ConfigFactory;public class ClientApp {static class ClientActor extends AbstractActor {private final String serverActorPath;public ClientActor(String serverActorPath) {this.serverActorPath = serverActorPath;}@Overridepublic void preStart() {ActorSelection serverActor = getContext().actorSelection(serverActorPath);serverActor.tell("Hello from Client", getSelf());}@Overridepublic Receive createReceive() {return receiveBuilder().match(String.class, msg -> {System.out.println("Client received message: " + msg);}).build();}}public static void main(String[] args) {ActorSystem system = ActorSystem.create("RemoteSystem", ConfigFactory.load("client"));String serverPath = "akka://RemoteSystem@127.0.0.1:2552/user/serverActor";ActorRef clientActor = system.actorOf(Props.create(ClientActor.class, serverPath), "clientActor");System.out.println("Client is running...");}
}

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

  • GitHub - Harries/springboot-demo: a simple springboot demo with some components for example: redis,solr,rockmq and so on.(akka)

3.测试

  1. 启动server
  2. 启动client
  3. 产看日志

server

Server is running...
Server received message: Hello from Client

client

Client is running...
Client received message: Hello from Server

4.引用

  • https://doc.akka.io/docs/akka/current/remoting.html
  • https://doc.akka.io/docs/akka/current/remoting-artery.html#selecting-a-transport
  • Spring Boot集成Akka remoting快速入门Demo | Harries Blog™

http://www.ppmy.cn/news/1529112.html

相关文章

SQLiteDatabase insert or replace数据不生效

在Android开发中&#xff0c;如果您在SQLite数据库中更新了数据&#xff0c;但重启应用后更新的数据不再生效&#xff0c;那么可能的原因有&#xff1a; 更新操作没有正确执行&#xff0c;可能是由于SQL语句错误或者数据库没有正确打开。 更新操作在事务中没有被正确提交。 更…

qt--Qml控件库如何从外部导入

文章目录 两种方案方案1 给项目添加子项目方案2 使用pri文件 综合来说 &#xff1a; 两种方案 方案1 给项目添加子项目 利用git的特性 对应的子项目就是我们的控件库 然后需要哪个控件 在父项目的qrc路径进行导入 即可将控件库里面的控件给导入项目 在使用的时候 使用模…

[Python学习日记-26] Python 中的文件操作

[Python学习日记-26] Python 中的文件操作 简介 操作模式 循环文件 其他功能 混合模式 修改文件 简介 在 Python 中的文件操作其实和我们平时使用的 Word 的操作是比较类似的&#xff0c;我们先说一下 Word 的操作流程&#xff0c;流程如下&#xff1a; 找到文件&#x…

基于vue框架的宠物托管系统设计与实现is203(程序+源码+数据库+调试部署+开发环境)系统界面在最后面。

系统程序文件列表 项目功能&#xff1a;用户,宠物种类,商家,咨询商家,用户宠物,宠物托管,宠物状况,宠物用品,用品分类,商家公告,结束托管,账单信息,延长托管 开题报告内容 基于Vue框架的宠物托管系统设计与实现开题报告 一、引言 随着现代生活节奏的加快&#xff0c;越来越…

发现编程的全新境界——明基RD280U显示器使用体验

前言 在大学的四年里&#xff0c;我几乎每天都泡在实验室&#xff0c;盯着电脑屏幕&#xff0c;一行行地码代码。那时&#xff0c;学校提供的显示器是非常基础的款式&#xff0c;功能简单&#xff0c;几乎没有任何特别之处&#xff0c;甚至配置也比较低。那个时候&#xff0c;…

用Python打造互动式中秋节庆祝小程序

中秋节&#xff0c;这个充满传统韵味的节日&#xff0c;不仅是家人团聚的时刻&#xff0c;也是程序员展示创意的好机会。本文将引导您使用Python创建一个互动式中秋节庆祝小程序&#xff0c;它不仅能够展示节日祝福&#xff0c;还能通过一些简单的特效增加节日气氛。 文章目录 …

python爬虫初体验(一)

文章目录 1. 什么是爬虫&#xff1f;2. 为什么选择 Python&#xff1f;3. 爬虫小案例3.1 安装python3.2 安装依赖3.3 requests请求设置3.4 完整代码 4. 总结 1. 什么是爬虫&#xff1f; 爬虫&#xff08;Web Scraping&#xff09;是一种从网站自动提取数据的技术。简单来说&am…

VS Code终端命令执行后老是出现 __vsc_prompt_cmd_original: command not found

VS Code终端命令执行后老是出现 __vsc_prompt_cmd_original: command not found。 如下图&#xff08;vscode终端中&#xff09;&#xff1a; 解决方案&#xff1a; 1、vim ~/.bashrc 2、在~/.bashrc里面加入命令&#xff1a;unset PROMPT_COMMAND 3、source ~/.bashrc