Fix discovery

pull/1/head
Ogoun 6 years ago
parent 00ae8a666f
commit e7e4bc2f83

@ -33,22 +33,5 @@ namespace ZeroLevel.Discovery
return BadRequestAnswer(request, ex); return BadRequestAnswer(request, ex);
} }
} }
[HttpPost]
[Route("api/v0/routes")]
[ResponseType(typeof(InvokeResult))]
public HttpResponseMessage AddRoute(HttpRequestMessage request, ExServiceInfo service)
{
try
{
var ir = Injector.Default.Resolve<RouteTable>().Append(service);
return request.CreateSelfDestroyingResponse(ir);
}
catch (Exception ex)
{
Log.Error(ex, "Error with append endpoint");
return BadRequestAnswer(request, ex);
}
}
} }
} }

@ -39,7 +39,7 @@ namespace ZeroLevel.Discovery
var socketPort = Configuration.Default.First<int>("socketport"); var socketPort = Configuration.Default.First<int>("socketport");
_exInbox = ExchangeTransportFactory.GetServer(socketPort); _exInbox = ExchangeTransportFactory.GetServer(socketPort);
_exInbox.RegisterInbox<IEnumerable<ServiceEndpointsInfo>>("services", (_, __) => routeTable.Get()); _exInbox.RegisterInbox<IEnumerable<ServiceEndpointsInfo>>("services", (_, __) => routeTable.Get());
_exInbox.RegisterInbox<ExServiceInfo, InvokeResult>("register", (info, _, __) => routeTable.Append(info)); _exInbox.RegisterInbox<ExServiceInfo, InvokeResult>("register", (info, _, client) => routeTable.Append(info, client));
Log.Info($"TCP server started {_exInbox.Endpoint.Address}:{socketPort}"); Log.Info($"TCP server started {_exInbox.Endpoint.Address}:{socketPort}");
} }

@ -166,10 +166,11 @@ namespace ZeroLevel.Discovery
Save(); Save();
} }
public InvokeResult Append(ExServiceInfo serviceInfo) public InvokeResult Append(ExServiceInfo serviceInfo, IZBackward client)
{ {
InvokeResult result = null; InvokeResult result = null;
if (Ping(serviceInfo.Endpoint, serviceInfo.ServiceKey)) var endpoint = $"{client.Endpoint.Address}:{client.Endpoint.Port}";
if (Ping(endpoint, serviceInfo.ServiceKey))
{ {
var key = $"{serviceInfo.ServiceGroup}:{serviceInfo.ServiceType}:{serviceInfo.ServiceKey.Trim().ToLowerInvariant()}"; var key = $"{serviceInfo.ServiceGroup}:{serviceInfo.ServiceType}:{serviceInfo.ServiceKey.Trim().ToLowerInvariant()}";
_lock.EnterWriteLock(); _lock.EnterWriteLock();
@ -185,22 +186,22 @@ namespace ZeroLevel.Discovery
ServiceType = serviceInfo.ServiceType, ServiceType = serviceInfo.ServiceType,
Endpoints = new List<string>() Endpoints = new List<string>()
}); });
_table[key].Endpoints.Add(serviceInfo.Endpoint); _table[key].Endpoints.Add(endpoint);
Log.Info($"The service '{serviceInfo.ServiceKey}' registered on endpoint: {serviceInfo.Endpoint}"); Log.Info($"The service '{serviceInfo.ServiceKey}' registered on endpoint: {endpoint}");
} }
else else
{ {
var exists = _table[key]; var exists = _table[key];
if (exists.Endpoints.Contains(serviceInfo.Endpoint) == false) if (exists.Endpoints.Contains(endpoint) == false)
{ {
Log.Info($"The service '{serviceInfo.ServiceKey}' register endpoint: {serviceInfo.Endpoint}"); Log.Info($"The service '{serviceInfo.ServiceKey}' register endpoint: {endpoint}");
exists.Endpoints.Add(serviceInfo.Endpoint); exists.Endpoints.Add(endpoint);
} }
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Log.Error(ex, "Fault append service ({0} {1}) endpoint '{2}'", serviceInfo.ServiceKey, serviceInfo.Version, serviceInfo.Endpoint); Log.Error(ex, "Fault append service ({0} {1}) endpoint '{2}'", serviceInfo.ServiceKey, serviceInfo.Version, endpoint);
result = InvokeResult.Fault(ex.Message); result = InvokeResult.Fault(ex.Message);
} }
finally finally
@ -212,7 +213,7 @@ namespace ZeroLevel.Discovery
} }
else else
{ {
result = InvokeResult.Fault($"Appending endpoint '{serviceInfo.Endpoint}' canceled for service {serviceInfo.ServiceKey} ({serviceInfo.Version}) because endpoind no avaliable"); result = InvokeResult.Fault($"Appending endpoint '{endpoint}' canceled for service {serviceInfo.ServiceKey} ({serviceInfo.Version}) because endpoind no avaliable");
} }
return result; return result;
} }

