diff --git a/src/DotNetCore.CAP/NodeDiscovery/IDiscoveryProviderFactory.Default.cs b/src/DotNetCore.CAP/NodeDiscovery/IDiscoveryProviderFactory.Default.cs new file mode 100644 index 0000000..c83dc6d --- /dev/null +++ b/src/DotNetCore.CAP/NodeDiscovery/IDiscoveryProviderFactory.Default.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace DotNetCore.CAP.NodeDiscovery +{ + class DiscoveryProviderFactory : IDiscoveryProviderFactory + { + public INodeDiscoveryProvider Get(NodeConfiguration configuration) + { + if (configuration == null) + { + return null; + } + + return new ConsulNodeDiscoveryProvider(configuration.ServerHostName, configuration.ServerProt); + } + } +} diff --git a/src/DotNetCore.CAP/NodeDiscovery/IDiscoveryProviderFactory.cs b/src/DotNetCore.CAP/NodeDiscovery/IDiscoveryProviderFactory.cs new file mode 100644 index 0000000..a2fdb9e --- /dev/null +++ b/src/DotNetCore.CAP/NodeDiscovery/IDiscoveryProviderFactory.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace DotNetCore.CAP.NodeDiscovery +{ + interface IDiscoveryProviderFactory + { + INodeDiscoveryProvider Get(NodeConfiguration configuration); + } +} diff --git a/src/DotNetCore.CAP/NodeDiscovery/INodeDiscoveryProvider.Consul.cs b/src/DotNetCore.CAP/NodeDiscovery/INodeDiscoveryProvider.Consul.cs new file mode 100644 index 0000000..b1ad779 --- /dev/null +++ b/src/DotNetCore.CAP/NodeDiscovery/INodeDiscoveryProvider.Consul.cs @@ -0,0 +1,69 @@ +using System; +using System.Linq; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Consul; + +namespace DotNetCore.CAP.NodeDiscovery +{ + class ConsulNodeDiscoveryProvider : INodeDiscoveryProvider + { + private readonly string _hostName; + private readonly int _port; + + private readonly ConsulClient _consul; + + public ConsulNodeDiscoveryProvider(string hostName, int port) + { + _hostName = hostName; + _port = port; + + _consul = new ConsulClient(config => + { + config.Address = new Uri($"http://{_hostName}:{_port}"); + }); + } + + public async Task> GetNodes() + { + var members = await _consul.Agent.Members(false); + + var nodes = members.Response.Select(x => new Node + { + Address = x.Addr, + Name = x.Name + }); + + return nodes.ToList(); + } + + public Task RegisterNode(string address, int port) + { + //CatalogRegistration registration = new CatalogRegistration(); + //registration.Node = "CAP"; + //registration.Address = "192.168.2.55"; + //registration.Service = new AgentService + //{ + // Port = 5000, + // Service = "CAP.Test.Service" + //}; + //return _consul.Catalog.Register(registration); + + return _consul.Agent.ServiceRegister(new AgentServiceRegistration + { + Name = "CAP", + Port = port, + Address = address, + Tags = new string[] { "CAP", "Client", "Dashboard" }, + Check = new AgentServiceCheck + { + DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(30), + Interval = TimeSpan.FromSeconds(10), + Status = HealthStatus.Passing, + HTTP = "/CAP" + } + }); + } + } +} diff --git a/src/DotNetCore.CAP/NodeDiscovery/INodeDiscoveryProvider.cs b/src/DotNetCore.CAP/NodeDiscovery/INodeDiscoveryProvider.cs new file mode 100644 index 0000000..b8f1508 --- /dev/null +++ b/src/DotNetCore.CAP/NodeDiscovery/INodeDiscoveryProvider.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace DotNetCore.CAP.NodeDiscovery +{ + interface INodeDiscoveryProvider + { + Task> GetNodes(); + + Task RegisterNode(string address, int port); + } +} diff --git a/src/DotNetCore.CAP/NodeDiscovery/IProcessingServer.Consul.cs b/src/DotNetCore.CAP/NodeDiscovery/IProcessingServer.Consul.cs new file mode 100644 index 0000000..ea6e738 --- /dev/null +++ b/src/DotNetCore.CAP/NodeDiscovery/IProcessingServer.Consul.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace DotNetCore.CAP.NodeDiscovery +{ + class ConsulProcessingNodeServer : IProcessingServer + { + private readonly DashboardOptions dashboardOptions; + private readonly IDiscoveryProviderFactory discoveryProviderFactory; + + public ConsulProcessingNodeServer( + DashboardOptions dashboardOptions, + IDiscoveryProviderFactory discoveryProviderFactory) + { + this.dashboardOptions = dashboardOptions; + this.discoveryProviderFactory = discoveryProviderFactory; + } + + public void Start() + { + if (dashboardOptions.Discovery != null) + { + var discoveryProvider = discoveryProviderFactory.Get(dashboardOptions.Discovery); + discoveryProvider.RegisterNode("192.168.2.55", dashboardOptions.Discovery.CurrentPort); + } + } + + public void Pulse() + { + + } + + public void Dispose() + { + + } + } +} diff --git a/src/DotNetCore.CAP/NodeDiscovery/Node.cs b/src/DotNetCore.CAP/NodeDiscovery/Node.cs new file mode 100644 index 0000000..6fc340a --- /dev/null +++ b/src/DotNetCore.CAP/NodeDiscovery/Node.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace DotNetCore.CAP.NodeDiscovery +{ + class Node + { + + public string Name { get; set; } + + public string Address { get; set; } + + + } +} diff --git a/src/DotNetCore.CAP/NodeDiscovery/ServerNodeConfiguration.cs b/src/DotNetCore.CAP/NodeDiscovery/ServerNodeConfiguration.cs new file mode 100644 index 0000000..4814f48 --- /dev/null +++ b/src/DotNetCore.CAP/NodeDiscovery/ServerNodeConfiguration.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace DotNetCore.CAP.NodeDiscovery +{ + public class NodeConfiguration + { + public string ServerHostName { get; set; } + + public int ServerProt { get; set; } + + public int CurrentPort { get; set; } + + public string PathMatch { get; set; } = "/cap"; + } +}