High-performance, distributed fault tolerant framework AKKA find out?

Introduction Framework

AKKA is a high performance, distributed & parallel application framework high fault tolerance, follow Apache2 open source license, based on the realization of the classical Actor concurrency model, the underlying Scala language by providing Java and Scala API.

  • Parallel and Concurrent: Provides highly parallel and concurrent abstraction.
  • Non-blocking asynchronous: Akka-Actor asynchronous message communication is based on the non-blocking.
  • High fault tolerance: to provide strong fault-tolerant distributed processing model across multiple JVM's, never known the machine.
  • Persistence: Actor carrying state or message may be persistent, so that the state can be restored in the JVM crash.
  • Lightweight: Each Actor only about 300bytes, 1G memory can accommodate nearly 3 million Actor.



E-mail: Actor Each has its own mailbox, messages sent by other Actor will enter the mailbox. Akka comes with a variety of types of mail also provides an interface to customize the mailbox. (Default mailbox type UnboundedMailbox, the bottom of the ConcurrentLinkedQueue, non-blocking, non-bound queue)

Route: In addition to the message sent by an ordinary Actor, may be sent by routing. Routing strategies polling, broadcasting and so on. Routing can also be an Actor.

State persistence: log heap-based memory, storage-based snapshot and the local file system based LevelDB log.

Network: Actor for remote and distributed cluster, containing I / O, network communication (TCP / UDP), a sequence of contents arranged, distributed communication protocol (Gossip), the management node, the cluster fragment and the like.

HTTP modules: Akka provides a simple-to-use HTTP module that supports full HTTP server and client development, to quickly build a strong performance Rest Web services.

Actor model

Actor is a distributed concurrent programming model proposed in 1973, the design is based on message-driven and non-blocking. Everything considered Actor.

Actor is the core concept of Akka, is the most basic execution units. Actor that the smallest unit is a parallel computation Actor instance, but each instance has its own state and behavior in a large system, there may be thousands Actor instance, the communication between them by way of the message, each Actor can send messages to other Actor, Actor also receive messages from the other.

Reference article:

https://www.jianshu.com/p/d803e2a7de8eActor

Actor Features

Quote: not through the traditional "new" direct way to create a Actor object, returns object via a ActorRef actorOf or actorSelection etc., the subject may exist locally, may also be present in the remote node.

Status: Actor at different times may have different states, which are represented by variables, can be long-lasting operation.

Behavior: Actor has the ability to receive and send messages, each time after it receives a message, a business operation can be performed, but also can forward messages to other nodes for processing.

Regulatory strategies: Actor system is a hierarchical structure, when the task is apportioned to a child Actor Actor, Actor parent to have the child Actor's regulatory obligations. When regulation, we need to choose a different treatment options (such as stop, restart, or restore failures traced back) and strategies according to different situations.

Security Thread: Actor running on the thread pool, for each abstract AKKA Actor lightweight performing a "thread", a single thread is always safe Actor, and itself when processing the received message is serial.

Lightweight: in Akka, each Actor occupy only about 300 bytes, supports distributed mode.

Actor definitions

In the Akka, Actor roughly divided into two categories:

  • UntypedActor: Actor classical model-based implementation, message-driven, it is recommended to define the Actor.
  • TypedActor: the normal OOP code packaged Actor asynchronous execution, more in line with API call logic programmer, but use a little complicated, this will not discuss.

Defined by a succession UntypedActor Actor, onReceive method for receiving and processing messages place, the message types match, when not match the corresponding message type, using the unhandled processing.


Actor creation

First, download the dependencies needed

<dependency>    
    <groupId>com.typesafe.akka</groupId>    
    <artifactId>akka-actor_2.11</artifactId>  
    <version>2.4.1</version>
</dependency>
<dependency>
    <groupId>com.typesafe.akka</groupId>   
    <artifactId>akka-remote_2.11</artifactId>   
    <version>2.4.1</version>
</dependency>复制代码

Actor created by actorOf method ActorSystem of:

ActorSystem system = ActorSystem.create(“hiseePS”); ActorRef actorRef = system.actorOf(Props.create(ActorDemo.class),"actorDemo");

  • actorOf not return Actor itself, but ActorRef, namely Actor references, we are used to communicate messages by this reference.
  • If the constructor has parameters Actor can create by methods successor.
  • Created by ActorSystem is a top Actor (ie branch Actor in / user), if you want to create a hierarchy of Actor network, the task to the child Actor to deal with, the parent is responsible to supervise children. You can create sub Actor Actor use of getContext.

ActorRef childActor = getContext().actorOf(Props.create(ChildActor.class ),"childActor");


Actor After being created each has its own path, the path follows the hierarchy ActorSystem: Akka: // mysys / the User / parentActor / childActor akka.tcp: //[email protected]: 2554 / the User / parentActor / childActor

mysys is ActorSystem name, / user is the user's default root created Actor, parentActor and childActor are father and son Actor.

Actor messages

ActorRef objects through message communication and Actor, Actor There are two ways to send a message, tell and ask. They are messages sent in an asynchronous manner, except that the former is made immediately after the return, and the latter returned to expect a result, if not the time (the Timeout) returns the result set, the sender will receive a timeout exception . Any message sent to the Actor will receive by the receive method.

tell 方法:actorRef.tell("Hello Akka", ActorRef.noSender());

The first parameter is the "message", which may be any sequence of objects or data, the second parameter indicates the sender, i.e. a reference to another Actor, noSender () indicates no sender (the default deadLetters Actor) . If you want to get inside the Actor message sender, you can call getSender () method. After the call to tell method, the Actor will asynchronously to process the message, does not block the subsequent operation code.

Method ask: When we need to get a return result from the Actor, can ask, which is a typical "request-response" mode. ask packaging method will return a result in scala.concurrent.Future in (Future mode can be understood as a thread), may (blocking) to obtain this value, you can obtain this value through asynchronous callback function, taking into account performance issues, we tend to adopt the latter.

Ask test method


Actor message forwarding

Message forwarding:

In addition to sending messages directly to an Actor, but we can also forward by way of message forwarding, typical usage scenario is for message routing and load functions, message sender will not change.


Other Uses

1. For existing already Actor, we can find the path to

ActorSelection=[ActorSystem/ActorContext].actorSelecton([path]);

2.Actor switching behavior: Sample Code (BecomeActor)

3.Actor life cycle

Actor runtime will go through different stages, such as creating, running, restart and destruction, process or condition of this series, we call it life cycle.

4. Monitoring and fault tolerance

5.Circuit Breaker (fuse)

Not presented Interested students can find out yourself.

Well, briefly AKKA on here for the first time to write, write well but also look pointing.


Guess you like

Origin juejin.im/post/5d8accd66fb9a04e247c7871