How can I host multiple Service Fabric Actor Types inside a single service?

前端 未结 1 1654
死守一世寂寞
死守一世寂寞 2021-01-04 21:18

I\'ve read here that is should be possible to host tightly coupled ActorTypes within the same service but I can\'t seem to find any documentation on exactly how to do it.

1条回答
  •  逝去的感伤
    2021-01-04 21:56

    Sort of but not really. You can have multiple actor types in the same application. It looks like they're in the same service in Visual Studio, but they're actually deployed as separate services. Bear with me for a minute if you will..

    So you can register multiple actors like this:

    internal static class Program
    {
        private static void Main()
        {
            ActorRuntime.RegisterActorAsync().GetAwaiter().GetResult();
            ActorRuntime.RegisterActorAsync().GetAwaiter().GetResult();
    
            Thread.Sleep(Timeout.Infinite);
        }
    }
    

    Great, I have multiple actor types. This works and you can just do that.

    But you want know how it works! Well, that's just a simplified version of this:

    internal static class Program
    {
        private static void Main()
        {
            ActorRuntime.RegisterActorAsync(
                (context, actorType) => new ActorService(context, actorType, () => new Actor1())).GetAwaiter().GetResult();
    
            ActorRuntime.RegisterActorAsync(
                (context, actorType) => new ActorService(context, actorType, () => new Actor2())).GetAwaiter().GetResult();
    
            Thread.Sleep(Timeout.Infinite);
        }
    }
    

    This is more telling of what's actually happening, because you see here I now have two services. So what's going on?

    The secret is in the ActorRuntime. It does a little more work than ServiceRuntime (where you register Reliable Services normally). The Actor framework build process does some magic on your behalf to configure a service type and a default service instance inside your application for each actor type. You can see this in your ApplicationManifest.xml, where the build tools sets up default services for you:

    
      
         
            
         
      
      
         
            
         
      
    

    As an example, if I take the two actor types I have defined above and deploy that application, here is the result:

    These are actually separate service instances in the application, and each is of a different service type, all of which is automatically generated for you:

    And of course, because they're different service instances, they'll be distribute across the cluster as you would normally expect:

    I'll go update that doc.

    0 讨论(0)
提交回复
热议问题