我读过here,应该可以在同一服务中托管紧密耦合的ActorType,但是我似乎找不到任何有关如何执行的文档。
我认为可能需要创建自己的ActorService实例并将上下文传递给它,但是我看不出能够从文档中找到正确的API。
有谁能分享一个榜样?
最佳答案
有点,但不是真的。您可以在同一应用程序中具有多种参与者类型。看起来它们在Visual Studio中处于同一服务中,但实际上它们是作为单独的服务部署的。如果可以的话,请和我呆一分钟。
因此,您可以像这样注册多个 Actor :
internal static class Program
{
private static void Main()
{
ActorRuntime.RegisterActorAsync<Actor1>().GetAwaiter().GetResult();
ActorRuntime.RegisterActorAsync<Actor2>().GetAwaiter().GetResult();
Thread.Sleep(Timeout.Infinite);
}
}
太好了,我有多种 Actor 类型。这有效,您可以做到这一点。
但是您想知道它是如何工作的!好吧,这只是它的简化版本:
internal static class Program
{
private static void Main()
{
ActorRuntime.RegisterActorAsync<Actor1>(
(context, actorType) => new ActorService(context, actorType, () => new Actor1())).GetAwaiter().GetResult();
ActorRuntime.RegisterActorAsync<Actor2>(
(context, actorType) => new ActorService(context, actorType, () => new Actor2())).GetAwaiter().GetResult();
Thread.Sleep(Timeout.Infinite);
}
}
这更能说明实际情况,因为您在这里看到了两个服务。发生什么了?
secret 在ActorRuntime中。它比ServiceRuntime(您通常在其中注册Reliable Services)做更多的工作。 Actor框架构建过程代表您进行了一些魔术操作,以在应用程序中为每种actor类型配置服务类型和默认服务实例。您可以在ApplicationManifest.xml中看到此内容,其中构建工具会为您设置默认服务:
<DefaultServices>
<Service Name="Actor1ActorService" GeneratedIdRef="3262c188-3eee-44c5-9d1e-d2c2a2685f89|Persisted">
<StatefulService ServiceTypeName="Actor1ActorServiceType" TargetReplicaSetSize="[Actor1ActorService_TargetReplicaSetSize]" MinReplicaSetSize="[Actor1ActorService_MinReplicaSetSize]">
<UniformInt64Partition PartitionCount="[Actor1ActorService_PartitionCount]" LowKey="-9223372036854775808" HighKey="9223372036854775807" />
</StatefulService>
</Service>
<Service Name="Actor2ActorService" GeneratedIdRef="1bc66d2c-0479-4bb2-a9aa-3254030506f1|Persisted">
<StatefulService ServiceTypeName="Actor2ActorServiceType" TargetReplicaSetSize="[Actor2ActorService_TargetReplicaSetSize]" MinReplicaSetSize="[Actor2ActorService_MinReplicaSetSize]">
<UniformInt64Partition PartitionCount="[Actor2ActorService_PartitionCount]" LowKey="-9223372036854775808" HighKey="9223372036854775807" />
</StatefulService>
</Service>
例如,如果我使用上面定义的两个actor类型并部署该应用程序,则结果如下:
这些实际上是应用程序中单独的服务实例,并且每个都是不同的服务类型,所有这些都会自动为您生成:
当然,由于它们是不同的服务实例,因此它们将像您通常期望的那样分布在整个集群中:
我将更新该文档。
关于actor - 如何在单个服务中托管多个Service Fabric Actor类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37146843/