@ -2,6 +2,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -18,7 +19,6 @@ namespace ZeroLevel.NetworkUnitTests
// Arrange // Arrange
var info = new ExServiceInfo var info = new ExServiceInfo
{ {
Endpoint = "192.168.1.11:7755",
ServiceGroup = "MyServiceGroup", ServiceGroup = "MyServiceGroup",
ServiceKey = "MyServiceKey", ServiceKey = "MyServiceKey",
ServiceType = "MyServiceType", ServiceType = "MyServiceType",
@ -35,7 +35,7 @@ namespace ZeroLevel.NetworkUnitTests
}); });
// Act // Act
var client = ExchangeTransportFactory.GetClient(server.Endpoint.Address.ToString() + ":6666"); var client = ExchangeTransportFactory.GetClient(IPAddress.Loopback.ToString() + ":6666");
var ir = client.Send<ExServiceInfo>("register", info); var ir = client.Send<ExServiceInfo>("register", info);
locker.WaitOne(1000); locker.WaitOne(1000);
@ -56,7 +56,6 @@ namespace ZeroLevel.NetworkUnitTests
// Arrange // Arrange
var info1 = new ExServiceInfo var info1 = new ExServiceInfo
{ {
Endpoint = "192.168.1.11:7755",
ServiceGroup = "MyServiceGroup", ServiceGroup = "MyServiceGroup",
ServiceKey = "MyServiceKey", ServiceKey = "MyServiceKey",
ServiceType = "MyServiceType", ServiceType = "MyServiceType",
@ -64,7 +63,6 @@ namespace ZeroLevel.NetworkUnitTests
}; };
var info2 = new ExServiceInfo var info2 = new ExServiceInfo
{ {
Endpoint = "192.168.41.11:4564",
ServiceGroup = "MyServiceGroup", ServiceGroup = "MyServiceGroup",
ServiceKey = "MyServiceKey2", ServiceKey = "MyServiceKey2",
ServiceType = "MyServiceType", ServiceType = "MyServiceType",
@ -77,7 +75,7 @@ namespace ZeroLevel.NetworkUnitTests
server.RegisterInbox<IEnumerable<ExServiceInfo>>("services", (_, __) => new[] { info1, info2 }); server.RegisterInbox<IEnumerable<ExServiceInfo>>("services", (_, __) => new[] { info1, info2 });
// Act // Act
var client = ExchangeTransportFactory.GetClient(server.Endpoint.Address.ToString() + ":6666"); var client = ExchangeTransportFactory.GetClient(IPAddress.Loopback.ToString() + ":6666");
var ir = client.Request<IEnumerable<ExServiceInfo>>("services", response => var ir = client.Request<IEnumerable<ExServiceInfo>>("services", response =>
{ {
received = response; received = response;

@ -30,13 +30,6 @@ namespace ZeroLevel.Network
/// </summary> /// </summary>
[DataMember] [DataMember]
public string ServiceType { get; set; } = DEFAULT_TYPE_NAME; public string ServiceType { get; set; } = DEFAULT_TYPE_NAME;
/// <summary>
/// Connection point, address
/// </summary>
[DataMember]
public string Endpoint { get; set; }
/// <summary> /// <summary>
/// Service version /// Service version
/// </summary> /// </summary>
@ -51,8 +44,6 @@ namespace ZeroLevel.Network
if (string.Compare(this.ServiceKey, other.ServiceKey, true) != 0) return false; if (string.Compare(this.ServiceKey, other.ServiceKey, true) != 0) return false;
if (string.Compare(this.ServiceGroup, other.ServiceGroup, true) != 0) return false; if (string.Compare(this.ServiceGroup, other.ServiceGroup, true) != 0) return false;
if (string.Compare(this.ServiceType, other.ServiceType, true) != 0) return false; if (string.Compare(this.ServiceType, other.ServiceType, true) != 0) return false;
if (string.Compare(this.Endpoint, other.Endpoint, true) != 0) return false;
if (string.Compare(this.Version, other.Version, true) != 0) return false; if (string.Compare(this.Version, other.Version, true) != 0) return false;
return true; return true;
} }
@ -64,7 +55,7 @@ namespace ZeroLevel.Network
public override int GetHashCode() public override int GetHashCode()
{ {
return this.ServiceKey.GetHashCode() ^ this.Endpoint.GetHashCode(); return this.ServiceKey.GetHashCode();
} }
public void Serialize(IBinaryWriter writer) public void Serialize(IBinaryWriter writer)
@ -72,7 +63,6 @@ namespace ZeroLevel.Network
writer.WriteString(this.ServiceKey); writer.WriteString(this.ServiceKey);
writer.WriteString(this.ServiceGroup); writer.WriteString(this.ServiceGroup);
writer.WriteString(this.ServiceType); writer.WriteString(this.ServiceType);
writer.WriteString(this.Endpoint);
writer.WriteString(this.Version); writer.WriteString(this.Version);
} }
@ -81,7 +71,6 @@ namespace ZeroLevel.Network
this.ServiceKey = reader.ReadString(); this.ServiceKey = reader.ReadString();
this.ServiceGroup = reader.ReadString(); this.ServiceGroup = reader.ReadString();
this.ServiceType = reader.ReadString(); this.ServiceType = reader.ReadString();
this.Endpoint = reader.ReadString();
this.Version = reader.ReadString(); this.Version = reader.ReadString();
} }

