参考文档:https://dotnet.github.io/orleans/Tutorials/index.html
一、通过模板创建Orleans
①下载vs插件:https://marketplace.visualstudio.com/items?itemName=sbykov.MicrosoftOrleansToolsforVisualStudio
②通过模板添加
③引用关系
Grains引用GrainInterfaces
Host引用 Grains、GrainInterfaces
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Orleans; namespace GrainInterfaces { public interface ITestGrain:IGrainWithGuidKey { Task Run(); Task<string> Get(); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Orleans; using GrainInterfaces; namespace Grains { public class TestGrain : Grain, ITestGrain { public Task<string> Get() { return Task.FromResult(this.GetType().FullName + ".Get()"); } public Task Run() { Console.WriteLine(this.GetType().FullName + ".Run()"); return Task.CompletedTask; } } }
using System; using Orleans; using Orleans.Runtime.Configuration; using Orleans.Runtime.Host; using GrainInterfaces; namespace Host { /// <summary> /// Orleans test silo host /// </summary> public class Program { static void Main(string[] args) { // 首先,配置并启动一个本地silo var siloConfig = ClusterConfiguration.LocalhostPrimarySilo(); var silo = new SiloHost("TestSilo", siloConfig); silo.InitializeOrleansSilo(); silo.StartOrleansSilo(); Console.WriteLine("Silo started."); // 然后配置并连接一个客户端。 var clientConfig = ClientConfiguration.LocalhostSilo(); var client = new ClientBuilder().UseConfiguration(clientConfig).Build(); client.Connect().Wait(); Console.WriteLine("Client connected."); // // 这是你测试代码的地方 // var testGrain = client.GetGrain<ITestGrain>(Guid.Empty); while (true) { Console.ReadKey(); testGrain.Run(); Console.WriteLine(testGrain.Get().Result); Console.WriteLine(" Press Enter to terminate..."); Console.ReadLine(); } // 关掉 client.Close(); silo.ShutdownOrleansSilo(); } } }