-
-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathProgram.cs
More file actions
78 lines (62 loc) · 2.31 KB
/
Program.cs
File metadata and controls
78 lines (62 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.Threading.Tasks;
using Google.Protobuf.WellKnownTypes;
using Microsoft.Extensions.Logging;
using Proto;
using Proto.Cluster;
using Proto.Cluster.Gossip;
using Proto.Cluster.Partition;
using Proto.Cluster.Testing;
using Proto.Remote;
using Proto.Remote.GrpcNet;
using Proto.Remote;
using System.Linq;
namespace ClusterGossip;
internal static class Program
{
private const string GossipKey = "demo-key";
public static async Task Main()
{
Log.SetLoggerFactory(LoggerFactory.Create(b => b.AddConsole().SetMinimumLevel(LogLevel.Information)));
var agent = new InMemAgent();
var cluster1 = CreateCluster(agent);
var cluster2 = CreateCluster(agent);
await cluster1.StartMemberAsync();
await cluster2.StartMemberAsync();
await cluster1.Gossip.SetStateAsync(GossipKey, new StringValue { Value = "hello from node1" });
await Task.Delay(1000);
var values = await cluster2.Gossip.GetState<StringValue>(GossipKey);
foreach (var (member, v) in values)
{
Console.WriteLine($"Node2 received '{v.Value}' from {member}");
}
var topology = await cluster2.Gossip.GetState<ClusterTopology>(GossipKeys.Topology);
var first = topology.Values.FirstOrDefault();
Console.WriteLine("Members seen via gossip:");
if (first != null)
{
foreach (var member in first.Members)
{
Console.WriteLine($" - {member.Id}");
}
}
Console.WriteLine("Press any key to exit");
Console.ReadKey();
await cluster1.ShutdownAsync();
await cluster2.ShutdownAsync();
}
private static Cluster CreateCluster(InMemAgent agent)
{
var system = new ActorSystem()
.WithRemote(RemoteConfig.BindToLocalhost().WithProtoMessages(WrappersReflection.Descriptor))
.WithCluster(
ClusterConfig.Setup("gossip-cluster", new TestProvider(new TestProviderOptions(), agent),
new PartitionIdentityLookup())
.WithClusterKind("empty", Props.FromProducer(() => new EmptyActor())));
return system.Cluster();
}
private class EmptyActor : IActor
{
public Task ReceiveAsync(IContext context) => Task.CompletedTask;
}
}