Commit d5487de4 authored by yangxiaodong's avatar yangxiaodong

add consul discovery.

parent b415315e
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);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace DotNetCore.CAP.NodeDiscovery
{
interface IDiscoveryProviderFactory
{
INodeDiscoveryProvider Get(NodeConfiguration configuration);
}
}
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<IList<Node>> 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"
}
});
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace DotNetCore.CAP.NodeDiscovery
{
interface INodeDiscoveryProvider
{
Task<IList<Node>> GetNodes();
Task RegisterNode(string address, int port);
}
}
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()
{
}
}
}
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; }
}
}
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";
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment