在执行组件服务中实现服务级功能Implement service-level features in your actor service

服务分层中所述,执行组件服务本身是一项可靠的服务。As described in service layering, the actor service itself is a reliable service. 可以编写自己的派生自 ActorService 的服务。You can write your own service that derives from ActorService. 还可以实现服务级功能,所用方式与继承有状态服务的方式相同,例如:You also can implement service-level features in the same way as when you inherit a stateful service, such as:

  • 服务备份和还原。Service backup and restore.
  • 共享给所有执行组件的功能,例如断路器。Shared functionality for all actors, for example, a circuit breaker.
  • 对执行组件服务自身和每个执行组件的远程过程调用。Remote procedure calls on the actor service itself and on each individual actor.

使用执行组件服务Use the actor service

执行组件实例可访问运行这些实例的执行组件服务。Actor instances have access to the actor service in which they are running. 通过执行组件服务,执行组件实例可以编程方式获取服务上下文。Through the actor service, actor instances can programmatically obtain the service context. 服务上下文包括分区 ID、服务名称、应用程序名称以及其他特定于 Azure Service Fabric 平台的信息。The service context has the partition ID, service name, application name, and other Azure Service Fabric platform-specific information.

Task MyActorMethod()
{
    Guid partitionId = this.ActorService.Context.PartitionId;
    string serviceTypeName = this.ActorService.Context.ServiceTypeName;
    Uri serviceInstanceName = this.ActorService.Context.ServiceName;
    string applicationInstanceName = this.ActorService.Context.CodePackageActivationContext.ApplicationName;
}
CompletableFuture<?> MyActorMethod()
{
    UUID partitionId = this.getActorService().getServiceContext().getPartitionId();
    String serviceTypeName = this.getActorService().getServiceContext().getServiceTypeName();
    URI serviceInstanceName = this.getActorService().getServiceContext().getServiceName();
    String applicationInstanceName = this.getActorService().getServiceContext().getCodePackageActivationContext().getApplicationName();
}

与所有 Reliable Services 一样,执行组件服务必须使用 Service Fabric 运行时中的服务类型注册。Like all Reliable Services, the actor service must be registered with a service type in the Service Fabric runtime. 为了使执行组件服务能够运行执行组件实例,还必须将执行组件类型注册到执行组件服务。For the actor service to run your actor instances, your actor type also must be registered with the actor service. ActorRuntime 注册方法将为执行组件执行此操作。The ActorRuntime registration method performs this work for actors. 最简单的情况是,只需注册执行组件类型,然后执行组件服务就会使用默认的设置。In the simplest case, you can register your actor type, and the actor service then uses the default settings.

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

        Thread.Sleep(Timeout.Infinite);
    }
}

或者,可以使用此注册方法提供的 lambda 自己构造执行组件服务。Alternatively, you can use a lambda provided by the registration method to construct the actor service yourself. 然后可以配置执行组件服务和显式构造执行组件实例。You then can configure the actor service and explicitly construct your actor instances. 可以通过执行组件的构造函数向执行组件注入依赖项。You can inject dependencies to your actor through its constructor.

static class Program
{
    private static void Main()
    {
        ActorRuntime.RegisterActorAsync<MyActor>(
            (context, actorType) => new ActorService(context, actorType, () => new MyActor()))
            .GetAwaiter().GetResult();

        Thread.Sleep(Timeout.Infinite);
    }
}
static class Program
{
    private static void Main()
    {
      ActorRuntime.registerActorAsync(
              MyActor.class,
              (context, actorTypeInfo) -> new FabricActorService(context, actorTypeInfo),
              timeout);

        Thread.sleep(Long.MAX_VALUE);
    }
}

执行组件服务方法Actor service methods

