소스 검색

add consul discovery.

master
yangxiaodong 7 년 전
부모
커밋
d5487de4c4
7개의 변경된 파일185개의 추가작업 그리고 0개의 파일을 삭제
  1. +19
    -0
      src/DotNetCore.CAP/NodeDiscovery/IDiscoveryProviderFactory.Default.cs
  2. +11
    -0
      src/DotNetCore.CAP/NodeDiscovery/IDiscoveryProviderFactory.cs
  3. +69
    -0
      src/DotNetCore.CAP/NodeDiscovery/INodeDiscoveryProvider.Consul.cs
  4. +14
    -0
      src/DotNetCore.CAP/NodeDiscovery/INodeDiscoveryProvider.cs
  5. +39
    -0
      src/DotNetCore.CAP/NodeDiscovery/IProcessingServer.Consul.cs
  6. +16
    -0
      src/DotNetCore.CAP/NodeDiscovery/Node.cs
  7. +17
    -0
      src/DotNetCore.CAP/NodeDiscovery/ServerNodeConfiguration.cs

+ 19
- 0
src/DotNetCore.CAP/NodeDiscovery/IDiscoveryProviderFactory.Default.cs 파일 보기

@@ -0,0 +1,19 @@
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);
}
}
}

+ 11
- 0
src/DotNetCore.CAP/NodeDiscovery/IDiscoveryProviderFactory.cs 파일 보기

@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace DotNetCore.CAP.NodeDiscovery
{
interface IDiscoveryProviderFactory
{
INodeDiscoveryProvider Get(NodeConfiguration configuration);
}
}

+ 69
- 0
src/DotNetCore.CAP/NodeDiscovery/INodeDiscoveryProvider.Consul.cs 파일 보기

@@ -0,0 +1,69 @@
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"
}
});
}
}
}

+ 14
- 0
src/DotNetCore.CAP/NodeDiscovery/INodeDiscoveryProvider.cs 파일 보기

@@ -0,0 +1,14 @@
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);
}
}

+ 39
- 0
src/DotNetCore.CAP/NodeDiscovery/IProcessingServer.Consul.cs 파일 보기

@@ -0,0 +1,39 @@
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()
{

}
}
}

+ 16
- 0
src/DotNetCore.CAP/NodeDiscovery/Node.cs 파일 보기

@@ -0,0 +1,16 @@
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; }


}
}

+ 17
- 0
src/DotNetCore.CAP/NodeDiscovery/ServerNodeConfiguration.cs 파일 보기

@@ -0,0 +1,17 @@
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";
}
}

불러오는 중...
취소
저장