diff --git a/src/DotNetCore.CAP/Dashboard/RazorPage.cs b/src/DotNetCore.CAP/Dashboard/RazorPage.cs index 3704e48..cd55cbf 100644 --- a/src/DotNetCore.CAP/Dashboard/RazorPage.cs +++ b/src/DotNetCore.CAP/Dashboard/RazorPage.cs @@ -3,6 +3,8 @@ using System.Diagnostics; using System.Net; using System.Text; using DotNetCore.CAP.Dashboard.Monitoring; +using DotNetCore.CAP.NodeDiscovery; +using Microsoft.Extensions.DependencyInjection; namespace DotNetCore.CAP.Dashboard { @@ -85,13 +87,29 @@ namespace DotNetCore.CAP.Dashboard var monitoring = Storage.GetMonitoringApi(); var dto = monitoring.GetStatistics(); - if (CapCache.Global.TryGet("cap.nodes.count", out var count)) - dto.Servers = (int) count; + SetServersCount(dto); return dto; }); } + private void SetServersCount(StatisticsDto dto) + { + if (CapCache.Global.TryGet("cap.nodes.count", out var count)) + { + dto.Servers = (int) count; + } + else + { + if (RequestServices.GetService() != null) + { + var discoveryProvider = RequestServices.GetService(); + var nodes = discoveryProvider.GetNodes().GetAwaiter().GetResult(); + dto.Servers = nodes.Count; + } + } + } + /// protected void WriteLiteral(string textToAppend) { diff --git a/src/DotNetCore.CAP/NodeDiscovery/IDiscoveryProviderFactory.Default.cs b/src/DotNetCore.CAP/NodeDiscovery/IDiscoveryProviderFactory.Default.cs index 2f0eb5f..e7639a6 100644 --- a/src/DotNetCore.CAP/NodeDiscovery/IDiscoveryProviderFactory.Default.cs +++ b/src/DotNetCore.CAP/NodeDiscovery/IDiscoveryProviderFactory.Default.cs @@ -1,15 +1,23 @@ using System; +using Microsoft.Extensions.Logging; namespace DotNetCore.CAP.NodeDiscovery { internal class DiscoveryProviderFactory : IDiscoveryProviderFactory { + private readonly ILoggerFactory _loggerFactory; + + public DiscoveryProviderFactory(ILoggerFactory loggerFactory) + { + _loggerFactory = loggerFactory; + } + public INodeDiscoveryProvider Create(DiscoveryOptions options) { if (options == null) throw new ArgumentNullException(nameof(options)); - return new ConsulNodeDiscoveryProvider(options); + return new ConsulNodeDiscoveryProvider(_loggerFactory, options); } } } \ No newline at end of file diff --git a/src/DotNetCore.CAP/NodeDiscovery/INodeDiscoveryProvider.Consul.cs b/src/DotNetCore.CAP/NodeDiscovery/INodeDiscoveryProvider.Consul.cs index 01fcbff..cd4e236 100644 --- a/src/DotNetCore.CAP/NodeDiscovery/INodeDiscoveryProvider.Consul.cs +++ b/src/DotNetCore.CAP/NodeDiscovery/INodeDiscoveryProvider.Consul.cs @@ -3,16 +3,19 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Consul; +using Microsoft.Extensions.Logging; namespace DotNetCore.CAP.NodeDiscovery { public class ConsulNodeDiscoveryProvider : INodeDiscoveryProvider, IDisposable { + private readonly ILogger _logger; private readonly DiscoveryOptions _options; private ConsulClient _consul; - public ConsulNodeDiscoveryProvider(DiscoveryOptions options) + public ConsulNodeDiscoveryProvider(ILoggerFactory logger, DiscoveryOptions options) { + _logger = logger.CreateLogger(); _options = options; InitClient(); @@ -27,46 +30,64 @@ namespace DotNetCore.CAP.NodeDiscovery { try { - var services = await _consul.Agent.Services(); - - var nodes = services.Response.Select(x => new Node + var nodes = new List(); + var services = await _consul.Catalog.Services(); + foreach (var service in services.Response) { - Id = x.Key, - Name = x.Value.Service, - Address = x.Value.Address, - Port = x.Value.Port, - Tags = string.Join(", ", x.Value.Tags) - }); - var nodeList = nodes.ToList(); + var serviceInfo = await _consul.Catalog.Service(service.Key); + var node = serviceInfo.Response. + SkipWhile(x => !x.ServiceTags.Contains("CAP")) + .Select(info => new Node + { + Id = info.ServiceID, + Name = info.ServiceName, + Address = info.ServiceAddress, + Port = info.ServicePort, + Tags = string.Join(", ", info.ServiceTags) + }).ToList(); + + nodes.AddRange(node); + } - CapCache.Global.AddOrUpdate("cap.nodes.count", nodeList.Count, TimeSpan.FromSeconds(30), true); + CapCache.Global.AddOrUpdate("cap.nodes.count", nodes.Count, TimeSpan.FromSeconds(60), true); - return nodeList; + return nodes; } - catch (Exception) + catch (Exception ex) { + CapCache.Global.AddOrUpdate("cap.nodes.count", 0, TimeSpan.FromSeconds(20)); + + _logger.LogError("Get consul nodes raised an exception. Exception:" + ex.Message); return null; } } public Task RegisterNode() { - return _consul.Agent.ServiceRegister(new AgentServiceRegistration + try { - ID = _options.NodeId.ToString(), - Name = _options.NodeName, - Address = _options.CurrentNodeHostName, - Port = _options.CurrentNodePort, - Tags = new[] {"CAP", "Client", "Dashboard"}, - Check = new AgentServiceCheck + return _consul.Agent.ServiceRegister(new AgentServiceRegistration { - DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(30), - Interval = TimeSpan.FromSeconds(10), - Status = HealthStatus.Passing, - HTTP = - $"http://{_options.CurrentNodeHostName}:{_options.CurrentNodePort}{_options.MatchPath}/health" - } - }); + ID = _options.NodeId.ToString(), + Name = _options.NodeName, + Address = _options.CurrentNodeHostName, + Port = _options.CurrentNodePort, + Tags = new[] { "CAP", "Client", "Dashboard" }, + Check = new AgentServiceCheck + { + DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(30), + Interval = TimeSpan.FromSeconds(10), + Status = HealthStatus.Passing, + HTTP = + $"http://{_options.CurrentNodeHostName}:{_options.CurrentNodePort}{_options.MatchPath}/health" + } + }); + } + catch (Exception ex) + { + _logger.LogError("Register consul nodes raised an exception. Exception:" + ex.Message); + return null; + } } public void InitClient()