1. 程式人生 > >Orleans學習總結(二)--創建工程

Orleans學習總結(二)--創建工程

單機版 input cluster res isnull stp pre log hello

通過第一篇Orleans學習總結(一)--入門認識我們大致知道知道是幹嘛的了,下面我們來動手造一個傳說中的神秘的高並發集群Orleans程序。

一、創建四個C#工程

1、IGrain工程,用來定義各種業務邏輯對象的接口的工程

1)創建一個Class Library工程

技術分享圖片

2)安裝Olreans依賴
PM> Install-Package Microsoft.Orleans.OrleansCodeGenerator.Build
或者右鍵Refrerence->Manage NuGet Packages
技術分享圖片



3)在Class1.cs裏寫入代碼
using System;
using
System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Orleans; public interface IHello : Orleans.IGrainWithIntegerKey { Task<string> SayHello(string greeting); }

 

2、Grain工程,實現IGrain中定義的各種業務邏輯

1)創建一個Class Library工程

技術分享圖片

2)安裝Olreans依賴

PM> Install-Package Microsoft.Orleans.OrleansCodeGenerator.Build
或者右鍵Refrerence->Manage NuGet Packages



3)在Class1.cs裏寫入代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class HelloGrain : Orleans.Grain, IHello
{
    public Task<string> SayHello(string greeting)
    {
        Console.WriteLine(greeting);
        return
Task.FromResult($"You said: ‘{greeting}‘, I say: Hello!"); } }

4)添加工程依賴

技術分享圖片

3、Host工程,他加載所有跟他同級目錄的有類繼承自Orleans.Grain的dll

1)創建一個Console工程,你也可以是其他的GUI工程

技術分享圖片

2、添加依賴

PM> Install-Package Microsoft.Orleans.Server

3、寫入代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Orleans.Runtime.Host;

namespace Host
{
    class Program
    {
        static void Main(string[] args)
        {
            var config = Orleans.Runtime.Configuration.ClusterConfiguration.LocalhostPrimarySilo();
            using (var host = new SiloHost("Default", config))
            {
                try
                {
                    host.InitializeOrleansSilo();
                    host.StartOrleansSilo();

                    Console.WriteLine("Orleans Silo is running.");
                    Console.WriteLine("Press Enter to terminate...");
                    Console.ReadLine();

                    host.StopOrleansSilo();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    Console.ReadLine();
                }

            }
        }
    }
}

4、Client工程,也就是我們的前端工程,註意他的定位並不是說比如我們CS架構裏的C,他是請求業務邏輯的入口,可能是個Web服務器。

1)添加工程

技術分享圖片

2、添加依賴

PM> Install-Package Microsoft.Orleans.Client

3、寫入代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Orleans;
using Orleans.Runtime;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            Task.Run(() => Start(args)).GetAwaiter().GetResult();




        }

        public static async Task Start(string[] args)
        {
            while (true)
            {
                try
                {
                    var config = Orleans.Runtime.Configuration.ClientConfiguration.LocalhostSilo();

                    GrainClient.Initialize(config);
                    break;
                }
                catch (SiloUnavailableException ex)
                {
                    await Task.Delay(1000);
                }
            }
            Console.WriteLine("Orleans Client is running.");
            
            while (true)
            {
                Console.WriteLine("Input to say");

                var input = Console.ReadLine();
                if (!string.IsNullOrEmpty(input))
                {
                    var grain = GrainClient.GrainFactory.GetGrain<IHello>(0);
                    var ret = await grain.SayHello(input);
                    Console.WriteLine(ret);
                }
            }

            GrainClient.Uninitialize();
        }
    }
}

4、工程依賴

技術分享圖片

二、啟動工程

1)4個工程都已經創建好了,設置下啟動項

技術分享圖片

2)將4個工程輸出目錄都設置到一個目錄,目的是為了讓Host能加載到dll

技術分享圖片

三、測試工程

1)F5啟動

技術分享圖片

當你得到這樣的輸出就說明一切正常

1)在Client控制臺程序下敲入字符,看效果

技術分享圖片

總結:至此我們的Orleans工程都跑起來了,單機版Orleans就分4部分:接口,實現,Host,Client。

你說這根本不是我想要的集群啊高並發啊,別急,這次我們只是簡單的搭建起框架,甚至連配置都直接寫在代碼裏了,後面還有相關的介紹。

Orleans學習總結(二)--創建工程