https://github.com/wswind/learn-orleans/tree/master/03.MultiGrain
grain虽然是单线程的,但是我们可以创建多个grain来并行执行任务。
修改上一讲的例子:
private static async Task DoClientWork(IClusterClient client)
{
var client1 = client.GetGrain<IHello>(0);
var client2 = client.GetGrain<IHello>(1);
//https://dotnet.github.io/orleans/Documentation/grains/grain_identity.html
var id1 = client1.GetGrainIdentity().GetPrimaryKeyLong(out string keyExt);
var id2 = client2.GetGrainIdentity().GetPrimaryKeyLong(out string keyExt2);
Console.WriteLine(id1);
Console.WriteLine(keyExt);
Console.WriteLine(id2);
Console.WriteLine(keyExt2);
await client1.AddCount();
var count1 = await client1.GetCount();
Console.WriteLine("count1:{0}", count1);
await client2.AddCount();
await client2.AddCount();
var count2 = await client2.GetCount();
Console.WriteLine("count2:{0}", count2);
}
上述代码,通过client,创建多个actor,分别执行AddCount。
可以看到各grain的状态是独立的。