如何在单个服务中托管多个Service Fabric Actor类型?

Joh*_*orn 9 actor azure-service-fabric

我在这里读到应该可以在同一个服务中托管紧密耦合的ActorTypes,但我似乎无法找到关于如何做到这一点的任何文档.

我认为我可能需要创建自己的ActorService实例并将上下文传递给它,但我没有看到能够从文档中找到正确的API.

有没有人有他们可以分享的例子?

Vac*_*cek 19

有点但不是真的.您可以在同一个应用程序中拥有多个actor类型.看起来他们在Visual Studio中使用相同的服务,但它们实际上是作为单独的服务部署的.如果你愿意的话,请耐心等待一下......

所以你可以注册这样的多个演员:

internal static class Program
{
    private static void Main()
    {
        ActorRuntime.RegisterActorAsync<Actor1>().GetAwaiter().GetResult();
        ActorRuntime.RegisterActorAsync<Actor2>().GetAwaiter().GetResult();

        Thread.Sleep(Timeout.Infinite);
    }
}
Run Code Online (Sandbox Code Playgroud)

太好了,我有多种演员类型.这有效,你可以这样做.

但你想知道它是如何工作的!嗯,这只是这个的简化版本:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这更能说明实际发生了什么,因为你看到我现在有两个服务.发生什么了?

秘密在于ActorRuntime.它比ServiceRuntime(通常注册可靠服务)做了一些工作.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>
Run Code Online (Sandbox Code Playgroud)

举个例子,如果我采用上面定义的两个actor类型并部署该应用程序,结果如下:

演员服务

这些实际上是应用程序中的单独服务实例,每个服务实例都是不同的服务类型,所有这些都是为您自动生成的:

在此输入图像描述

当然,因为它们是不同的服务实例,所以它们将像您通常期望的那样在整个集群中分发:

在此输入图像描述

我会去更新那个doc.