- 作者:老汪软件技巧
- 发表时间:2024-09-13 15:01
- 浏览量:
1.什么是akka?
AKKA 是一个用于构建高并发、分布式和容错应用程序的开源框架。它基于Actor模型,提供了强大的并发抽象和工具,适用于各种业务场景。以下是一些使用AKKA框架的常见业务场景的示例:
Actor模型简介
Actor由状态(state)、行为(Behavior)和邮箱(mailBox)三部分组成
Actor 模型及其说明
Akka 处理并发的方法基于 Actor 模型。(示意图)在基于 Actor 的系统里,所有的事物都是 Actor,就好像在面向对象设计里面所有的事物都是 对象一样。Actor 模型是作为一个并发模型设计和架构的。Actor 与 Actor 之间只能通过消息通信,如图 的信封Actor 与 Actor 之间只能用消息进行通信,当一个 Actor 给另外一个 Actor 发消息,消息是有 顺序的 (消息队列),只需要将消息投寄的相应的邮箱即可。怎么处理消息是由接收消息的 Actor 决定的,发送消息 Actor 可以等待回复,也可以异步处理 【ajax】ActorSystem 的职责是负责创建并管理其创建的 Actor, ActorSystem 是单例的 (可以 ActorSystem 是一个工厂,专门创建 Actor),一个 JVM 进程中有一个即可,而 Acotr 是可以有多个的。Actor 模型是对并发模型进行了更高的抽象。Actor 模型是异步、非阻塞、高性能的事件驱动编程模型。[案例:说明 什么是异步、非阻塞,最 经典的案例就是 ajax 异步请求处理]Actor 模型是轻量级事件处理 (1GB 内存可容纳百万级别个 Actor),因此处理大并发性能高.2.代码工程实验目的
基于AKKA actor模型编程
pom.xml
"1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-demoartifactId>
<groupId>com.etgroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>akkaartifactId>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-autoconfigureartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>com.typesafe.akkagroupId>
<artifactId>akka-actor_2.13artifactId>
<version>2.6.0version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
dependencies>
project>
actor
传递string和int参数
package com.et.akka.actor;
import akka.actor.AbstractActor;
public class ActorNormal extends AbstractActor {
//process msg
@Override
public Receive createReceive() {
//Process a specific type of message, such as a string type message
Receive build = receiveBuilder().match(String.class,(msg)-> {
System.out.println(msg);
sender().tell("response", self());
}).match(Integer.class,(msg)-> {
System.out.println(msg+"1");
}).build();
return build;
}
}
传递对象参数
package com.et.akka.actor;
import akka.actor.AbstractActor;
import com.et.akka.model.User;
public class ActorStruct extends AbstractActor {
private final User user;
public ActorStruct(User userModel){
this.user = userModel;
}
//process msg
@Override
public Receive createReceive() {
Receive build = receiveBuilder().match(String.class,(msg)-> {
System.out.println(msg);
sender().tell(" I am a result of ActorStruct:"+user.getName(), self());
}).match(Integer.class,(msg)-> {
System.out.println(msg+"1");
}).build();
return build;
}
}
controller
package com.et.akka.controller;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.pattern.Patterns;
import akka.util.Timeout;
import com.et.akka.actor.ActorNormal;
import com.et.akka.actor.ActorStruct;
import com.et.akka.model.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import scala.concurrent.Await;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;
import java.util.concurrent.TimeUnit;
@RestController
public class AkkaController {
@GetMapping(value = "/Akka/AkkaSendString")
@ResponseBody
public void AkkaSendString() {
//Creates system management objects for all management actors
ActorSystem actorSystem = ActorSystem.create();
//use actorSystem.actorOf to define actorNormal as ActorRef
ActorRef actor = actorSystem.actorOf(Props.create(ActorNormal.class), "actorNormal");
//Send message Object msg (the content of the message, any type of data), final ActorRef sender (indicates that there is no sender (actually an Actor called deadLetters))
actor.tell("kiba", ActorRef.noSender());
}
@GetMapping(value = "/Akka/AkkaSendInt")
@ResponseBody
public void AkkaSendInt() {
ActorSystem actorSystem = ActorSystem.create();
ActorRef actor = actorSystem.actorOf(Props.create(ActorNormal.class), "actorNormal");
actor.tell(518, ActorRef.noSender());//send int
}
@GetMapping(value = "/Akka/AkkaAsk")
@ResponseBody
public void AkkaAsk() {
ActorSystem actorSystem = ActorSystem.create();
ActorRef actor = actorSystem.actorOf(Props.create(ActorNormal.class), "actorNormal");
Timeout timeout = new Timeout(Duration.create(2, TimeUnit.SECONDS));
Future
model
package com.et.akka.model;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* @author liuhaihua
* @version 1.0
* @ClassName User
* @Description todo
* @date 2024/09/11/ 9:45
*/
@Data
@AllArgsConstructor
public class User {
private int age;
private String name;
}
以上只是一些关键代码,所有代码请参见下面代码仓库
代码仓库3.测试启动Spring Boot应用访问:8088/Akka/AkkaSendString访问:8088/Akka/AkkaSendInt访问:8088/Akka/AkkaAsk访问:8088/Akka/AkkaAskStruct4.引用