当前位置: 首页 > article >正文

Spring Boot集成Akka remoting快速入门Demo

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),如下图所示:

akka-remoting

上图中,连接到一个远程系统的过程中,包括上面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 = on
      transport = tcp
      canonical.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 {
        @Override
        public 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 = on
      transport = tcp
      canonical.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;
        }

        @Override
        public void preStart() {
            ActorSelection serverActor = getContext().actorSelection(serverActorPath);
            serverActor.tell("Hello from Client", getSelf());
        }

        @Override
        public 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.kler.cn/news/313872.html

相关文章:

  • Spring-关于IOC的解释及相关理解(如何从三个方面理解)
  • 计算机人工智能前沿进展-大语言模型方向-2024-09-16
  • leetcode 380.O(1) 时间插入、删除和获取随机元素
  • 电脑ip会因为换了网络改变吗
  • JavaScript web API完结篇---多案例
  • Abp vNext(五)集成MQTTnet,可收发消息
  • 【楚怡杯】职业院校技能大赛 “云计算应用” 赛项样题六
  • Android架构组件中的MVVM模式实战应用与数据绑定技巧
  • Python精选200Tips:156-160
  • 力扣刷题--3033. 修改矩阵【简单】
  • 【sgCreateCallAPIFunctionParam】自定义小工具:敏捷开发→调用接口方法参数生成工具
  • 执行 npm报错 Cannot find module ‘../lib/cli.js‘
  • 电脑技巧:Win11家庭版和专业版之间的区别详解
  • KVM环境下制作ubuntu qcow2格式镜像
  • xml中的转义字符
  • 【我的 PWN 学习手札】tcache stash with fastbin double free —— tcache key 绕过
  • 前端web端项目运行的时候没有ip访问地址
  • 【信创】Linux上如何创建和管理自定义的 systemd 服务 _ 统信 _ 麒麟 _ 方德
  • 滑动窗口算法专题(1)
  • pgsql 分组查询方法
  • Python基础知识——字典排序(不断补充)
  • 数据库课程设计mysql
  • python-SZ斐波那契数列/更相减损数
  • 【Python】Anaconda插件:Sublime Text中的Python开发利器
  • 【数据结构初阶】链式二叉树接口实现超详解
  • InnoDB锁机制全解析
  • VScode快速配置c++(菜鸟版)
  • 基于SpringBoot的招生宣传管理系统【附源码】
  • 计算机网络(Hub 集线器、交换机、路由器)
  • linux-安全管理-防火墙与网络安全