@@ -0,0 +1,33 @@ | |||||
namespace DotNetCore.CAP | |||||
{ | |||||
public class DiscoveryOptions | |||||
{ | |||||
public const string DefaultDiscoveryServerHost = "localhost"; | |||||
public const int DefaultDiscoveryServerProt = 8500; | |||||
public const string DefaultCurrentNodeHostName = "localhost"; | |||||
public const int DefaultCurrentNodePort = 5000; | |||||
public const string DefaultMatchPath = "/cap"; | |||||
public DiscoveryOptions() | |||||
{ | |||||
DiscoveryServerHostName = DefaultDiscoveryServerHost; | |||||
DiscoveryServerProt = DefaultDiscoveryServerProt; | |||||
CurrentNodeHostName = DefaultCurrentNodeHostName; | |||||
CurrentNodePort = DefaultCurrentNodePort; | |||||
MatchPath = DefaultMatchPath; | |||||
} | |||||
public string DiscoveryServerHostName { get; set; } | |||||
public int DiscoveryServerProt { get; set; } | |||||
public string CurrentNodeHostName { get; set; } | |||||
public int CurrentNodePort { get; set; } | |||||
public string NodeName { get; set; } | |||||
public string MatchPath { get; set; } | |||||
} | |||||
} |
@@ -0,0 +1,52 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Text; | |||||
namespace DotNetCore.CAP | |||||
{ | |||||
using DotNetCore.CAP.NodeDiscovery; | |||||
using Microsoft.Extensions.DependencyInjection; | |||||
internal sealed class DiscoveryOptionsExtension : ICapOptionsExtension | |||||
{ | |||||
private readonly Action<DiscoveryOptions> _options; | |||||
public DiscoveryOptionsExtension(Action<DiscoveryOptions> option) | |||||
{ | |||||
_options = option; | |||||
} | |||||
public void AddServices(IServiceCollection services) | |||||
{ | |||||
var discoveryOptions = new DiscoveryOptions(); | |||||
_options?.Invoke(discoveryOptions); | |||||
services.AddSingleton(discoveryOptions); | |||||
services.AddSingleton<IDiscoveryProviderFactory, DiscoveryProviderFactory>(); | |||||
services.AddSingleton<IProcessingServer, ConsulProcessingNodeServer>(); | |||||
} | |||||
} | |||||
} | |||||
namespace Microsoft.Extensions.DependencyInjection | |||||
{ | |||||
using DotNetCore.CAP; | |||||
public static class CapDiscoveryOptionsExtensions | |||||
{ | |||||
public static CapOptions UseDiscovery(this CapOptions capOptions) | |||||
{ | |||||
return capOptions.UseDiscovery(opt => {}); | |||||
} | |||||
public static CapOptions UseDiscovery(this CapOptions capOptions, Action<DiscoveryOptions> options) | |||||
{ | |||||
if (options == null) throw new ArgumentNullException(nameof(options)); | |||||
capOptions.RegisterExtension(new DiscoveryOptionsExtension(options)); | |||||
return capOptions; | |||||
} | |||||
} | |||||
} |
@@ -6,14 +6,14 @@ namespace DotNetCore.CAP.NodeDiscovery | |||||
{ | { | ||||
class DiscoveryProviderFactory : IDiscoveryProviderFactory | class DiscoveryProviderFactory : IDiscoveryProviderFactory | ||||
{ | { | ||||
public INodeDiscoveryProvider Get(NodeConfiguration configuration) | |||||
public INodeDiscoveryProvider Create(DiscoveryOptions options) | |||||
{ | { | ||||
if (configuration == null) | |||||
if (options == null) | |||||
{ | { | ||||
return null; | |||||
throw new ArgumentNullException(nameof(options)); | |||||
} | } | ||||
return new ConsulNodeDiscoveryProvider(configuration.ServerHostName, configuration.ServerProt); | |||||
return new ConsulNodeDiscoveryProvider(options); | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -6,6 +6,6 @@ namespace DotNetCore.CAP.NodeDiscovery | |||||
{ | { | ||||
interface IDiscoveryProviderFactory | interface IDiscoveryProviderFactory | ||||
{ | { | ||||
INodeDiscoveryProvider Get(NodeConfiguration configuration); | |||||
INodeDiscoveryProvider Create(DiscoveryOptions options); | |||||
} | } | ||||
} | } |
@@ -7,63 +7,62 @@ using Consul; | |||||
namespace DotNetCore.CAP.NodeDiscovery | namespace DotNetCore.CAP.NodeDiscovery | ||||
{ | { | ||||
class ConsulNodeDiscoveryProvider : INodeDiscoveryProvider | |||||
class ConsulNodeDiscoveryProvider : INodeDiscoveryProvider, IDisposable | |||||
{ | { | ||||
private readonly string _hostName; | |||||
private readonly int _port; | |||||
private ConsulClient _consul; | |||||
private readonly DiscoveryOptions _options; | |||||
private readonly ConsulClient _consul; | |||||
public ConsulNodeDiscoveryProvider(string hostName, int port) | |||||
public ConsulNodeDiscoveryProvider(DiscoveryOptions options) | |||||
{ | { | ||||
_hostName = hostName; | |||||
_port = port; | |||||
_options = options; | |||||
InitClient(); | |||||
} | |||||
public void InitClient() | |||||
{ | |||||
_consul = new ConsulClient(config => | _consul = new ConsulClient(config => | ||||
{ | { | ||||
config.Address = new Uri($"http://{_hostName}:{_port}"); | |||||
config.Address = new Uri($"http://{_options.DiscoveryServerHostName}:{_options.DiscoveryServerProt}"); | |||||
}); | }); | ||||
} | } | ||||
public async Task<IList<Node>> GetNodes() | public async Task<IList<Node>> GetNodes() | ||||
{ | { | ||||
var members = await _consul.Agent.Members(false); | |||||
var services = await _consul.Agent.Services(); | |||||
var nodes = members.Response.Select(x => new Node | |||||
var nodes = services.Response.Select(x => new Node | |||||
{ | { | ||||
Address = x.Addr, | |||||
Name = x.Name | |||||
Name = x.Key, | |||||
Address = x.Value.Address, | |||||
Port = x.Value.Port, | |||||
Tags = string.Join(", ", x.Value.Tags) | |||||
}); | }); | ||||
return nodes.ToList(); | return nodes.ToList(); | ||||
} | } | ||||
public Task RegisterNode(string address, int port) | |||||
public Task RegisterNode() | |||||
{ | { | ||||
//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 | return _consul.Agent.ServiceRegister(new AgentServiceRegistration | ||||
{ | { | ||||
Name = "CAP", | |||||
Port = port, | |||||
Address = address, | |||||
Name = _options.NodeName, | |||||
Address = _options.CurrentNodeHostName, | |||||
Port = _options.CurrentNodePort, | |||||
Tags = new string[] { "CAP", "Client", "Dashboard" }, | Tags = new string[] { "CAP", "Client", "Dashboard" }, | ||||
Check = new AgentServiceCheck | Check = new AgentServiceCheck | ||||
{ | { | ||||
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(30), | DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(30), | ||||
Interval = TimeSpan.FromSeconds(10), | Interval = TimeSpan.FromSeconds(10), | ||||
Status = HealthStatus.Passing, | Status = HealthStatus.Passing, | ||||
HTTP = "/CAP" | |||||
HTTP = $"http://{_options.CurrentNodeHostName}:{_options.CurrentNodePort}{_options.MatchPath}/health" | |||||
} | } | ||||
}); | }); | ||||
} | } | ||||
public void Dispose() | |||||
{ | |||||
_consul.Dispose(); | |||||
} | |||||
} | } | ||||
} | } |
@@ -9,6 +9,6 @@ namespace DotNetCore.CAP.NodeDiscovery | |||||
{ | { | ||||
Task<IList<Node>> GetNodes(); | Task<IList<Node>> GetNodes(); | ||||
Task RegisterNode(string address, int port); | |||||
Task RegisterNode(); | |||||
} | } | ||||
} | } |
@@ -6,11 +6,11 @@ namespace DotNetCore.CAP.NodeDiscovery | |||||
{ | { | ||||
class ConsulProcessingNodeServer : IProcessingServer | class ConsulProcessingNodeServer : IProcessingServer | ||||
{ | { | ||||
private readonly DashboardOptions dashboardOptions; | |||||
private readonly DiscoveryOptions dashboardOptions; | |||||
private readonly IDiscoveryProviderFactory discoveryProviderFactory; | private readonly IDiscoveryProviderFactory discoveryProviderFactory; | ||||
public ConsulProcessingNodeServer( | public ConsulProcessingNodeServer( | ||||
DashboardOptions dashboardOptions, | |||||
DiscoveryOptions dashboardOptions, | |||||
IDiscoveryProviderFactory discoveryProviderFactory) | IDiscoveryProviderFactory discoveryProviderFactory) | ||||
{ | { | ||||
this.dashboardOptions = dashboardOptions; | this.dashboardOptions = dashboardOptions; | ||||
@@ -19,16 +19,14 @@ namespace DotNetCore.CAP.NodeDiscovery | |||||
public void Start() | public void Start() | ||||
{ | { | ||||
if (dashboardOptions.Discovery != null) | |||||
{ | |||||
var discoveryProvider = discoveryProviderFactory.Get(dashboardOptions.Discovery); | |||||
discoveryProvider.RegisterNode("192.168.2.55", dashboardOptions.Discovery.CurrentPort); | |||||
} | |||||
var discoveryProvider = discoveryProviderFactory.Create(dashboardOptions); | |||||
discoveryProvider.RegisterNode(); | |||||
} | } | ||||
public void Pulse() | public void Pulse() | ||||
{ | { | ||||
//ignore | |||||
} | } | ||||
public void Dispose() | public void Dispose() | ||||
@@ -6,11 +6,12 @@ namespace DotNetCore.CAP.NodeDiscovery | |||||
{ | { | ||||
class Node | class Node | ||||
{ | { | ||||
public string Name { get; set; } | public string Name { get; set; } | ||||
public string Address { get; set; } | public string Address { get; set; } | ||||
public int Port { get; set; } | |||||
public string Tags { get; set; } | |||||
} | } | ||||
} | } |
@@ -1,17 +0,0 @@ | |||||
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"; | |||||
} | |||||
} |