ClassRoom
ClassRoom是一个练手demo,目的是为了能熟悉掌握Orleans的基本知识和使用方法,我会尽量在这个项目中加入更多的知识点,一边学一边练避免我看完文档就忘掉
创建项目
依旧是四个项目起步
项目名称 | 项目类型 | 项目说明 |
---|---|---|
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)
Grains:
Microsoft.Orleans.Core.Abstractions(3.0.2)
Microsoft.Orleans.CodeGenerator.MSBuild(3.0.2)
Silo_ConsoleApp:
Microsoft.Orleans.Server(3.0.2)
Client_ConsoleApp:
Microsoft.Orleans.Client(3.0.2)
IGrains 定义
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
.
再加个控制台日志帮助类
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);
}
}
}
Grains 定义
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($"
{id}收到SayHello消息
");
return Task.FromResult($"
大家好,我是{id}
");
}
}
}
这里我使用了GrainReference.GrainIdentity.PrimaryKeyLong
,这个值就是当前Grain的idkey值,有了它Grain才能知道自己是谁. =_=!
Silo_ConsoleApp
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("
按回车键停止
");
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;
}
}
}
先用本地开发配置
Client_ConsoleApp
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
");
await DoClientWork(client);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.WriteLine("
按任意键退出
");
Console.ReadKey();
return;
}
private static async Task DoClientWork(IClusterClient client)
{
//从客户端调用Grain的示例
var student = client.GetGrain<IStudent>(321);
var response = await student.SayHello();
Console.WriteLine("
{0}
", 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环境下的分布式应用程序