执行组件服务实现了 IActorService (C#) 或 ActorService (Java),后者又实现了 IService (C#) 或 Service (Java)。The actor service implements IActorService (C#) or ActorService (Java), which in turn implements IService (C#) or Service (Java). 此接口由 Reliable Services 远程控制使用,该远程控制允许对服务方法执行远程过程调用。This interface is used by Reliable Services remoting, which allows remote procedure calls on service methods. 它包含可以通过服务远程控制进行远程调用的服务级别方法。It contains service-level methods that can be called remotely via service remoting. 可以使用它来枚举删除执行组件。You can use it to enumerate and delete actors.

自定义执行组件服务Custom actor service

通过使用执行组件注册 lambda,可以注册从 ActorService (C#) 和 FabricActorService (Java) 派生的自定义执行组件服务。By using the actor registration lambda, you can register your own custom actor service that derives from ActorService (C#) and FabricActorService (Java). 然后,可以通过编写从 ActorService (C#) 或 FabricActorService (Java) 继承的服务类来实现自己的服务级功能。You then can implement your own service-level functionality by writing a service class that inherits ActorService (C#) or FabricActorService (Java). 自定义执行组件服务从 ActorService (C#) 或 FabricActorService (Java) 继承了所有执行组件运行时功能,A custom actor service inherits all the actor runtime functionality from ActorService (C#) or FabricActorService (Java). 可用来实现你自己的服务方法。It can be used to implement your own service methods.

class MyActorService : ActorService
{
    public MyActorService(StatefulServiceContext context, ActorTypeInformation typeInfo, Func<ActorBase> newActor)
        : base(context, typeInfo, newActor)
    { }
}
class MyActorService extends FabricActorService
{
    public MyActorService(StatefulServiceContext context, ActorTypeInformation typeInfo, BiFunction<FabricActorService, ActorId, ActorBase> newActor)
    {
         super(context, typeInfo, newActor);
    }
}
static class Program
{
    private static void Main()
    {
        ActorRuntime.RegisterActorAsync<MyActor>(
            (context, actorType) => new MyActorService(context, actorType, () => new MyActor()))
            .GetAwaiter().GetResult();

        Thread.Sleep(Timeout.Infinite);
    }
}
public class Program
{
    public static void main(String[] args)
    {
        ActorRuntime.registerActorAsync(
                MyActor.class,
                (context, actorTypeInfo) -> new FabricActorService(context, actorTypeInfo),
                timeout);
        Thread.sleep(Long.MAX_VALUE);
    }
}

实现执行组件备份和还原Implement actor backup and restore

自定义执行组件服务可通过利用于 ActorService 中已存在的远程侦听器公开备份执行组件数据的方法。A custom actor service can expose a method to back up actor data by taking advantage of the remoting listener already present in ActorService. 有关示例,请参阅备份和还原执行组件For an example, see Backup and restore actors.

使用远程处理 V2(与接口兼容)堆栈的执行组件Actor that uses a remoting V2 (interface compatible) stack

远程处理 V2(与接口兼容,称为 V2_1)堆栈具有 V2 远程处理堆栈的所有功能。The remoting V2 (interface compatible, known as V2_1) stack has all the features of the V2 remoting stack. 其接口与远程处理 V1 堆栈兼容,但不与 V2 和 V1 后向兼容。Its interface is compatible with the remoting V1 stack, but it is not backward compatible with V2 and V1. 若要在不影响服务可用性的情况下从 V1 升级到 V2_1,请执行下一部分的步骤。To upgrade from V1 to V2_1 with no effects on service availability, follow the steps in the next section.

需要进行以下更改才能使用远程处理 V2_1 堆栈:The following changes are required to use the remoting V2_1 stack:

  1. 在执行组件接口中添加以下程序集属性。Add the following assembly attribute on actor interfaces.

    [assembly:FabricTransportActorRemotingProvider(RemotingListenerVersion = RemotingListenerVersion.V2_1,RemotingClientVersion = RemotingClientVersion.V2_1)]
    
  2. 生成并升级执行组件服务和执行组件客户端项目即可开始使用 V2 堆栈。Build and upgrade actor service and actor client projects to start using the V2 stack.

在不影响服务可用性的情况下将执行组件服务升级到远程处理 V2(与接口兼容)堆栈Actor service upgrade to remoting V2 (interface compatible) stack without affecting service availability

此更改是一个两步升级过程。This change is a two-step upgrade. 请按顺序执行以下步骤。Follow the steps in this sequence.

  1. 在执行组件接口中添加以下程序集属性。Add the following assembly attribute on actor interfaces. 此属性针对执行组件服务启动两个侦听器:V1(现有)和 V2_1 侦听器。This attribute starts two listeners for the actor service, V1 (existing) and the V2_1 listener. 通过此项更改升级执行组件服务。Upgrade the actor service with this change.

    [assembly:FabricTransportActorRemotingProvider(RemotingListenerVersion = RemotingListenerVersion.V1|RemotingListenerVersion.V2_1,RemotingClientVersion = RemotingClientVersion.V2_1)]
    
  2. 在完成上一升级后升级执行组件客户端。Upgrade the actor clients after you complete the previous upgrade. 此步骤确保执行组件代理使用远程处理 V2_1 堆栈。This step makes sure that the actor proxy uses the remoting V2_1 stack.

  3. 此步骤是可选的。This step is optional. 可以通过更改上述属性来删除 V1 侦听器。Change the previous attribute to remove the V1 listener.

    [assembly:FabricTransportActorRemotingProvider(RemotingListenerVersion = RemotingListenerVersion.V2_1,RemotingClientVersion = RemotingClientVersion.V2_1)]
    

使用远程处理 V2 堆栈的执行组件Actor that uses the remoting V2 stack

借助 2.8 版 NuGet 包,用户现在可以使用性能更好且提供自定义序列化等功能的远程处理 V2 堆栈。With the version 2.8 NuGet package, users can now use the remoting V2 stack, which performs better and provides features like custom serialization. 远程处理 V2 不后向兼容现有的远程处理堆栈(现在称为 V1 远程处理堆栈)。Remoting V2 is not backward compatible with the existing remoting stack (now called the V1 remoting stack).

需要进行以下更改才能使用远程处理 V2 堆栈。The following changes are required to use the remoting V2 stack.

  1. 在执行组件接口中添加以下程序集属性。Add the following assembly attribute on actor interfaces.

    [assembly:FabricTransportActorRemotingProvider(RemotingListenerVersion = RemotingListenerVersion.V2,RemotingClientVersion = RemotingClientVersion.V2)]
    
  2. 生成并升级执行组件服务和执行组件客户端项目即可开始使用 V2 堆栈。Build and upgrade the actor service and actor client projects to start using the V2 stack.

在不影响服务可用性的情况下将执行组件服务升级到远程处理 V2 堆栈Upgrade the actor service to the remoting V2 stack without affecting service availability

此更改是一个两步升级过程。This change is a two-step upgrade. 请按顺序执行以下步骤。Follow the steps in this sequence.

  1. 在执行组件接口中添加以下程序集属性。Add the following assembly attribute on actor interfaces. 此属性针对执行组件服务启动两个侦听器:V1(现有)和 V2 侦听器。This attribute starts two listeners for the actor service, V1 (existing) and the V2 listener. 通过此项更改升级执行组件服务。Upgrade the actor service with this change.

    [assembly:FabricTransportActorRemotingProvider(RemotingListenerVersion = RemotingListenerVersion.V1|RemotingListenerVersion.V2,RemotingClientVersion = RemotingClientVersion.V2)]
    
  2. 在完成上一升级后升级执行组件客户端。Upgrade the actor clients after you complete the previous upgrade. 此步骤确保执行组件代理使用远程处理 V2 堆栈。This step makes sure that the actor proxy uses the remoting V2 stack.

  3. 此步骤是可选的。This step is optional. 可以通过更改上述属性来删除 V1 侦听器。Change the previous attribute to remove the V1 listener.

    [assembly:FabricTransportActorRemotingProvider(RemotingListenerVersion = RemotingListenerVersion.V2,RemotingClientVersion = RemotingClientVersion.V2)]
    

后续步骤Next steps