diff --git a/ZeroLevel.Discovery/Controllers/RoutesController.cs b/ZeroLevel.Discovery/Controllers/RoutesController.cs index 86d0f8c..779fa98 100644 --- a/ZeroLevel.Discovery/Controllers/RoutesController.cs +++ b/ZeroLevel.Discovery/Controllers/RoutesController.cs @@ -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 { diff --git a/ZeroLevel.Discovery/DiscoveryService.cs b/ZeroLevel.Discovery/DiscoveryService.cs index 846fe22..b36e4c9 100644 --- a/ZeroLevel.Discovery/DiscoveryService.cs +++ b/ZeroLevel.Discovery/DiscoveryService.cs @@ -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() { } diff --git a/ZeroLevel.Discovery/RouteTable.cs b/ZeroLevel.Discovery/RouteTable.cs index 3426f93..d1e00b9 100644 --- a/ZeroLevel.Discovery/RouteTable.cs +++ b/ZeroLevel.Discovery/RouteTable.cs @@ -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 { diff --git a/ZeroLevel.Discovery/obj/Debug/ZeroLevel.Discovery.csprojAssemblyReference.cache b/ZeroLevel.Discovery/obj/Debug/ZeroLevel.Discovery.csprojAssemblyReference.cache index 7e437ec..34790e8 100644 Binary files a/ZeroLevel.Discovery/obj/Debug/ZeroLevel.Discovery.csprojAssemblyReference.cache and b/ZeroLevel.Discovery/obj/Debug/ZeroLevel.Discovery.csprojAssemblyReference.cache differ diff --git a/ZeroLevel/Services/Application/BaseWindowsExService.cs b/ZeroLevel/Services/Application/BaseWindowsExService.cs new file mode 100644 index 0000000..65aeeae --- /dev/null +++ b/ZeroLevel/Services/Application/BaseWindowsExService.cs @@ -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(); + } + } +} \ No newline at end of file diff --git a/ZeroLevel/Services/Application/BaseWindowsService.cs b/ZeroLevel/Services/Application/BaseWindowsService.cs index 5419274..20a7936 100644 --- a/ZeroLevel/Services/Application/BaseWindowsService.cs +++ b/ZeroLevel/Services/Application/BaseWindowsService.cs @@ -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 diff --git a/ZeroLevel/Services/Application/BusinessApplication.cs b/ZeroLevel/Services/Application/Bootstrap.cs similarity index 92% rename from ZeroLevel/Services/Application/BusinessApplication.cs rename to ZeroLevel/Services/Application/Bootstrap.cs index b9e4203..166393e 100644 --- a/ZeroLevel/Services/Application/BusinessApplication.cs +++ b/ZeroLevel/Services/Application/Bootstrap.cs @@ -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(string[] args, Func preStartConfiguration = null, Func postStartConfiguration = null) where T : IZeroService, new() { + IZeroService service = null; var cmd = Configuration.ReadFromCommandLine(args); if (cmd.Contains("install", "setup")) { @@ -90,8 +91,7 @@ namespace ZeroLevel else { Configuration.Save(Configuration.ReadFromApplicationConfig()); - Log.CreateLoggingFromConfiguration(Configuration.Default); - IZeroService service = null; + Log.CreateLoggingFromConfiguration(Configuration.Default); 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"); } } } } \ No newline at end of file diff --git a/ZeroLevel/Services/Application/IZeroService.cs b/ZeroLevel/Services/Application/IZeroService.cs index 33208c1..2b077e3 100644 --- a/ZeroLevel/Services/Application/IZeroService.cs +++ b/ZeroLevel/Services/Application/IZeroService.cs @@ -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(); } } \ No newline at end of file diff --git a/ZeroLevel/Services/Network/Models/ServiceEndpointInfo.cs b/ZeroLevel/Services/Network/Models/ServiceEndpointInfo.cs index 6983b56..7efb5e5 100644 --- a/ZeroLevel/Services/Network/Models/ServiceEndpointInfo.cs +++ b/ZeroLevel/Services/Network/Models/ServiceEndpointInfo.cs @@ -1,4 +1,5 @@ using System; +using ZeroLevel.Services.Serialization; namespace ZeroLevel.Network { @@ -6,7 +7,7 @@ namespace ZeroLevel.Network /// Endpoint /// public class ServiceEndpointInfo : - IEquatable + IEquatable, 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(); + } } } \ No newline at end of file diff --git a/ZeroLevel/Services/Network/Models/ServiceEndpointsInfo.cs b/ZeroLevel/Services/Network/Models/ServiceEndpointsInfo.cs index 191e57a..8ca4ea6 100644 --- a/ZeroLevel/Services/Network/Models/ServiceEndpointsInfo.cs +++ b/ZeroLevel/Services/Network/Models/ServiceEndpointsInfo.cs @@ -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 /// public class ServiceEndpointsInfo : - IEquatable + IEquatable, IBinarySerializable { public string ServiceKey { get; set; } public string Version { get; set; } public string ServiceGroup { get; set; } public string ServiceType { get; set; } - public List 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(); + } } } \ No newline at end of file diff --git a/ZeroLevel/ZeroLevel.csproj b/ZeroLevel/ZeroLevel.csproj index 9464011..85aee82 100644 --- a/ZeroLevel/ZeroLevel.csproj +++ b/ZeroLevel/ZeroLevel.csproj @@ -57,11 +57,14 @@ + + Component + Component - + diff --git a/ZeroLevel/obj/Debug/ZeroLevel.csproj.CoreCompileInputs.cache b/ZeroLevel/obj/Debug/ZeroLevel.csproj.CoreCompileInputs.cache index 7af6f21..91b6aaf 100644 --- a/ZeroLevel/obj/Debug/ZeroLevel.csproj.CoreCompileInputs.cache +++ b/ZeroLevel/obj/Debug/ZeroLevel.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -ee06969e2b1f87052733e2a64f8bf6d27363d162 +f165fe7fc045ddedd4b08504422209fd88e56cc6