我正在尝试 Akka.Net Cluster Tools,以使用 Singleton 行为,它似乎工作得很好,但就在当前的单例节点“主机”以优雅的方式离开集群时。如果我突然关闭主机节点,则不会发生切换。
背景
我正在构建一个由四个节点(最初)组成的系统。其中一个节点将是“工人协调员”,它将负责监控数据库中的一些数据,并在必要时将作业提交给其他工人。我正在考虑订阅集群事件并使用角色领导者更改事件来使参与者(在领导者节点上)成为协调者,但我认为在这种情况下集群单例会是一个更好的选择。
工作样本(但如果我优雅地离开集群)
private void Start() {
Console.Title = "Worker";
var section = (AkkaConfigurationSection)ConfigurationManager.GetSection("akka");
var config = section.AkkaConfig;
// Create a new actor system (a container for your actors)
var system = ActorSystem.Create("SingletonActorSystem", config);
var cluster = Cluster.Get(system);
cluster.RegisterOnMemberRemoved(() => MemberRemoved(system));
var settings = new ClusterSingletonManagerSettings("processorCoordinatorInstance",
"worker", TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(1));
var actor = system.ActorOf(ClusterSingletonManager.Props(
singletonProps: Props.Create<ProcessorCoordinatorActor>(),
terminationMessage: PoisonPill.Instance,
settings: settings),
name: "processorCoordinator");
string line = Console.ReadLine();
if (line == "g") { //handover works
cluster.Leave(cluster.SelfAddress);
_leaveClusterEvent.WaitOne();
system.Shutdown();
} else { //doesn't work
system.Shutdown();
}
}
private async void MemberRemoved(ActorSystem actorSystem) {
await actorSystem.Terminate();
_leaveClusterEvent.Set();
}
配置
akka {
suppress-json-serializer-warning = on
actor {
provider = "Akka.Cluster.ClusterActorRefProvider, Akka.Cluster"
}
remote {
helios.tcp {
port = 0
hostname = localhost
}
}
cluster {
seed-nodes = ["akka.tcp://SingletonActorSystem@127.0.0.1:4053"]
roles = [worker]
}
}