📜 ⬆️ ⬇️

Introduction to Akka.NET

What is Akka?


Scalable, distributed transaction processing in real time


We believe that writing correct, parallel, fault-tolerant and scalable applications is difficult.

In most cases, this is due to the fact that we use the wrong tools and the wrong level of abstraction. Akka was created to change this.

Using the actor model, we raise the level of abstraction and provide the best platform for building scalable, robust, and flexible applications (see Reactive Manifesto for details).
')
To ensure resiliency, we use the “let it fall” model, which is used with great success in the telecommunications industry to create self-healing applications and systems that should never stop. Actors also provide a transparent level of abstraction for distribution and a base for truly scalable and fault-tolerant applications.

Akka.NET is an open source project and is available under the Apache 2 License - explained here .

Unique hybrid


Actors


Actors give you:


fault tolerance



Transparent location


Everything in Akka is designed to work in a distributed environment: any interaction between actors uses simple message passing and everything is asynchronous.

Clustering support is currently in beta. See here for details .

Persistence


Akka.Persistence is currently in beta and is actively developing. (ppts in the documentation link is broken, but on the githaba it is simply written that " not yet implemented ").

Why Akka?


What are the advantages in Akka.NET over competitors?


Akka.NET provides scalable, distributed, real-time transaction processing.

Akka.NET is a unified runtime and programming model for:


Akka.NET is a very scalable piece of software; not only in the context of performance, but also in the size of the applications for which it is used. The core of Akka.NET, Akka.NET-actor, is very small and can be easily put into an existing project, in which you need asynchrony and non-blocking competition.

A good example of using Akka.NET?


We see how Akka.NET is used by many large organizations in a wide range of industries:


Actors allow you to manage failures (managers), manage load (delay strategies, time-outs, and process isolation), as well as horizontal and vertical scalability (add more cores and / or add more machines).

That's what some of Akka users say .

Getting Started with Akka.NET


This tutorial is an introduction to using Akka.NET. Here a simple Greeter (Welcoming) actor is created using C #.

Project Setup


Run visual studio and create a new C # console application. Then open the package manager console and type:
PM> Install -Package Akka

You can also use NuGet
image

Then you need to add using:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; //     using Akka; using Akka.Actor; namespace ConsoleApplication11 { class Program { static void Main(string[] args) { } } } 

Creating your first actor


First of all, you need to create a message type to which our actor will respond:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Akka; using Akka.Actor; namespace ConsoleApplication11 { //  ()  ,       public class Greet { public Greet(string who) { Who = who; } public string Who { get;private set; } } class Program { static void Main(string[] args) { } } } 

After we have a message type, we can create an actor:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Akka; using Akka.Actor; namespace ConsoleApplication11 { public class Greet { public Greet(string who) { Who = who; } public string Who { get;private set; } } //    public class GreetingActor : ReceiveActor { public GreetingActor() { //    //  Greet ()  Receive<Greet>(greet => Console.WriteLine("Hello {0}", greet.Who)); } } class Program { static void Main(string[] args) { } } } 

Now it's time to send a message to the actor. To do this, we use the ActorSystem and call ActorOf .

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Akka; using Akka.Actor; namespace ConsoleApplication11 { public class Greet { public Greet(string who) { Who = who; } public string Who { get;private set; } } public class GreetingActor : ReceiveActor { public GreetingActor() { Receive<Greet>(greet => Console.WriteLine("Hello {0}", greet.Who)); } } class Program { static void Main(string[] args) { //     (   ) var system = ActorSystem.Create("MySystem"); //        . //   "ActorRef",    //     , //       . var greeter = system.ActorOf<GreetingActor>("greeter"); //    greeter.Tell(new Greet("World")); //      //   ,    //   . Console.ReadLine(); } } } 

Thus, your actor is ready to receive messages sent from any number of calling threads.

Required Hello World!


This example shows how to define and use actors in C # and F #.

Hello World using the C # API


Message definition:

 //  ()  ,       public class Greet { public Greet(string who) { Who = who; } public string Who { get;private set; } } 

Defining your actor using the ReceiveActor API:

 //    public class GreetingActor : ReceiveActor { public GreetingActor() { Receive<Greet>(greet => Console.WriteLine("Hello {0}", greet.Who)); } } 

... or using the TypedActor API:

 public class GreetingActor : TypedActor , IHandle<Greet> { public void Handle(Greet greet) { Console.WriteLine("Hello {0}!", greet.Who); } } 

Using:

 //     (   ) var system = ActorSystem.Create("MySystem"); //        . //   "IActorRef",    //     , //       . var greeter = system.ActorOf<GreetingActor>("greeter"); //    greeter.Tell(new Greet("World")); //      //   ,    //   . Console.ReadLine(); 

See also (pct is currently missing in the original):


Hello World using the F # API:

 //  ()  ,       type Greet = Greet of string let system = ActorSystem.Create "MySystem" // Use F# computation expression with tail-recursive loop // to create an actor message handler and return a reference let greeter = spawn system "greeter" <| fun mailbox -> let rec loop() = actor { let! msg = mailbox.Receive() match msg with | Greet who -> printf "Hello, %s!\n" who return! loop() } loop() greeter <! Greet "World" 

Use cases and deployment scenarios:

Console application
PM> install-package Akka
PM> install-package Akka.Remote

 using Akka; using Akka.Actor; using Akka.Configuration; namespace Foo.Bar { class Program { static void Main(string[] args) { // configure remoting for localhost:8081 var fluentConfig = FluentConfig.Begin() .StartRemotingOn("localhost", 8081) .Build(); using (var system = ActorSystem.Create("my-actor-server", fluentConfig)) { // start two services var service1= system.ActorOf<Service1>("service1"); var service2 = system.ActorOf<Service2>("service2"); Console.ReadKey(); } } } } 


Basement


This article is a very free translation of Introduction to Akka.NET .

Source: https://habr.com/ru/post/320396/


All Articles