Przeglądaj źródła

refactor.

master
yangxiaodong 7 lat temu
rodzic
commit
ca38bff1bb
9 zmienionych plików z 126 dodań i 60 usunięć
  1. +33
    -0
      src/DotNetCore.CAP/NodeDiscovery/CAP.DiscoveryOptions.cs
  2. +52
    -0
      src/DotNetCore.CAP/NodeDiscovery/CAP.DiscoveryOptionsExtensions.cs
  3. +4
    -4
      src/DotNetCore.CAP/NodeDiscovery/IDiscoveryProviderFactory.Default.cs
  4. +1
    -1
      src/DotNetCore.CAP/NodeDiscovery/IDiscoveryProviderFactory.cs
  5. +27
    -28
      src/DotNetCore.CAP/NodeDiscovery/INodeDiscoveryProvider.Consul.cs
  6. +1
    -1
      src/DotNetCore.CAP/NodeDiscovery/INodeDiscoveryProvider.cs
  7. +6
    -8
      src/DotNetCore.CAP/NodeDiscovery/IProcessingServer.Consul.cs
  8. +2
    -1
      src/DotNetCore.CAP/NodeDiscovery/Node.cs
  9. +0
    -17
      src/DotNetCore.CAP/NodeDiscovery/ServerNodeConfiguration.cs

+ 33
- 0
src/DotNetCore.CAP/NodeDiscovery/CAP.DiscoveryOptions.cs Wyświetl plik

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

}

+ 52
- 0
src/DotNetCore.CAP/NodeDiscovery/CAP.DiscoveryOptionsExtensions.cs Wyświetl plik

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

+ 4
- 4
src/DotNetCore.CAP/NodeDiscovery/IDiscoveryProviderFactory.Default.cs Wyświetl plik

@@ -6,14 +6,14 @@ namespace DotNetCore.CAP.NodeDiscovery
{
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);
}
}
}

+ 1
- 1
src/DotNetCore.CAP/NodeDiscovery/IDiscoveryProviderFactory.cs Wyświetl plik

@@ -6,6 +6,6 @@ namespace DotNetCore.CAP.NodeDiscovery
{
interface IDiscoveryProviderFactory
{
INodeDiscoveryProvider Get(NodeConfiguration configuration);
INodeDiscoveryProvider Create(DiscoveryOptions options);
}
}

+ 27
- 28
src/DotNetCore.CAP/NodeDiscovery/INodeDiscoveryProvider.Consul.cs Wyświetl plik

@@ -7,63 +7,62 @@ using Consul;

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 =>
{
config.Address = new Uri($"http://{_hostName}:{_port}");
config.Address = new Uri($"http://{_options.DiscoveryServerHostName}:{_options.DiscoveryServerProt}");
});
}

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

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
{
Name = "CAP",
Port = port,
Address = address,
Name = _options.NodeName,
Address = _options.CurrentNodeHostName,
Port = _options.CurrentNodePort,
Tags = new string[] { "CAP", "Client", "Dashboard" },
Check = new AgentServiceCheck
{
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(30),
Interval = TimeSpan.FromSeconds(10),
Status = HealthStatus.Passing,
HTTP = "/CAP"
HTTP = $"http://{_options.CurrentNodeHostName}:{_options.CurrentNodePort}{_options.MatchPath}/health"
}
});
}

public void Dispose()
{
_consul.Dispose();
}
}
}

+ 1
- 1
src/DotNetCore.CAP/NodeDiscovery/INodeDiscoveryProvider.cs Wyświetl plik

@@ -9,6 +9,6 @@ namespace DotNetCore.CAP.NodeDiscovery
{
Task<IList<Node>> GetNodes();

Task RegisterNode(string address, int port);
Task RegisterNode();
}
}

+ 6
- 8
src/DotNetCore.CAP/NodeDiscovery/IProcessingServer.Consul.cs Wyświetl plik

@@ -6,11 +6,11 @@ namespace DotNetCore.CAP.NodeDiscovery
{
class ConsulProcessingNodeServer : IProcessingServer
{
private readonly DashboardOptions dashboardOptions;
private readonly DiscoveryOptions dashboardOptions;
private readonly IDiscoveryProviderFactory discoveryProviderFactory;

public ConsulProcessingNodeServer(
DashboardOptions dashboardOptions,
DiscoveryOptions dashboardOptions,
IDiscoveryProviderFactory discoveryProviderFactory)
{
this.dashboardOptions = dashboardOptions;
@@ -19,16 +19,14 @@ namespace DotNetCore.CAP.NodeDiscovery

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()
{
//ignore
}

public void Dispose()


+ 2
- 1
src/DotNetCore.CAP/NodeDiscovery/Node.cs Wyświetl plik

@@ -6,11 +6,12 @@ namespace DotNetCore.CAP.NodeDiscovery
{
class Node
{

public string Name { get; set; }

public string Address { get; set; }

public int Port { get; set; }

public string Tags { get; set; }
}
}

+ 0
- 17
src/DotNetCore.CAP/NodeDiscovery/ServerNodeConfiguration.cs Wyświetl plik

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

Ładowanie…
Anuluj
Zapisz