Series Theme: The evolution of message-based software architecture model
Since the theme of this series is "the evolution of the message-based architecture model", the Actor model is not the only one. Akka.net is a distributed framework based on actor model. If you are still unfamiliar with distributed applications, when others are talking about "distributed", "cloud computing" and other nouns you feel at a loss, then this article will take you on a distributed development journey.
First, what is the Actor model
The actor model was proposed by Carl Hewitt in the early 70 and has been widely used in the Erlang language to solve a series of problems in distributed programming. Its main features are as follows:
- The system is composed of actors
- The actor is completely independent
- Message passing is non-blocking and asynchronous
- All messages are sent in parallel
Everything is an actor in the Actor model. Why does the feeling of the flicker start again ...
If you look at the "message-based Architecture Evolution" series that I wrote, you'll find that the software system is constantly abstracted from the observer pattern to the event and then to the message. When the software interacts with the message without a direct reference, without coupling, everything becomes asynchronous and parallel. With distributed support, cloud computing becomes possible.
Second, what is Akka.net
Akka is a Scala-based Actor model library designed to build a set of high-concurrency, distributed, automatic fault-tolerant, message-driven application tools. Akka.net is the Akka version written in C # and has a friendly F#API interface. Git address: https://github.com/akkadotnet/akka.net
Another Actor model Library from Microsoft Research: Orleans. This project is designed to provide a distributed, highly scalable cloud computing framework. git address: Https://github.com/dotnet/orleans, I will then write an introductory article about Orleans, please follow.
Iii. Declaration of Responsiveness
Speaking of Akka.net had to mention the Responsive manifesto. With the development of the Internet and software industry, the previous software architecture has not adapted to the needs of social development. Software system architecture should have: elastic, loose coupling, scalability, easier to develop and maintain, error can be self-fault-tolerant. So the concept of a responsive system follows:
- Responsive:the system responds in a timely manner if at all possible (system should respond as promptly as possible)
- Resilient:the system stays responsive in the face of failure (system can respond in a timely manner when an error occurs)
- Elastic:the system stays responsive under varying workload (systems can respond in a timely manner under various loads)
- Message driven:reactive Systems rely on asynchronous message-passing to establish a boundary between components that Ensu Res loose coupling, isolation, location transparency, and provides the means to delegate errors as messages. (Reactive systems rely on asynchronous message groups To ensure loose coupling, isolation, location transparency, and the means to provide delegated error messages, a bit less accurate translation
The reason for mentioning this declaration is that akka.net is a framework that can help you build a responsive system.
Iv. starting from HelloWorld
1. Create a new Console application
2. Installing Akka from NuGet
3. Create a new Greetmessage class to notify the actor
public class GreetingMessage { }
3, the emphasis has come, since in Akka.net all is actor, then we want to output a "Hello world" also need to through an actor to complete. New Greetingactor class:
public class Greetingactor:receiveactor {public greetingactor () { receive<greetmessage> ( Greet =>console.writeline ("Hello World");} }
As the code shows: when the actor receives the Greetmessage message, it outputs "Hello world".
4. Send a message in the Main method
static void Main (string[] args) { //Create A new actor system (a container for your actors) var system = Acto Rsystem.create ("Mysystem"); Create your actor and get a reference to it. var greeter = System. Actorof<greetingactor> ("greeter"); Send a message to the actor Greeter. Tell (New GreetingMessage ()); Console.ReadLine (); }
A actorsystem is a container for a group of actors.
var greeter = System. Actorof<greetingactor> ("greeter");
An actor named "Greeter" was created.
Greeter. Tell (New GreetingMessage ());
Send the Greetmessage message to the greeter actor.
Ctrl+f5 ran up to see:
This example is simple, but shows you how to use messages and actors in akka.net.
V. Development of a distributed HelloWorld
This helloworld its NB is that it is not a simple helloworld, he is a distributed HelloWorld ...
Since it is distributed, we need to build two console application to be deployed in the cloud and on-premises separately, and create a new class Liabrary project for common message types.
Both the client and server two projects need to install Akka.remote from NuGet for remote communication.
Let's start with the client side:
1, since it involves the remote communication, it is unavoidable to configure the local address, communication protocol, port and other information. All of these configurations can be configured in Web. config. Akka.net is configured using the format of Hocon (human-optimized config Object Notation), for the sake of simplicity, we write this configuration directly into the code-in fact, the content is the same as writing Web. config.
var config = configurationfactory.parsestring (@ "Akka { actor { Provider =" " Akka.Remote.RemoteActorRefProvider, Akka.remote "" } Remote { helios.tcp { transport-class = "" Akka.Remote.Transport.Helios.HeliosTcpTransport, Akka.remote "" applied-adapters = [] Transport-protocol = TCP port = 0 hostname = localhost }}} ");
You can see that this configuration describes the provider of the remote communication, uses the Helios to do TCP communication, and defines the client's hostname, port number, and so on.
2, define the client side of the Actorsystem, and send messages to the remote
using (var system = actorsystem.create ("myclient", config)) { var greeting = System. Actorselection ("Akka.tcp://[email protected]:8081/user/greeting"); while (true) { var input = Console.ReadLine (); if (input. Equals ("SayHello")) { greeting. Tell (New GreetingMessage ());}}}
The point here is that the actor is created by the System.actorselection method, because in the cloud, we don't refer to the cloud-based assembly, but to the actor address in the Cloud Akka.tcp://[email Protected]:8081/user/greeting to access Greetingactor. MyServer is the Actorsystem name in the cloud, localhost:8081 is the address and IP of the cloud, and because of our local simulations, we are still localhost. User/greeting says we have to choose the actor named greeting.
When the user enters "SayHello" on the client side, a greetingmessage message is sent to the greetingactor.
3. Server-side configuration
var config = configurationfactory.parsestring (@ "Akka { actor { Provider =" " Akka.Remote.RemoteActorRefProvider, Akka.remote "" } Remote { helios.tcp { transport-class = "" Akka.Remote.Transport.Helios.HeliosTcpTransport, Akka.remote "" applied-adapters = [] Transport-protocol = TCP port = 8081 hostname = localhost }} ");
Server-side configuration is similar to client side, but has different port numbers, if deployed in the cloud, you should enter the cloud IP.
4, server side of the Actorsystem
using (var system = actorsystem.create ("MyServer", config)) { system. Actorof<greetingactor> ("greeting"); Console.ReadLine (); }
This code is very simple and is somewhat similar to the code in the first example.
5. Operation
Set both client and server as Startup items in the solution
Then Ctrl+f5, after entering "SayHello" in the client, the sever end will output Hello world.
VI. Actor structure
The actor in Akka.net is a hierarchical model in which each actorsystem has multiple actors, and each parent actor can create its own child actor, which corresponds to a business process in which different actors can emulate an object-oriented structure to handle specific work. And this process can be distributed on different servers, thus constituting a complete set of distributed computing systems.
Vii. Summary
The examples mentioned in this article are very simple, but show the actor and message-based communication approach. The strength of akka.net is that it not only solves the production and consumption of messages like an ESB, but also provides a strategy and mechanism for actor to consume messages in parallel, emphasizing how to coordinate work among a large number of actors to build distributed and responsive systems.
With Akka.net you can also easily implement CQRS, and if DDD is playing object-oriented analysis, then akka.net will solve the remaining technical details for you. There are already people using Scala to make such an attempt on Akka, and it is expected that there will be better applications under the. NET platform to further our learning and understanding.
Development of the first distributed application using Akka.net