@ -3,6 +3,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Net;
using System.Reflection; using System.Reflection;
namespace ZeroLevel.Network namespace ZeroLevel.Network
@ -46,7 +47,6 @@ namespace ZeroLevel.Network
Server = server, Server = server,
ServiceInfo = new ExServiceInfo ServiceInfo = new ExServiceInfo
{ {
Endpoint = $"{server.Endpoint.Address}:{server.Endpoint.Port}",
ServiceKey = service.Key, ServiceKey = service.Key,
Version = service.Version, Version = service.Version,
ServiceGroup = service.Group, ServiceGroup = service.Group,
@ -88,7 +88,6 @@ namespace ZeroLevel.Network
Server = server, Server = server,
ServiceInfo = new ExServiceInfo ServiceInfo = new ExServiceInfo
{ {
Endpoint = $"{server.Endpoint.Address}:{server.Endpoint.Port}",
ServiceKey = serviceInfo.ServiceKey, ServiceKey = serviceInfo.ServiceKey,
Version = serviceInfo.Version, Version = serviceInfo.Version,
ServiceGroup = serviceInfo.ServiceGroup, ServiceGroup = serviceInfo.ServiceGroup,

@ -1 +1 @@
4d221d21073e6ec70fc75d0c6e7d2458aa0f2b9a 23b05e1da25edde99ed56a899f894605a2243d39

Loading…
Cancel
Save

Powered by TurnKey Linux.