Fix discovery

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

@ -33,22 +33,5 @@ namespace ZeroLevel.Discovery
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");
_exInbox = ExchangeTransportFactory.GetServer(socketPort);
_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}");
}

@ -166,10 +166,11 @@ namespace ZeroLevel.Discovery
Save();
}
public InvokeResult Append(ExServiceInfo serviceInfo)
public InvokeResult Append(ExServiceInfo serviceInfo, IZBackward client)
{
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()}";
_lock.EnterWriteLock();
@ -185,22 +186,22 @@ namespace ZeroLevel.Discovery
ServiceType = serviceInfo.ServiceType,
Endpoints = new List<string>()
});
_table[key].Endpoints.Add(serviceInfo.Endpoint);
Log.Info($"The service '{serviceInfo.ServiceKey}' registered on endpoint: {serviceInfo.Endpoint}");
_table[key].Endpoints.Add(endpoint);
Log.Info($"The service '{serviceInfo.ServiceKey}' registered on endpoint: {endpoint}");
}
else
{
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}");
exists.Endpoints.Add(serviceInfo.Endpoint);
Log.Info($"The service '{serviceInfo.ServiceKey}' register endpoint: {endpoint}");
exists.Endpoints.Add(endpoint);
}
}
}
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);
}
finally
@ -212,7 +213,7 @@ namespace ZeroLevel.Discovery
}
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;
}

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

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

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

@ -1 +1 @@
4d221d21073e6ec70fc75d0c6e7d2458aa0f2b9a
23b05e1da25edde99ed56a899f894605a2243d39

Loading…
Cancel
Save

Powered by TurnKey Linux.