Fix service exchange

pull/1/head
Ogoun 6 years ago
parent 3c3512ec18
commit c206ff71fe

@ -4,7 +4,7 @@ using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using ZeroLevel.Models;
using ZeroLevel.Network.Microservices;
using ZeroLevel.Network;
namespace ZeroLevel.Discovery
{
@ -37,7 +37,7 @@ namespace ZeroLevel.Discovery
[HttpPost]
[Route("api/v0/routes")]
[ResponseType(typeof(InvokeResult))]
public HttpResponseMessage AddRoute(HttpRequestMessage request, MicroserviceInfo service)
public HttpResponseMessage AddRoute(HttpRequestMessage request, ExServiceInfo service)
{
try
{

@ -1,10 +1,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
using ZeroLevel.Models;
using ZeroLevel.Network;
using ZeroLevel.Network.Microservices;
using ZeroLevel.Services.Applications;
using ZeroLevel.Services.Network;
namespace ZeroLevel.Discovery
{
@ -18,6 +15,10 @@ namespace ZeroLevel.Discovery
{
}
public override void DisposeResources()
{
}
public override void PauseAction()
{
}

@ -6,7 +6,6 @@ using System.Linq;
using System.Threading;
using ZeroLevel.Models;
using ZeroLevel.Network;
using ZeroLevel.Network.Microservices;
namespace ZeroLevel.Discovery
{
@ -98,7 +97,8 @@ namespace ZeroLevel.Discovery
{
using (var client = ExchangeTransportFactory.GetClient(protocol, endpoint))
{
return client.Status == Services.Network.ZTransportStatus.Working;
client.ForceConnect();
return client.Status == ZTransportStatus.Working;
}
}
catch (Exception ex)
@ -190,7 +190,7 @@ namespace ZeroLevel.Discovery
Endpoint = serviceInfo.Endpoint,
Protocol = serviceInfo.Protocol
});
Log.SystemInfo($"The service '{serviceInfo.ServiceKey}' registered on protocol {serviceInfo.Protocol}, endpoint: {serviceInfo.Endpoint}");
Log.Info($"The service '{serviceInfo.ServiceKey}' registered on protocol {serviceInfo.Protocol}, endpoint: {serviceInfo.Endpoint}");
}
else
{

@ -0,0 +1,100 @@
using ZeroLevel.Network;
namespace ZeroLevel.Services.Applications
{
public abstract class BaseWindowsExService
: BaseWindowsService, IExchangeService
{
public string Key { get; private set; }
public string Version { get; private set; }
public string Protocol { get; private set; }
public string Group { get; private set; }
public string Type { get; private set; }
protected readonly Exchange _exchange;
protected readonly IConfiguration _config;
protected BaseWindowsExService(IConfiguration configuration = null)
: base()
{
_config = configuration ?? Configuration.Default;
base.Name = ReadName(_config);
this.Key = ReadKey(_config);
this.Version = ReadVersion(_config);
this.Protocol = ReadProtocol(_config);
this.Group = ReadServiceGroup(_config);
this.Type = ReadServiceType(_config);
var discovery = _config.First("discovery");
var discoveryProtocol = _config.FirstOrDefault("discoveryProtocol", "socket");
_exchange = new Exchange(new DiscoveryClient(discoveryProtocol, discovery));
}
#region Config
private string ReadName(IConfiguration configuration)
{
if (_config.Contains("ServiceName"))
return _config.First("ServiceName");
if (_config.Contains("AppName"))
return _config.First("AppName");
return this.GetType().Name;
}
private string ReadKey(IConfiguration configuration)
{
if (_config.Contains("ServiceKey"))
return _config.First("ServiceKey");
if (_config.Contains("AppKey"))
return _config.First("AppKey");
return null;
}
private string ReadVersion(IConfiguration configuration)
{
if (_config.Contains("Version"))
return _config.First("Version");
if (_config.Contains("AppVersion"))
return _config.First("AppVersion");
return "1.0";
}
private string ReadProtocol(IConfiguration configuration)
{
if (_config.Contains("Protocol"))
return _config.First("Protocol");
if (_config.Contains("Transport"))
return _config.First("Transport");
return null;
}
private string ReadServiceGroup(IConfiguration configuration)
{
if (_config.Contains("DiscoveryGroup"))
return _config.First("DiscoveryGroup");
if (_config.Contains("ServiceGroup"))
return _config.First("ServiceGroup");
return ExServiceInfo.DEFAULT_GROUP_NAME;
}
private string ReadServiceType(IConfiguration configuration)
{
if (_config.Contains("DiscoveryType"))
return _config.First("DiscoveryType");
if (_config.Contains("ServiceType"))
return _config.First("ServiceType");
return ExServiceInfo.DEFAULT_TYPE_NAME;
}
#endregion Config
public string Endpoint { get; private set; }
public override void DisposeResources()
{
this._exchange.Dispose();
}
}
}

@ -7,14 +7,14 @@ namespace ZeroLevel.Services.Applications
public abstract class BaseWindowsService
: ServiceBase, IZeroService
{
public string Name { get; }
public string Name { get; protected set; }
public BaseWindowsService()
protected BaseWindowsService()
{
Name = GetType().Name;
}
public BaseWindowsService(string name)
protected BaseWindowsService(string name)
{
Name = name;
}
@ -43,6 +43,8 @@ namespace ZeroLevel.Services.Applications
public abstract void ResumeAction();
public abstract void DisposeResources();
#endregion IZeroService
#region Windows service

@ -7,7 +7,7 @@ using ZeroLevel.Services.Applications;
namespace ZeroLevel
{
public class Bootstrap
public static class Bootstrap
{
static Bootstrap()
{
@ -78,6 +78,7 @@ namespace ZeroLevel
public static void Startup<T>(string[] args, Func<bool> preStartConfiguration = null, Func<bool> postStartConfiguration = null)
where T : IZeroService, new()
{
IZeroService service = null;
var cmd = Configuration.ReadFromCommandLine(args);
if (cmd.Contains("install", "setup"))
{
@ -91,7 +92,6 @@ namespace ZeroLevel
{
Configuration.Save(Configuration.ReadFromApplicationConfig());
Log.CreateLoggingFromConfiguration(Configuration.Default);
IZeroService service = null;
if (preStartConfiguration != null)
{
try
@ -159,9 +159,10 @@ namespace ZeroLevel
}
}
}
try { Sheduller.Dispose(); } catch { }
try { Log.Dispose(); } catch { }
try { Injector.Default.Dispose(); Injector.Dispose(); } catch { }
try { Sheduller.Dispose(); } catch (Exception ex) { Log.Error(ex, "Dispose default sheduller error"); }
try { Log.Dispose(); } catch (Exception ex) { Log.Error(ex, "Dispose log error"); }
try { Injector.Default.Dispose(); Injector.Dispose(); } catch (Exception ex) { Log.Error(ex, "Dispose DI containers error"); }
try { service?.DisposeResources(); } catch (Exception ex) { Log.Error(ex, "Dispose service error"); }
}
}
}

@ -1,4 +1,6 @@
namespace ZeroLevel.Services.Applications
using System;
namespace ZeroLevel.Services.Applications
{
public interface IZeroService
{
@ -13,5 +15,7 @@
void ResumeAction();
void InteractiveStart(string[] args);
void DisposeResources();
}
}

@ -1,4 +1,5 @@
using System;
using ZeroLevel.Services.Serialization;
namespace ZeroLevel.Network
{
@ -6,7 +7,7 @@ namespace ZeroLevel.Network
/// Endpoint
/// </summary>
public class ServiceEndpointInfo :
IEquatable<ServiceEndpointInfo>
IEquatable<ServiceEndpointInfo>, IBinarySerializable
{
public string Endpoint { get; set; }
public string Protocol { get; set; }
@ -28,5 +29,17 @@ namespace ZeroLevel.Network
{
return Endpoint?.GetHashCode() ?? 0 ^ Protocol?.GetHashCode() ?? 0;
}
public void Serialize(IBinaryWriter writer)
{
writer.WriteString(this.Protocol);
writer.WriteString(this.Endpoint);
}
public void Deserialize(IBinaryReader reader)
{
this.Protocol = reader.ReadString();
this.Endpoint = reader.ReadString();
}
}
}

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using ZeroLevel.Services.Serialization;
namespace ZeroLevel.Network
{
@ -7,13 +8,12 @@ namespace ZeroLevel.Network
/// Information about service connection points
/// </summary>
public class ServiceEndpointsInfo :
IEquatable<ServiceEndpointsInfo>
IEquatable<ServiceEndpointsInfo>, IBinarySerializable
{
public string ServiceKey { get; set; }
public string Version { get; set; }
public string ServiceGroup { get; set; }
public string ServiceType { get; set; }
public List<ServiceEndpointInfo> Endpoints { get; set; }
public bool Equals(ServiceEndpointsInfo other)
@ -36,5 +36,23 @@ namespace ZeroLevel.Network
{
return ServiceKey?.GetHashCode() ?? 0 ^ Version?.GetHashCode() ?? 0 ^ ServiceGroup?.GetHashCode() ?? 0 ^ ServiceType?.GetHashCode() ?? 0;
}
public void Serialize(IBinaryWriter writer)
{
writer.WriteString(this.ServiceKey);
writer.WriteString(this.Version);
writer.WriteString(this.ServiceGroup);
writer.WriteString(this.ServiceType);
writer.WriteCollection(this.Endpoints);
}
public void Deserialize(IBinaryReader reader)
{
this.ServiceKey = reader.ReadString();
this.Version = reader.ReadString();
this.ServiceGroup = reader.ReadString();
this.ServiceType = reader.ReadString();
this.Endpoints = reader.ReadCollection<ServiceEndpointInfo>();
}
}
}

@ -57,11 +57,14 @@
<Compile Include="Models\IEntity.cs" />
<Compile Include="Models\InvokeResult.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\Application\BaseWindowsExService.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Services\Application\BaseWindowsService.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Services\Application\BasicServiceInstaller.cs" />
<Compile Include="Services\Application\BusinessApplication.cs" />
<Compile Include="Services\Application\Bootstrap.cs" />
<Compile Include="Services\Application\IZeroService.cs" />
<Compile Include="Services\Application\ZeroServiceState.cs" />
<Compile Include="Services\Async\AsyncConditionVariable.cs" />

@ -1 +1 @@
ee06969e2b1f87052733e2a64f8bf6d27363d162
f165fe7fc045ddedd4b08504422209fd88e56cc6

Loading…
Cancel
Save

Powered by TurnKey Linux.