Ejemplo n.º 1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            var myPodIp = GetLocalIPAddress();

            var config = ConfigurationFactory.ParseString(@"
	            akka {
		            actor.provider = cluster
                    remote.dot-netty.tcp {
                        hostname = """ + myPodIp + @"""
                        port = 2551 
                    }
                    cluster.roles = [sample,demo]
		            cluster.discovery {
			            provider = akka.cluster.discovery.k8s
			            k8s {
		                    refresh-interval = 10s
                            namespace = ""default""
                            label-selector = ""akka-cluster=sample,env=Development""
                        }
		            }
	            }"    );

            using var system = ActorSystem.Create("sample", config);
            await ClusterDiscovery.JoinAsync(system);

            system.ActorOf(Props.Create <SampleActor>());

            try
            {
                while (!stoppingToken.IsCancellationRequested)
                {
                    var sb = new StringBuilder();
                    sb.AppendLine($"My address: {myPodIp}");

                    var state = Cluster.Get(system).State;

                    foreach (var member in state.Members)
                    {
                        sb.AppendLine($"    Member: {member.Address} = {member.Status}, Seen by = {state.SeenBy.Contains(member.Address)} [{string.Join(", ", member.Roles)}]");
                    }
                    logger.LogInformation("Status:\r\n" + sb.ToString());

                    await Task.Delay(10000, stoppingToken);
                }
            }
            finally
            {
                var cluster = Cluster.Get(system);
                await cluster.LeaveAsync();
            }
        }
Ejemplo n.º 2
0
        public override async Task StartAsync(CancellationToken cancellationToken)
        {

            cluster = Akka.Cluster.Cluster.Get(ActorSystem);

            cluster.RegisterOnMemberUp(RegisterOnMemberUp);

            cluster.RegisterOnMemberRemoved(RegisterOnMemberRemoved);
            ClusterDiscovery.Get(ActorSystem);
           

            await ClusterDiscovery.JoinAsync(ActorSystem);
           
        }
Ejemplo n.º 3
0
        private static async Task RunAsync()
        {
            var config = ConfigurationFactory.ParseString(@"
	            akka {
		            actor.provider = cluster
                    remote.dot-netty.tcp {
                        hostname = ""127.0.0.1""
                        port = 0
                    }
		            cluster.discovery {
			            provider = akka.cluster.discovery.consul
			            consul {
				            listener-url = ""http://127.0.0.1:8500""
		                    refresh-interval = 10s
                            # restart consul client every 5 minutes
                            restart-interval = 5m
			            }
		            }
	            }"    );

            using (var system = ActorSystem.Create("sample", config))
            {
                await ClusterDiscovery.JoinAsync(system);

                system.ActorOf(Props.Create <SampleActor>());

                Console.WriteLine("Press enter to shutdown the current node...");
                Console.ReadLine();

                var cluster = Cluster.Get(system);
                await cluster.LeaveAsync();

                Console.WriteLine("Node terminated. Press enter to exit...");
                Console.ReadLine();
            }
        }