Skip to content

Instantly share code, notes, and snippets.

@Arkatufus
Created July 16, 2021 14:36
Show Gist options
  • Save Arkatufus/dc15524aff46918ff603518c1190dbc9 to your computer and use it in GitHub Desktop.
Save Arkatufus/dc15524aff46918ff603518c1190dbc9 to your computer and use it in GitHub Desktop.
Attempt to create a network environment with
using Akka.Actor;
using Akka.Cluster;
using Akka.Event;
namespace Simple.Cluster
{
public class ClusterListener : ReceiveActor
{
public static Props Props() => Akka.Actor.Props.Create(() => new ClusterListener());
private readonly ILoggingAdapter _log;
public ClusterListener()
{
_log = Context.GetLogger();
var cluster = Akka.Cluster.Cluster.Get(Context.System);
cluster.Subscribe(
Self,
ClusterEvent.SubscriptionInitialStateMode.InitialStateAsEvents,
typeof(ClusterEvent.MemberStatusChange),
typeof(ClusterEvent.ReachabilityEvent));
Receive<ClusterEvent.ReachabilityEvent>(message =>
{
switch (message)
{
case ClusterEvent.UnreachableMember msg:
_log.Info($"Member detected as unreachable: {msg.Member}");
break;
case ClusterEvent.ReachableMember msg:
_log.Info($"Member is now reachable: {msg.Member}");
break;
default:
Unhandled(message);
break;
}
});
Receive<ClusterEvent.MemberStatusChange>(message =>
{
switch (message)
{
case ClusterEvent.MemberUp msg:
_log.Info($"Member is now Up: {msg.Member.Address}");
break;
case ClusterEvent.MemberRemoved msg:
_log.Info($"Member is removed: {msg.Member.Address} after {msg.PreviousStatus}");
break;
default:
Unhandled(message);
break;
}
});
}
}
}
version: '3'
services:
lighthouse.main:
image: petabridge/lighthouse:linux-latest
hostname: lighthouse.main
environment:
ACTORSYSTEM: "ClusterSystem"
CLUSTER_PORT: 4053
CLUSTER_IP: "lighthouse.main"
CLUSTER_SEEDS: "[akka.tcp://ClusterSystem@lighthouse.main:4053]"
cluster.one:
image: simple.cluster
build: .
hostname: cluster.one
networks:
- subnet.one
depends_on: [lighthouse.main]
environment:
CLUSTER_PORT: 4053
CLUSTER_SEEDS: "[akka.tcp://ClusterSystem@lighthouse.main:4053]"
cluster.two:
image: simple.cluster
hostname: cluster.two
networks:
- subnet.two
depends_on: [lighthouse.main]
environment:
CLUSTER_PORT: 4053
CLUSTER_SEEDS: "[akka.tcp://ClusterSystem@lighthouse.main:4053]"
networks:
default:
driver: bridge
subnet.one:
ipam:
driver: default
config:
- subnet: 172.16.237.0/24
gateway: 172.16.237.1
subnet.two:
ipam:
driver: default
config:
- subnet: 172.16.238.0/24
gateway: 172.16.238.1
FROM mcr.microsoft.com/dotnet/runtime:5.0 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["Simple.Cluster.csproj", "Simple.Cluster/"]
RUN dotnet restore "Simple.Cluster/Simple.Cluster.csproj"
COPY . "Simple.Cluster/"
WORKDIR "/src/Simple.Cluster"
RUN dotnet build "Simple.Cluster.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Simple.Cluster.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Simple.Cluster.dll"]
using System;
using Akka.Actor;
using Akka.Bootstrap.Docker;
using Akka.Configuration;
namespace Simple.Cluster
{
class Program
{
internal static void Main(string[] args)
{
var setup = BootstrapSetup.Create()
.WithConfig(ConfigurationFactory.Empty.BootstrapFromDocker())
.WithActorRefProvider(ProviderSelection.Cluster.Instance);
var system = ActorSystem.Create("ClusterSystem", setup);
system.ActorOf(ClusterListener.Props(), "ClusterListener");
Console.CancelKeyPress += async (sender, eventArgs) => { await system.Terminate(); };
system.WhenTerminated.Wait();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment