Kaynağa Gözat

fix consul discovery bug.

master
Savorboard 7 yıl önce
ebeveyn
işleme
cc7f2a1dfe
3 değiştirilmiş dosya ile 78 ekleme ve 31 silme
  1. +20
    -2
      src/DotNetCore.CAP/Dashboard/RazorPage.cs
  2. +9
    -1
      src/DotNetCore.CAP/NodeDiscovery/IDiscoveryProviderFactory.Default.cs
  3. +49
    -28
      src/DotNetCore.CAP/NodeDiscovery/INodeDiscoveryProvider.Consul.cs

+ 20
- 2
src/DotNetCore.CAP/Dashboard/RazorPage.cs Dosyayı Görüntüle

@@ -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<DiscoveryOptions>() != null)
{
var discoveryProvider = RequestServices.GetService<INodeDiscoveryProvider>();
var nodes = discoveryProvider.GetNodes().GetAwaiter().GetResult();
dto.Servers = nodes.Count;
}
}
}

/// <exclude />
protected void WriteLiteral(string textToAppend)
{


+ 9
- 1
src/DotNetCore.CAP/NodeDiscovery/IDiscoveryProviderFactory.Default.cs Dosyayı Görüntüle

@@ -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);
}
}
}

+ 49
- 28
src/DotNetCore.CAP/NodeDiscovery/INodeDiscoveryProvider.Consul.cs Dosyayı Görüntüle

@@ -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<ConsulNodeDiscoveryProvider> _logger;
private readonly DiscoveryOptions _options;
private ConsulClient _consul;

public ConsulNodeDiscoveryProvider(DiscoveryOptions options)
public ConsulNodeDiscoveryProvider(ILoggerFactory logger, DiscoveryOptions options)
{
_logger = logger.CreateLogger<ConsulNodeDiscoveryProvider>();
_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<Node>();
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()


Yükleniyor…
İptal
Kaydet