經過C#學Proto.Actor模型》之Remote

Proto.Actor中提供了基於tcp/ip的通迅來實現Remote,能夠經過其Remot實現對Actor的調用。tcp

先來看一個極簡單片的遠程調用。spa

碼友看碼:code

引用NuGet包blog

Proto.Actorip

Proto.Remoteget

Proto.Serialization.Wirestring

 

共享庫:it

 1 namespace P009_Lib
 2 {
 3     public class HelloRequest
 4     {
 5         public string Message
 6         {
 7             get; set;
 8         }
 9     } 
10     public class HelloResponse
11     {
12         public string Message
13         { get; set; }
14     }
15 }

服務端:io

 1 using P009_Lib;
 2 using Proto;
 3 using Proto.Remote;
 4 using Proto.Serialization.Wire;
 5 using System;
 6  
 7 using System.Threading;
 8 using System.Threading.Tasks;
 9  
10 namespace P009_Server
11 {
12     class Program
13     {
14         static void Main(string[] args)
15         {
16             Console.Title = "服務端";
17             Console.WriteLine("回車開始");
18             Console.ReadLine();
19             //設置序列化類型並註冊
20             var wire = new WireSerializer(new[] { typeof(HelloRequest), typeof(HelloResponse) });
21             Serialization.RegisterSerializer(wire, true);
22  
23             var props = Actor.FromProducer(() => new HelloQuestActor());
24             //註冊一個爲hello類別的          
25             Remote.RegisterKnownKind("hello", props);
26             //服務端監控端口5001
27             Remote.Start("127.0.0.1", 5001);
28             Console.WriteLine("服務端開始……");
29             Console.ReadLine();
30         }
31     }
32  
33     class HelloQuestActor : IActor
34     {
35         public Task ReceiveAsync(IContext context)
36         {
37             switch (context.Message)
38             {
39                 case HelloRequest msg:
40                     Console.WriteLine(msg.Message);
41                     context.Respond(new HelloResponse
42                     {
43                         Message = $"迴應:我是服務端【{DateTime.Now}】",
44                     });
45                     break;
46             }
47             return Actor.Done;
48         }
49     } 
50 }

客戶端:class

 1 using P009_Lib;
 2 using Proto;
 3 using Proto.Remote;
 4 using Proto.Serialization.Wire;
 5 using System;
 6 using System.Threading.Tasks;
 7  
 8 namespace P009_Client
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14  
15             Console.Title = "客戶端";
16             Console.WriteLine("回車開始");
17             Console.ReadLine();
18             //設置序列化類型並註冊
19             var wire = new WireSerializer(new[] { typeof(HelloRequest), typeof(HelloResponse) });
20             Serialization.RegisterSerializer(wire, true);
21             //設置本身監控端口5002
22             Remote.Start("127.0.0.1", 5002);
23             //鏈接服務端5001
24             var pid = Remote.SpawnNamedAsync("127.0.0.1:5001", "clientActor", "hello", TimeSpan.FromSeconds(50)).Result.Pid;
25             while (true)
26             {
27                 var res = pid.RequestAsync<HelloResponse>(new HelloRequest { Message = $"請求:我是客戶端 【{DateTime.Now}】" }).Result;
28                 Console.WriteLine(res.Message);
29                 Console.ReadLine();
30             }
31         }
32     }
33 }

代碼很簡單,看註釋就夠了。

 

……

相關文章
相關標籤/搜索