本文介绍了Service Fabric在启动时生成actor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法要求系统在启动时产生某些参与者.

Is there a way to require the system to spawn certain actors on startup.

当前,在演员注册后,我在Program.cs中激活所需的一组演员.

Currently I activate the set of actors that I need in Program.cs after the actor registration.

这可以正常工作,但是我偶尔会得到一个ReminderLoadInProgressException,因为被激活的actor需要注册一个提醒,但是在尝试执行此操作时并未加载所有提醒.

This is working ok, but I occasionally get a ReminderLoadInProgressException because the actor that is being activated needs to register a reminder but not all reminders have been loaded at the time it tries to do that.

是否有标准的方法来植入群集?

Is there standard way to seed the cluster?

推荐答案

不,您需要激活演员的一些东西.

No, you need something to activate your actors.

Program.cs并不是最佳选择,因为您可能已经注意到,它的执行与服务的生命周期并不完全一致.

Program.cs isn't the best place to do it because its execution doesn't really line up with your service's lifecycle, as you've probably noticed.

如果您需要在actor服务启动时激活一些actor,那么执行此操作的一个好地方就是在actor服务的RunAsync方法中.您可以通过编写从其派生的服务来自定义actor服务,就像编写有状态服务时一样:

if you need to activate some actors when your actor service starts up, a good place to do that would be in the RunAsync method of your actor service. You can customize your actor service by writing a service that derives from it, same as you do when you write a stateful service:

确保首先调用并等待base.RunAsync()!

make sure you call and await base.RunAsync() first!

class MyActorService : ActorService
{
    public MyActorService(StatefulServiceContext context, ActorTypeInformation typeInfo, Func<ActorBase> newActor)
        : base(context, typeInfo, newActor)
    { }

    protected async override Task RunAsync(CancellationToken cancellationToken)
    {
        await base.RunAsync(cancellationToken);

        var proxy = ActorProxy.Create<IMyActor>(new ActorId(0));
        await proxy.StartDoingStuff();
    }
}

然后注册您的actor服务,以便运行时知道使用它来托管您的actor实例:

And then register your actor service so the runtime knows to use it to host your actor instances:

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

        Thread.Sleep(Timeout.Infinite);
    }
}

只需确保actor方法是幂等的,因为它将在每次启动actor服务的主副本时执行(在故障转移,资源平衡,应用程序升级等之后),但是很高兴的是它不会在您的actor服务准备就绪之前执行,并且它将在服务启动时始终执行.

Just make sure that actor method is idempotent, because this will execute every time the primary replica of your actor service is started up (after failover, resource balancing, application upgrade, etc), but the nice thing is it won't execute before your actor service is ready and it will always execute when the service comes up.

有关如何使用角色服务的更多信息,请参见此处: https://azure.microsoft.com/en-us/documentation/articles/service-fabric-reliable-actors-platform/

See here for more info on how to work with the actor service: https://azure.microsoft.com/en-us/documentation/articles/service-fabric-reliable-actors-platform/

这篇关于Service Fabric在启动时生成actor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 13:56