static async Task Main(string[] args) { var system = new ActorSystem(); var remoteConfig = GrpcCoreRemoteConfig .BindToLocalhost(12000) .WithProtoMessages(ProtosReflection.Descriptor); var consulProvider = new ConsulProvider(new ConsulProviderConfig(), c => c.Address = new Uri("http://consul:8500/")); var clusterConfig = ClusterConfig.Setup("MyCluster", consulProvider, new PartitionIdentityLookup()); system .WithRemote(remoteConfig) .WithCluster(clusterConfig); var grains = new Grains(system.Cluster()); grains.HelloGrainFactory(() => new HelloGrain()); await system.Cluster().StartMemberAsync(); Console.CancelKeyPress += async(e, y) => { Console.WriteLine("Shutting Down..."); await system.Cluster().ShutdownAsync(); }; await Task.Delay(-1); }
public async Task StartAsync(CancellationToken cancellationToken) { // TODO: Register all known actors in a generic way var props = Actor.FromProducer(() => new PatternActor(eventStore)); Remote.RegisterKnownKind("pattern", props); var provider = new ConsulProvider(new ConsulProviderOptions(), configuration1 => configuration1.Address = new Uri(configuration["CONSUL_URL"])); Cluster.Start("PatternCluster", "127.0.0.1", 12001, provider); }
public void CanSetConfig() { const string key = "key1"; const string keyValue = "value1"; var registry = new FakeConfigurationRegistry(); var sut = new ConsulProvider(registry); sut.Set(key, keyValue); string value; sut.TryGet(key, out value); Assert.Equal(keyValue, value); }
public void CanReloadConfig() { var reloaded = false; var registry = new FakeConfigurationRegistry(); var sut = new ConsulProvider(registry); sut.Load(); sut.GetReloadToken() .RegisterChangeCallback(_ => reloaded = true, null); registry.FakeReload(); Assert.True(reloaded); }
/// <summary> /// 执行服务注册 /// </summary> /// <param name="app"></param> /// <returns></returns> public static void ExecServiceRegister(this IApplicationBuilder app, IApplicationLifetime lifetime, OptionsServiceInfo serviceInfo, OptionsHealth health) { if (serviceInfo == null || health == null) { return; } if (string.IsNullOrEmpty(serviceInfo.ServiceName) || string.IsNullOrEmpty(health.Router)) { return; } serviceInfo.ServiceIP = Common.Share.IPHelper.GetNetWork(serviceInfo.ServiceIP); IServiceRegisterProvider provider = new ConsulProvider();//todo:后续要改成工厂 provider.Register(lifetime, serviceInfo, health); }
private static ClusterConfig GetClusterConfig() { var consulProvider = new ConsulProvider(new ConsulProviderConfig()); //use an empty store, no persistence var store = new EmptyKeyValueStore <Subscribers>(); var clusterConfig = ClusterConfig .Setup("MyCluster", consulProvider, new PartitionIdentityLookup()) .WithClusterKind("topic", Props.FromProducer(() => new TopicActor(store))) .WithPubSubBatchSize(batchSize); return(clusterConfig); }
public static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup <Startup>() .UseApplicationInsights() .Build(); var options = new ConsulOptions(); options.Host = ConsulIP; options.HealthCheckPath = "/api/values"; var loggerFactory = new LoggerFactory(); var logger = loggerFactory.CreateLogger("logger"); var provider = new ConsulProvider(loggerFactory, Options.Create(options)); Cluster.RegisterService(new Uri(string.Format("http://{0}", Environment.MachineName)), provider, "articles", "v1", logger); host.Run(); }
private static async Task Main() { var remoteConfig = GrpcCoreRemoteConfig .BindToLocalhost() .WithProtoMessages(ProtosReflection.Descriptor); var consulProvider = new ConsulProvider(new ConsulProviderConfig(), c => c.Address = new Uri("http://consul:8500/")); var clusterConfig = ClusterConfig .Setup("MyCluster", consulProvider, new PartitionIdentityLookup()); var system = new ActorSystem() .WithRemote(remoteConfig) .WithCluster(clusterConfig); await system .Cluster() .StartMemberAsync(); //subscribe to the eventstream via type, just like you do locally system.EventStream.SubscribeToTopic <SomeMessage>("MyTopic.*", x => Console.WriteLine($"Got message for {x.Name}")); //publish messages onto the eventstream on Subtopic1 on MyTopic root //but do this using cluster broadcasting. this will publish the event //to the event stream on all the members in the cluster //this is best effort only, if some member is unavailable, there is no guarantee associated here system.Cluster().BroadcastEvent(new SomeMessage("ProtoActor", "MyTopic.Subtopic1")); //this message is published on a topic that is not subscribed to, and nothing will happen system.Cluster().BroadcastEvent(new SomeMessage("Asynkron", "AnotherTopic")); //send a message to the same root topic, but another child topic system.Cluster().BroadcastEvent(new SomeMessage("Do we get this?", "MyTopic.Subtopic1")); //this example is local only. //see ClusterEventStream for cluster broadcast onto the eventstream Console.ReadLine(); }
public ProtoClusterHostedService( Uri consulUrl, string clusterName, string nodeAddress, int nodePort, IEventStoreConnection eventStoreConnection ) { _clusterName = clusterName; _nodeAddress = nodeAddress; _nodePort = nodePort; _eventStoreConnection = eventStoreConnection; _consulProvider = new ConsulProvider( new ConsulProviderOptions { DeregisterCritical = TimeSpan.FromSeconds(30), RefreshTtl = TimeSpan.FromSeconds(2), ServiceTtl = TimeSpan.FromSeconds(10), BlockingWaitTime = TimeSpan.FromSeconds(20) }, c => c.Address = consulUrl); }
private void RegisterService() { try { Console.WriteLine("Register Service"); var options = new ConsulOptions { Host = ConfigurationManager.AppSettings["consulHost"], Port = Convert.ToInt32(ConfigurationManager.AppSettings["consulPort"]) }; var loggerFactory = new LoggerFactory(); var logger = loggerFactory.CreateLogger("logger"); var provider = new ConsulProvider(loggerFactory, Options.Create(options)); Console.WriteLine($"Connecting to consul server at: {options.Host}:{options.Port}"); Cluster.RegisterService(new Uri(_baseAddress), provider, "orders", "v1", logger, new string[] { "v1" }); AddConfiguration(); Console.WriteLine("Success!"); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } }