Actor model (distributed programming)

Actor purpose is to solve a series of problems in distributed programming. All messages are delivered asynchronously, thus separating the message sender and recipient, because of this separation, lead actor system having inherent concurrency: can be executed in parallel without limitation any actor has an input message. Actor with a written program can not control is how to achieve, you can only transfer data, simple operation. Actor course at the NB, in that remote connection, the same as RPC. The following example is used in Nuget Akka [], [remote] Akka.Remote

#Actor features

> 1. The system consists of the Actor
> 2. completely independent of the Actor
> 3. Messaging is asynchronous and non-blocking
> 4. All messages are parallel

#Actor use

> Can be used directly or through a specific programming language tool Actor Actor model, currently there are two types in C # language more popular, Akka.NET frame and frame Orleans.
> 1. Akka is a language-based scala Actor model library, designed to build a highly concurrent, distributed, fault-tolerant automatic, message-driven toolset applications.
> 2. Orleans framework can build large-scale, high-concurrency, distributed applications, without having to learn professional knowledge of distributed and concurrent framework. It is the study and designed for cloud computing by Microsoft.

Actor simple example

public class ActorDemo
{
    public static void Test()
    {
        var system = ActorSystem.Create("test");
        var greeter = system.ActorOf<JasonActor>("jason");

        for (int i = 0; i < 100; i++) {
            Task.Run(() =>  //异步发送数据
            {
                var id = Math.Abs(Guid.NewGuid().GetHashCode());
                greeter.Tell(newJasonMessage () {Id = ID, the Name $ = " {DateTime.Now.Ticks ID} {} {I}   " }); 
            }); 
        } 
    } 
} 

///  <Summary> 
/// the Actor pharmaceutically message processing
 / //  </ Summary> 
public  class JasonActor: ReceiveActor {
     public JasonActor () { 
        the Receive <JasonMessage> (the greet => { 
            Console.WriteLine ($ " current time: {DateTime.Now.Ticks}, Name: { greet.Name} greet.Id} {:, Id " ); 
        ;}) 
    } 
} 

///  <Summary> 
/// entity class for transmitting messages
/// </summary>
public class JasonMessage {
    public long Id { get; set; }
    public string Name { get; set; }
}

Remote Actor

///  <Summary> 
/// entity class (public library) for transmitting messages
 ///  </ Summary> 
public  class JasonMessage 
{ 
    public  Long Id { GET ; SET ;}
     public  String the Name { GET ; SET ;} 
}
//Actor客户端
namespace AkkaClient
{
    class Program
    {
        static void Main(string[] args)
        {
            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
        }
    }
}
");

            using (var system = ActorSystem.Create("MyClient", config))
            {
                var greeting = system.ActorSelection("akka.tcp://MyServer@localhost:51179/user/Greeting");

                while (true)
                {
                    var input = Console.ReadLine();
                    if (input != "exit")
                    {
                        var id = Math.Abs(Guid.NewGuid().GetHashCode());
                        greeting.Tell(new JasonMessage() { Id = id, Name = $"{DateTime.Now} {input} " });
                    }
                    else
                    {
                        break;
                    }

                }
            }

            Console.WriteLine("Hello World!");
        }
    }
}
//Actor服务端
namespace ServerAkka
{
    class Program
    {
        static void Main(string[] args)
        {
            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 = 51179
            hostname = localhost
        }
    }
}
");

            using (var system = ActorSystem.Create("MyServer", config))
            {
                system.ActorOf<JasonActor>("Greeting");

                Console.ReadLine();
            }

            Console.WriteLine("Hello World!");
        }
    }

    public class JasonActor : UntypedActor
    {
        protected override void OnReceive(object greet)
        {
            var greet1 = (JasonMessage)greet;
            Console.WriteLine($"当前时间:{DateTime.Now.Ticks}, Name:{greet1.Name}, Id:{greet1.Id} ");
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/zhao123/p/11039836.html