背景
我有一个 Akka.NET 集群,其中包含一个 Lighthouse 种子节点和另外两个运行 actor 系统的节点。当我尝试在我的一个集群节点上正常关闭时,我想看到至少一个其他节点收到一条关于节点离开的消息,并且所有集群节点最终都排除了节点列表中的离开节点。
一旦解决了这个问题,我希望我应该能够关闭该节点,而不会导致其他两个节点因无法连接到关闭的节点而发疯。
我尝试过的
我现在拥有的是一个包含在 TopShelf 应用程序中的控制台应用程序:
class ActorService : ServiceControl
{
private ActorSystem _actorSystem;
public bool Start(HostControl hostControl)
{
_actorSystem = ActorSystem.Create("myActorSystem");
var cluster = Cluster.Get(_actorSystem);
cluster.RegisterOnMemberRemoved(_Terminate);
return true;
}
public bool Stop(HostControl hostControl)
{
var cluster = Cluster.Get(_actorSystem);
cluster.Leave(cluster.SelfAddress);
return true;
}
private void _Terminate()
{
_actorSystem.Terminate();
}
}
这是我的主要内容:
class Program
{
static int Main(string[] args)
{
return (int) HostFactory.Run(x =>
{
x.UseAssemblyInfoForServiceInfo();
x.RunAsLocalSystem();
x.StartAutomatically();
x.Service<ActorService>();
x.EnableServiceRecovery(r => r.RestartService(1));
});
}
}
当单步执行 Stop 函数时,我看不到任何关于该节点离开其他节点的消息。然而,当函数返回时,其他节点开始抛出异常。
Akka.NET Gitter channel 的一位用户说:
I have observed the same thing even without TopShelf I must say, with a pure ASP.NET Core project after the webhost terminated.
问题
我可以添加什么来让其他节点收到关于该节点离开的消息?
最佳答案
我认为问题在于 Stop()
方法在离开完成之前完成。您应该等待 MemberRemoved 事件。
此 Stop()
方法将等待,直到 MemberRemoved 回调被调用并发出它甚至已终止 actor 系统的信号。
class Worker
{
private static readonly ManualResetEvent asTerminatedEvent = new ManualResetEvent(false);
private ActorSystem actorSystem;
public void Start()
{
this.actorSystem = ActorSystem.Create("sample");
}
public void Stop()
{
var cluster = Akka.Cluster.Cluster.Get(actorSystem);
cluster.RegisterOnMemberRemoved(() => MemberRemoved(actorSystem));
cluster.Leave(cluster.SelfAddress);
asTerminatedEvent.WaitOne();
//log.Info("Actor system terminated, exiting");
}
private async void MemberRemoved(ActorSystem actorSystem)
{
await actorSystem.Terminate();
asTerminatedEvent.Set();
}
}
注意:我检查了三种类型的应用程序如何毫无问题地离开集群。我在 GitHub 上主持了那个.离开时仍然有一些异常和一些死信,但其他节点不再继续尝试重新连接到退出的节点。
关于c# - Akka.NET集群节点优雅关机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38309461/