ClassRoom是一個練手demo,目的是爲了能熟悉掌握Orleans的基本知識和使用方法,我會盡可能在這個項目中加入更多的知識點,一邊學一邊練避免我看完文檔就忘掉html
依舊是四個項目起步app
項目名稱 | 項目類型 | 項目說明 |
---|---|---|
IGrains | .net Core 3.1 類庫 | Grain接口庫 |
Grains | .net Core 3.1 類庫 | 實現Grain的類庫 |
Silo_ConsoleApp | .net Core 3.1 控制檯 | Silo服務 |
Client_ConsoleApp | .net Core 3.1 控制檯 | 客戶端程序 |
IGrains:
Microsoft.Orleans.Core.Abstractions(3.0.2)
Microsoft.Orleans.CodeGenerator.MSBuild(3.0.2)
async
Grains:
Microsoft.Orleans.Core.Abstractions(3.0.2)
Microsoft.Orleans.CodeGenerator.MSBuild(3.0.2)
分佈式
Silo_ConsoleApp:
Microsoft.Orleans.Server(3.0.2)
oop
Client_ConsoleApp:
Microsoft.Orleans.Client(3.0.2)
學習
using System.Threading.Tasks; namespace IGrains { /// <summary> /// 學生 /// </summary> public interface IStudent : Orleans.IGrainWithIntegerKey { /// <summary> /// 打招呼 /// </summary> /// <returns></returns> Task<string> SayHello(); } }
注意Orleans.IGrainWithIntegerKey
,這個決定了Grain的key的類型,是long
仍是string
,或者Guid
.ui
再加個控制檯日誌幫助類this
using System; namespace IGrains { /// <summary> /// 控制檯幫助類 /// </summary> public static class ConsoleHelper { static void WriteColorLine(string str, ConsoleColor color) { ConsoleColor currentForeColor = Console.ForegroundColor; Console.ForegroundColor = color; Console.WriteLine(str); Console.ForegroundColor = currentForeColor; } /// <summary> /// 打印錯誤信息 /// </summary> /// <param name="str">待打印的字符串</param> /// <param name="color">想要打印的顏色</param> public static void WriteErrorLine(this string str, ConsoleColor color = ConsoleColor.Red) { WriteColorLine(str, color); } /// <summary> /// 打印警告信息 /// </summary> /// <param name="str">待打印的字符串</param> /// <param name="color">想要打印的顏色</param> public static void WriteWarningLine(this string str, ConsoleColor color = ConsoleColor.Yellow) { WriteColorLine(str, color); } /// <summary> /// 打印正常信息 /// </summary> /// <param name="str">待打印的字符串</param> /// <param name="color">想要打印的顏色</param> public static void WriteInfoLine(this string str, ConsoleColor color = ConsoleColor.White) { WriteColorLine(str, color); } /// <summary> /// 打印成功的信息 /// </summary> /// <param name="str">待打印的字符串</param> /// <param name="color">想要打印的顏色</param> public static void WriteSuccessLine(this string str, ConsoleColor color = ConsoleColor.Green) { WriteColorLine(str, color); } } }
using IGrains; using System; using System.Threading.Tasks; namespace Grains { /// <summary> /// 學生 /// </summary> public class Student : Orleans.Grain, IStudent { /// <summary> /// 打招呼 /// </summary> /// <returns></returns> public Task<string> SayHello() { var id = this.GrainReference.GrainIdentity.PrimaryKeyLong;//當前Grain的key Console.WriteLine($"\n {id}收到SayHello消息 \n"); return Task.FromResult($"\n 你們好,我是{id} \n"); } } }
這裏我使用了GrainReference.GrainIdentity.PrimaryKeyLong
,這個值就是當前Grain的idkey值,有了它Grain才能知道本身是誰. =_=!spa
using Grains; using Orleans; using Orleans.Configuration; using Orleans.Hosting; using System; using System.Net; using System.Threading.Tasks; namespace Silo_ConsoleApp { class Program { static async Task Main(string[] args) { Console.WriteLine("開始啓動Silo!"); try { var host = await StartSilo(); Console.WriteLine("Silo啓動完成"); Console.WriteLine("\n\n 按回車鍵中止 \n\n"); Console.ReadLine(); await host.StopAsync();//中止當前Silo return; } catch (Exception ex) { Console.WriteLine(ex); return; } } /// <summary> /// 啓動本地配置 /// </summary> /// <returns></returns> private static async Task<ISiloHost> StartSilo() { var host = new SiloHostBuilder() .UseLocalhostClustering() //配置Silo只使用開發集羣,並監聽本地主機。 .Configure<ClusterOptions>(options => { options.ClusterId = "dev"; options.ServiceId = "MyHost"; //獲取或設置此服務的惟一標識符,該標識符應在部署和從新部署後仍然有效 }) .Configure<EndpointOptions>(options => options.AdvertisedIPAddress = IPAddress.Loopback)//配置Silo的端口 .ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(Student).Assembly).WithReferences()) .Build(); await host.StartAsync();//啓動當前Silo. return host; } } }
先用本地開發配置.net
using IGrains; using Orleans; using Orleans.Configuration; using System; using System.Threading.Tasks; namespace Client_ConsoleApp { class Program { static async Task Main(string[] args) { Console.WriteLine("開始啓動Silo!"); try { using (var client = await ConnectClient()) { Console.WriteLine("客戶端已成功鏈接到Silo Host \n"); await DoClientWork(client); } } catch (Exception ex) { Console.WriteLine(ex); } Console.WriteLine("\n\n 按任意鍵退出 \n\n"); Console.ReadKey(); return; } private static async Task DoClientWork(IClusterClient client) { //從客戶端調用Grain的示例 var student = client.GetGrain<IStudent>(321); var response = await student.SayHello(); Console.WriteLine("\n\n{0}\n\n", response); } /// <summary> /// 使用本地配置鏈接服務 /// </summary> /// <returns></returns> private static async Task<IClusterClient> ConnectClient() { IClusterClient client; client = new ClientBuilder() .UseLocalhostClustering() //配置客戶端以鏈接到本地主機上的筒倉。 .Configure<ClusterOptions>(options => { options.ClusterId = "dev"; options.ServiceId = "MyHost"; }) .Build(); await client.Connect(); return client; } } }
一樣適用本地配置
.net core 的程序有個比較煩的地方,每次編譯都會編譯出來一大堆文件,看着心煩。
還好微軟給了咱一個單文件發佈的方法,能夠把這些文件打到一個文件裏,下面我來舉個栗子
右鍵Silo_ConsoleApp
項目,選擇發佈,而後選擇發佈到本地文件夾,修改成以下配置:
而後點一下發佈,我們在選擇的文件夾裏就能看到它了:
Client_ConsoleApp
能夠執行一樣的操做
我們把Silo和Client程序都跑起來,注意,要先啓動Silo程序,等待Silo啓動成功後再啓動Client。
運行效果以下:
目錄 : Orleans[NET Core 3.1] 學習筆記(一).NET環境下的分佈式應用程序
上一節 :Orleans[NET Core 3.1] 學習筆記(三)( 3 )服務端配置
下一節 :