From ea4b1599d6447e39eed687a357cf80b6b5d8b3da Mon Sep 17 00:00:00 2001 From: rogeralsing Date: Sun, 18 Oct 2015 12:51:27 +0200 Subject: [PATCH 1/7] Refactoring --- Microphone.Core/Cluster.cs | 55 +++++++++++++++++++ Microphone.Core/Configuration.cs | 27 +++++++++ Microphone.Core/Logger.cs | 11 ++-- Microphone.Core/Microphone.Core.csproj | 10 ++++ Microphone.Core/packages.config | 2 + Microphone.Nancy/AutoRegisterModule.cs | 42 ++------------ Microphone.Nancy/Bootstrap.cs | 43 +-------------- Microphone.Nancy/Microphone.Nancy.csproj | 4 -- Microphone.Nancy/packages.config | 1 - .../AutoRegisterApiController.cs | 31 +---------- Microphone.WebApi/Bootstrap.cs | 51 ++--------------- Microphone.WebApi/Microphone.WebApi.csproj | 4 -- Microphone.WebApi/packages.config | 1 - Service1/Program.cs | 10 +--- Service2/Program.cs | 3 +- 15 files changed, 115 insertions(+), 180 deletions(-) create mode 100644 Microphone.Core/Cluster.cs create mode 100644 Microphone.Core/Configuration.cs diff --git a/Microphone.Core/Cluster.cs b/Microphone.Core/Cluster.cs new file mode 100644 index 0000000..70f0ac9 --- /dev/null +++ b/Microphone.Core/Cluster.cs @@ -0,0 +1,55 @@ +using System; +using System.Linq; +using System.Text; +using Consul; + +namespace Microphone.Core +{ + public static class Cluster + { + private static string ServiceName; + private static string ServiceId; + + public static string GetConfig() + { + var client = new Client(); + var key = "ServiceConfig:" + ServiceName; + var response = client.KV.Get(key); + var res = Encoding.UTF8.GetString(response.Response.Value); + return res; + } + + public static ServiceInformation[] FindService(string name) + { + Logger.Information("{ServiceName} lookup {OtherServiceName}", ServiceName, name); + var client = new Client(); + var others = client.Health.Service(name, null, true); + + return + others.Response.Select(other => new ServiceInformation(other.Service.Address, other.Service.Port)) + .ToArray(); + } + + public static void RegisterService(string serviceName, string serviceId, string version, Uri uri) + { + ServiceName = serviceName; + ServiceId = serviceId; + var client = new Client(); + client.Agent.ServiceRegister(new AgentServiceRegistration + { + Address = uri.Host, + ID = serviceId, + Name = serviceName, + Port = uri.Port, + Tags = new[] {version}, + Check = new AgentServiceCheck + { + HTTP = uri + "status", + Interval = TimeSpan.FromSeconds(1), + TTL = TimeSpan.Zero, + Timeout = TimeSpan.Zero + } + }); + } + } +} \ No newline at end of file diff --git a/Microphone.Core/Configuration.cs b/Microphone.Core/Configuration.cs new file mode 100644 index 0000000..bce50cb --- /dev/null +++ b/Microphone.Core/Configuration.cs @@ -0,0 +1,27 @@ +using System; +using System.Net; +using System.Net.Sockets; + +namespace Microphone.Core +{ + public static class Configuration + { + + + public static Uri GetUri(int port = 0) + { + port = port == 0 ? FreeTcpPort() : port; + var uri = new Uri("http://localhost:" + port); + return uri; + } + + private static int FreeTcpPort() + { + var l = new TcpListener(IPAddress.Loopback, 0); + l.Start(); + var port = ((IPEndPoint)l.LocalEndpoint).Port; + l.Stop(); + return port; + } + } +} diff --git a/Microphone.Core/Logger.cs b/Microphone.Core/Logger.cs index d76dd45..2c98058 100644 --- a/Microphone.Core/Logger.cs +++ b/Microphone.Core/Logger.cs @@ -3,7 +3,7 @@ namespace Microphone.Core { - public class Logger + public static class Logger { private static readonly ILogger _logger; @@ -12,23 +12,22 @@ static Logger() _logger = new LoggerConfiguration().WriteTo.ColoredConsole(LogEventLevel.Debug).CreateLogger(); } - - public void Debug(string template, params object[] args) + public static void Debug(string template, params object[] args) { _logger.Debug(template,args); } - public void Information(string template, params object[] args) + public static void Information(string template, params object[] args) { _logger.Information(template, args); } - public void Error(string template, params object[] args) + public static void Error(string template, params object[] args) { _logger.Error(template, args); } - public void Warning(string template, params object[] args) + public static void Warning(string template, params object[] args) { _logger.Warning(template, args); } diff --git a/Microphone.Core/Microphone.Core.csproj b/Microphone.Core/Microphone.Core.csproj index e5e8e3c..d1c4f83 100644 --- a/Microphone.Core/Microphone.Core.csproj +++ b/Microphone.Core/Microphone.Core.csproj @@ -30,6 +30,14 @@ 4 + + ..\packages\Consul.0.5.2.65\lib\net45\Consul.dll + True + + + ..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll + True + ..\packages\Serilog.1.5.11\lib\net45\Serilog.dll True @@ -48,6 +56,8 @@ + + diff --git a/Microphone.Core/packages.config b/Microphone.Core/packages.config index 104b801..4a09a8b 100644 --- a/Microphone.Core/packages.config +++ b/Microphone.Core/packages.config @@ -1,4 +1,6 @@  + + \ No newline at end of file diff --git a/Microphone.Nancy/AutoRegisterModule.cs b/Microphone.Nancy/AutoRegisterModule.cs index ad7b789..5fe07df 100644 --- a/Microphone.Nancy/AutoRegisterModule.cs +++ b/Microphone.Nancy/AutoRegisterModule.cs @@ -1,51 +1,17 @@ -using System.Collections.Generic; -using System.Linq; -using Consul; -using Microphone.Core; +using Microphone.Core; using Nancy; namespace Microphone.Nancy { - public abstract class AutoRegisterModule : NancyModule + public class StatusModule : NancyModule { - private readonly Logger _logger; - - protected Logger Logger => _logger; - - protected AutoRegisterModule() + protected StatusModule() { - _logger = new Logger(); - //Before += ctx => - //{ - // _logger.Information("{Url} - {Method}", ctx.Request.Url , ctx.Request.Method ); - // return null; - //}; - Get["/status"] = _ => { Logger.Information("OK"); return "ok"; }; - } - - protected string GetConfig() - { - var client = new Client(); - var key = "ServiceConfig:" + Bootstrap.ServiceName; - var response = client.KV.Get(key); - var res = System.Text.Encoding.UTF8.GetString(response.Response.Value); - return res; - } - - protected ServiceInformation[] FindService(string name) - { - Logger.Information("{ServiceName} lookup {OtherServiceName}",Bootstrap.ServiceName,name); - var client = new Client(); - var others = client.Catalog.Service(name); - - return - others.Response.Select(other => new ServiceInformation(other.ServiceAddress, other.ServicePort)) - .ToArray(); - } + } } } diff --git a/Microphone.Nancy/Bootstrap.cs b/Microphone.Nancy/Bootstrap.cs index 484c74c..44ad4d4 100644 --- a/Microphone.Nancy/Bootstrap.cs +++ b/Microphone.Nancy/Bootstrap.cs @@ -1,8 +1,6 @@ using System; -using System.Net; -using System.Net.Sockets; using System.Threading.Tasks; -using Consul; +using Microphone.Core; using Nancy; using Nancy.Hosting.Self; @@ -10,36 +8,17 @@ namespace Microphone.Nancy { public class Bootstrap { - public static string ServiceName { get; private set; } - public static string Version { get; private set; } public static void Start(string serviceName, string version) { - ServiceName = serviceName; - Version = version; var serviceId = serviceName + Guid.NewGuid(); - var uri = GetUri(); + var uri = Configuration.GetUri(); var conf = GetConfiguration(); var host = GetHost(uri, conf); Console.WriteLine("{0} running on {1}", serviceId, uri); - var client = new Client(); - client.Agent.ServiceRegister(new AgentServiceRegistration - { - Address = uri.Host, - ID = serviceId, - Name = serviceName, - Port = uri.Port, - Tags = new[] {version}, - Check = new AgentServiceCheck - { - HTTP = uri + "status", - Interval = TimeSpan.FromSeconds(1), - TTL = TimeSpan.Zero, - Timeout = TimeSpan.Zero - } - }); + Cluster.RegisterService(serviceName, serviceId, version, uri); } private static NancyHost GetHost(Uri uri, HostConfiguration hostConfigs) @@ -71,21 +50,5 @@ private static HostConfiguration GetConfiguration() }; return hostConfigs; } - - private static Uri GetUri(int port = 0) - { - port = port == 0 ? FreeTcpPort() : port; - var uri = new Uri("http://localhost:" + port); - return uri; - } - - private static int FreeTcpPort() - { - var l = new TcpListener(IPAddress.Loopback, 0); - l.Start(); - var port = ((IPEndPoint) l.LocalEndpoint).Port; - l.Stop(); - return port; - } } } \ No newline at end of file diff --git a/Microphone.Nancy/Microphone.Nancy.csproj b/Microphone.Nancy/Microphone.Nancy.csproj index 41bb279..b1d6402 100644 --- a/Microphone.Nancy/Microphone.Nancy.csproj +++ b/Microphone.Nancy/Microphone.Nancy.csproj @@ -30,10 +30,6 @@ 4 - - ..\packages\Consul.0.5.2.65\lib\net45\Consul.dll - True - ..\packages\Nancy.1.3.0\lib\net40\Nancy.dll True diff --git a/Microphone.Nancy/packages.config b/Microphone.Nancy/packages.config index 4ebe9b6..908e549 100644 --- a/Microphone.Nancy/packages.config +++ b/Microphone.Nancy/packages.config @@ -1,6 +1,5 @@  - diff --git a/Microphone.WebApi/AutoRegisterApiController.cs b/Microphone.WebApi/AutoRegisterApiController.cs index 67ce8e3..460ecb6 100644 --- a/Microphone.WebApi/AutoRegisterApiController.cs +++ b/Microphone.WebApi/AutoRegisterApiController.cs @@ -1,39 +1,10 @@ -using System.Collections.Generic; -using System.Linq; -using System.Web.Http; -using Consul; +using System.Web.Http; using Microphone.Core; namespace Microphone.WebApi { - public abstract class AutoRegisterApiController : ApiController - { - protected Logger Logger { get; } = new Logger(); - - protected string GetConfig() - { - var client = new Client(); - var key = "ServiceConfig:" + Bootstrap.ServiceName; - var response = client.KV.Get(key); - var res = System.Text.Encoding.UTF8.GetString(response.Response.Value); - return res; - } - - protected ServiceInformation[] FindService(string name) - { - Logger.Information("{ServiceName} lookup {OtherServiceName}", Bootstrap.ServiceName, name); - var client = new Client(); - var others = client.Catalog.Service(name); - - return - others.Response.Select(other => new ServiceInformation(other.ServiceAddress, other.ServicePort)) - .ToArray(); - } - } - public class StatusController : ApiController { - private Logger Logger { get; } = new Logger(); [Route("status")] public string GetStatus() { diff --git a/Microphone.WebApi/Bootstrap.cs b/Microphone.WebApi/Bootstrap.cs index 6f04bfc..4c98e1c 100644 --- a/Microphone.WebApi/Bootstrap.cs +++ b/Microphone.WebApi/Bootstrap.cs @@ -3,70 +3,29 @@ using System.Net.Sockets; using System.Web.Http; using System.Web.Http.SelfHost; -using Consul; +using Microphone.Core; namespace Microphone.WebApi { public static class Bootstrap { - public static string ServiceName { get; private set; } - public static string Version { get; private set; } - public static void Start(string serviceName, string version) { - ServiceName = serviceName; - Version = version; var serviceId = serviceName + Guid.NewGuid(); - var uri = GetUri(); + var uri = Configuration.GetUri(); var config = new HttpSelfHostConfiguration(uri); Console.WriteLine("{0} running on {1}", serviceId, uri); - var client = new Client(); - client.Agent.ServiceRegister(new AgentServiceRegistration - { - Address = uri.Host, - ID = serviceId, - Name = serviceName, - Port = uri.Port, - Tags = new[] { version }, - Check = new AgentServiceCheck - { - HTTP = uri + "status", - Interval = TimeSpan.FromSeconds(1), - TTL = TimeSpan.Zero, - Timeout = TimeSpan.Zero - } - }); + Cluster.RegisterService(serviceName,serviceId,version,uri); config.Routes.MapHttpRoute( "API Default", "{controller}/{id}", new { id = RouteParameter.Optional }); - using (HttpSelfHostServer server = new HttpSelfHostServer(config)) - { - server.OpenAsync().Wait(); - Console.WriteLine("Press Enter to quit."); - Console.ReadLine(); - } - } - - - private static Uri GetUri(int port = 0) - { - port = port == 0 ? FreeTcpPort() : port; - var uri = new Uri("http://localhost:" + port); - return uri; - } - - private static int FreeTcpPort() - { - var l = new TcpListener(IPAddress.Loopback, 0); - l.Start(); - var port = ((IPEndPoint)l.LocalEndpoint).Port; - l.Stop(); - return port; + var server = new HttpSelfHostServer(config); + server.OpenAsync().Wait(); } } } diff --git a/Microphone.WebApi/Microphone.WebApi.csproj b/Microphone.WebApi/Microphone.WebApi.csproj index c4a7ed1..17402bf 100644 --- a/Microphone.WebApi/Microphone.WebApi.csproj +++ b/Microphone.WebApi/Microphone.WebApi.csproj @@ -30,10 +30,6 @@ 4 - - ..\packages\Consul.0.5.2.65\lib\net45\Consul.dll - True - ..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll True diff --git a/Microphone.WebApi/packages.config b/Microphone.WebApi/packages.config index 90b3801..4b54721 100644 --- a/Microphone.WebApi/packages.config +++ b/Microphone.WebApi/packages.config @@ -1,6 +1,5 @@  - diff --git a/Service1/Program.cs b/Service1/Program.cs index 3eefbee..3e32a85 100644 --- a/Service1/Program.cs +++ b/Service1/Program.cs @@ -1,7 +1,6 @@ using System; -using System.Linq; -using System.Management.Instrumentation; using Microphone.Nancy; +using Nancy; namespace Service1 { @@ -14,17 +13,12 @@ private static void Main(string[] args) } } - public class MyService : AutoRegisterModule + public class MyService : NancyModule { public MyService() { Get["/"] = _ => { - //var instances = FindService("Service2"); - //var instance = instances.First(); //or use random index for load balancing - - //MakeSomeCall("/api/orders",instance.ServiceAddress, instance.ServicePort); - return "Hello"; }; } diff --git a/Service2/Program.cs b/Service2/Program.cs index d9d258d..91de91e 100644 --- a/Service2/Program.cs +++ b/Service2/Program.cs @@ -12,9 +12,8 @@ static void Main(string[] args) Console.ReadLine(); } } - - public class DefaultController : AutoRegisterApiController + public class DefaultController : ApiController { public string Get() { From 2ab899e1e14d052703c4c1ab27dc773524f5068c Mon Sep 17 00:00:00 2001 From: rogeralsing Date: Sun, 18 Oct 2015 12:52:25 +0200 Subject: [PATCH 2/7] Dev --- Service1/Program.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Service1/Program.cs b/Service1/Program.cs index 3e32a85..e466fc7 100644 --- a/Service1/Program.cs +++ b/Service1/Program.cs @@ -1,4 +1,5 @@ using System; +using Microphone.Core; using Microphone.Nancy; using Nancy; @@ -18,7 +19,7 @@ public class MyService : NancyModule public MyService() { Get["/"] = _ => - { + { return "Hello"; }; } From a2632a3eb7893e37e9f620ca30ff7621a6311278 Mon Sep 17 00:00:00 2001 From: rogeralsing Date: Sun, 18 Oct 2015 13:01:09 +0200 Subject: [PATCH 3/7] Fixed Nancy issue --- Microphone.Nancy/AutoRegisterModule.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Microphone.Nancy/AutoRegisterModule.cs b/Microphone.Nancy/AutoRegisterModule.cs index 5fe07df..7083d1a 100644 --- a/Microphone.Nancy/AutoRegisterModule.cs +++ b/Microphone.Nancy/AutoRegisterModule.cs @@ -5,7 +5,7 @@ namespace Microphone.Nancy { public class StatusModule : NancyModule { - protected StatusModule() + public StatusModule() { Get["/status"] = _ => { From 9356fa13a7d2f31c0b3e4d6124920b2b48589f12 Mon Sep 17 00:00:00 2001 From: rogeralsing Date: Sun, 18 Oct 2015 13:15:47 +0200 Subject: [PATCH 4/7] Built in reapers --- Microphone.Core/Cluster.cs | 45 ++++++++++++ Microphone.Core/Logger.cs | 6 ++ Microphone.sln | 7 -- ServiceCleanup/App.config | 6 -- ServiceCleanup/Program.cs | 90 ----------------------- ServiceCleanup/Properties/AssemblyInfo.cs | 36 --------- ServiceCleanup/ServiceCleanup.csproj | 69 ----------------- ServiceCleanup/packages.config | 5 -- 8 files changed, 51 insertions(+), 213 deletions(-) delete mode 100644 ServiceCleanup/App.config delete mode 100644 ServiceCleanup/Program.cs delete mode 100644 ServiceCleanup/Properties/AssemblyInfo.cs delete mode 100644 ServiceCleanup/ServiceCleanup.csproj delete mode 100644 ServiceCleanup/packages.config diff --git a/Microphone.Core/Cluster.cs b/Microphone.Core/Cluster.cs index 70f0ac9..5ed0e3c 100644 --- a/Microphone.Core/Cluster.cs +++ b/Microphone.Core/Cluster.cs @@ -1,6 +1,8 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading.Tasks; using Consul; namespace Microphone.Core @@ -50,6 +52,49 @@ public static void RegisterService(string serviceName, string serviceId, string Timeout = TimeSpan.Zero } }); + StartReaper(); + } + + private static void StartReaper() + { + Task.Factory.StartNew(async () => + { + await Task.Delay(1000).ConfigureAwait(false); + Logger.Information("Reaper started.."); + var client = new Client(); + var lookup = new HashSet(); + while (true) + { + try + { + var checks = client.Agent.Checks(); + foreach (var check in checks.Response) + { + if (Equals(check.Value.Status, CheckStatus.Critical)) + { + //dont delete new services + if (lookup.Contains(check.Value.ServiceID)) + { + client.Agent.ServiceDeregister(check.Value.ServiceID); + Logger.Information("Unregistering {ServiceId}", check.Value.ServiceID); + } + else + { + Logger.Information("Marking {ServiceId} as up for reaping", check.Value.ServiceID); + lookup.Add(check.Value.ServiceID); + } + + } + } + } + catch(Exception x) + { + Logger.Error(x,"Crashed"); + } + + await Task.Delay(5000).ConfigureAwait(false); + } + }); } } } \ No newline at end of file diff --git a/Microphone.Core/Logger.cs b/Microphone.Core/Logger.cs index 2c98058..862ea72 100644 --- a/Microphone.Core/Logger.cs +++ b/Microphone.Core/Logger.cs @@ -1,3 +1,4 @@ +using System; using Serilog; using Serilog.Events; @@ -27,6 +28,11 @@ public static void Error(string template, params object[] args) _logger.Error(template, args); } + public static void Error(Exception cause, string template, params object[] args) + { + _logger.Error(cause,template, args); + } + public static void Warning(string template, params object[] args) { _logger.Warning(template, args); diff --git a/Microphone.sln b/Microphone.sln index 3410e39..e11d05f 100644 --- a/Microphone.sln +++ b/Microphone.sln @@ -3,8 +3,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14 VisualStudioVersion = 14.0.24606.1 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ServiceCleanup", "ServiceCleanup\ServiceCleanup.csproj", "{002E03ED-098F-4CEF-B575-2FE143119F29}" -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Lib", "Lib", "{8A71EDB4-5188-4605-8C5D-C78860B8D28B}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Service1", "Service1\Service1.csproj", "{B50B2E8E-E92A-49D4-A4C8-BA09AC89C4A6}" @@ -25,10 +23,6 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {002E03ED-098F-4CEF-B575-2FE143119F29}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {002E03ED-098F-4CEF-B575-2FE143119F29}.Debug|Any CPU.Build.0 = Debug|Any CPU - {002E03ED-098F-4CEF-B575-2FE143119F29}.Release|Any CPU.ActiveCfg = Release|Any CPU - {002E03ED-098F-4CEF-B575-2FE143119F29}.Release|Any CPU.Build.0 = Release|Any CPU {B50B2E8E-E92A-49D4-A4C8-BA09AC89C4A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {B50B2E8E-E92A-49D4-A4C8-BA09AC89C4A6}.Debug|Any CPU.Build.0 = Debug|Any CPU {B50B2E8E-E92A-49D4-A4C8-BA09AC89C4A6}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -54,7 +48,6 @@ Global HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution - {002E03ED-098F-4CEF-B575-2FE143119F29} = {8A71EDB4-5188-4605-8C5D-C78860B8D28B} {B50B2E8E-E92A-49D4-A4C8-BA09AC89C4A6} = {CCC5CCD5-8319-4C68-B867-689DCA530008} {C937DCE3-19BF-459C-A14F-348AAED672C7} = {CCC5CCD5-8319-4C68-B867-689DCA530008} {C630E7CE-C207-46FD-9C75-FA6FB27353D2} = {8A71EDB4-5188-4605-8C5D-C78860B8D28B} diff --git a/ServiceCleanup/App.config b/ServiceCleanup/App.config deleted file mode 100644 index 8324aa6..0000000 --- a/ServiceCleanup/App.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/ServiceCleanup/Program.cs b/ServiceCleanup/Program.cs deleted file mode 100644 index 7bc876a..0000000 --- a/ServiceCleanup/Program.cs +++ /dev/null @@ -1,90 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using Consul; - -namespace ServiceCleanup -{ - class Program - { - static void Main(string[] args) - { - var client = new Client(); - var lookup = new HashSet(); - while (true) - { - var checks = client.Agent.Checks(); - foreach (var check in checks.Response) - { - if (Equals(check.Value.Status, CheckStatus.Critical)) - { - //dont delete new services - if (lookup.Contains(check.Value.ServiceID)) - { - client.Agent.ServiceDeregister(check.Value.ServiceID); - Console.WriteLine("Unregistering service {0}", check.Value.ServiceID); - } - else - { - lookup.Add(check.Value.ServiceID); - } - - } - } - - Task.Delay(1000).Wait(); - } - - //var client = new Client(); - //var allServices = client.Catalog.Services(); - //foreach (var service in allServices.Response.Where(s => s.Key != "consul")) - //{ - - // var health = client.Health.Service(service.Key); - - // foreach (var serviceInstance in health.Response) - // { - - // client.Agent.ServiceDeregister(serviceInstance.Service.ID); - // } - //} - } - } -} - - -//using System; -//using System.Collections.Generic; -//using System.Linq; -//using System.Text; -//using System.Threading.Tasks; -//using Consul; - -//namespace ServiceCleanup -//{ -// class Program -// { -// static void Main(string[] args) -// { -// var client = new Client(); -// while (true) -// { -// var allServices = client.Catalog.Services(); -// foreach (var service in allServices.Response.Where(s => s.Key != "consul")) -// { -// var checks = client.Agent.Checks(); -// foreach (var check in checks.Response) -// { -// if (Equals(check.Value.Status, CheckStatus.Critical)) -// { -// client.Agent.ServiceDeregister(service.Key); -// client.Agent.CheckDeregister(check.Key); -// Console.WriteLine("Unregistering service {0}", service.Key); -// } -// } -// } -// Task.Delay(1000).Wait(); -// } -// } -// } -//} diff --git a/ServiceCleanup/Properties/AssemblyInfo.cs b/ServiceCleanup/Properties/AssemblyInfo.cs deleted file mode 100644 index 216345a..0000000 --- a/ServiceCleanup/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Reflection; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. -[assembly: AssemblyTitle("ServiceCleanup")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("ServiceCleanup")] -[assembly: AssemblyCopyright("Copyright © 2015")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("002e03ed-098f-4cef-b575-2fe143119f29")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ServiceCleanup/ServiceCleanup.csproj b/ServiceCleanup/ServiceCleanup.csproj deleted file mode 100644 index 5a489ea..0000000 --- a/ServiceCleanup/ServiceCleanup.csproj +++ /dev/null @@ -1,69 +0,0 @@ - - - - - Debug - AnyCPU - {002E03ED-098F-4CEF-B575-2FE143119F29} - Exe - Properties - ServiceCleanup - ServiceCleanup - v4.6 - 512 - true - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - ..\packages\Consul.0.5.2.65\lib\net45\Consul.dll - True - - - ..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll - True - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/ServiceCleanup/packages.config b/ServiceCleanup/packages.config deleted file mode 100644 index e89e838..0000000 --- a/ServiceCleanup/packages.config +++ /dev/null @@ -1,5 +0,0 @@ - - - - - \ No newline at end of file From e78ec80941c1ad7535af84f8f31636cb57182e4a Mon Sep 17 00:00:00 2001 From: rogeralsing Date: Sun, 18 Oct 2015 13:20:58 +0200 Subject: [PATCH 5/7] Autoreaper --- Microphone.Core/Cluster.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Microphone.Core/Cluster.cs b/Microphone.Core/Cluster.cs index 5ed0e3c..4ae1f73 100644 --- a/Microphone.Core/Cluster.cs +++ b/Microphone.Core/Cluster.cs @@ -60,7 +60,7 @@ private static void StartReaper() Task.Factory.StartNew(async () => { await Task.Delay(1000).ConfigureAwait(false); - Logger.Information("Reaper started.."); + Logger.Information("Reaper: started.."); var client = new Client(); var lookup = new HashSet(); while (true) @@ -76,15 +76,20 @@ private static void StartReaper() if (lookup.Contains(check.Value.ServiceID)) { client.Agent.ServiceDeregister(check.Value.ServiceID); - Logger.Information("Unregistering {ServiceId}", check.Value.ServiceID); + Logger.Information("Reaper: Removing {ServiceId}", check.Value.ServiceID); } else { - Logger.Information("Marking {ServiceId} as up for reaping", check.Value.ServiceID); + Logger.Information("Reaper: Marking {ServiceId}", check.Value.ServiceID); lookup.Add(check.Value.ServiceID); } } + else + { + //if service is ok, remove it from reaper set + lookup.Remove(check.Value.ServiceID); + } } } catch(Exception x) From c8d55d831c0ee31bc31694e3ae88812f921bf2a4 Mon Sep 17 00:00:00 2001 From: rogeralsing Date: Sun, 18 Oct 2015 15:32:24 +0200 Subject: [PATCH 6/7] fix --- Microphone.Core/Properties/AssemblyInfo.cs | 4 ++-- Microphone.Core/build.bat | 3 ++- Microphone.Nancy/Properties/AssemblyInfo.cs | 6 +++--- Microphone.Nancy/build.bat | 2 +- Microphone.WebApi/Properties/AssemblyInfo.cs | 6 +++--- Microphone.WebApi/build.bat | 2 +- Service1/Program.cs | 3 ++- 7 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Microphone.Core/Properties/AssemblyInfo.cs b/Microphone.Core/Properties/AssemblyInfo.cs index de5d1e8..fc63403 100644 --- a/Microphone.Core/Properties/AssemblyInfo.cs +++ b/Microphone.Core/Properties/AssemblyInfo.cs @@ -33,5 +33,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.1.0.0")] -[assembly: AssemblyFileVersion("0.1.0.0")] +[assembly: AssemblyVersion("0.1.2.0")] +[assembly: AssemblyFileVersion("0.1.2.0")] diff --git a/Microphone.Core/build.bat b/Microphone.Core/build.bat index d2f32ca..16ff470 100644 --- a/Microphone.Core/build.bat +++ b/Microphone.Core/build.bat @@ -1,2 +1,3 @@ +del *.nupkg nuget pack Microphone.Core.csproj -IncludeReferencedProjects -Prop Configuration=Release -nuget push Microphone.Core.0.1.0.0.nupkg +nuget push Microphone.Core.0.1.2.0.nupkg diff --git a/Microphone.Nancy/Properties/AssemblyInfo.cs b/Microphone.Nancy/Properties/AssemblyInfo.cs index 2348e14..92b5e29 100644 --- a/Microphone.Nancy/Properties/AssemblyInfo.cs +++ b/Microphone.Nancy/Properties/AssemblyInfo.cs @@ -6,7 +6,7 @@ // set of attributes. Change these attribute values to modify the information // associated with an assembly. [assembly: AssemblyTitle("Microphone.Nancy")] -[assembly: AssemblyDescription("Microservice framework using NancyFx and Consul")] +[assembly: AssemblyDescription("Microservice framework with powerful service discovery using Consul")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("Roger Johansson")] [assembly: AssemblyProduct("Microphone.Nancy")] @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.1.1.0")] -[assembly: AssemblyFileVersion("0.1.1.0")] +[assembly: AssemblyVersion("0.1.2.0")] +[assembly: AssemblyFileVersion("0.1.2.0")] diff --git a/Microphone.Nancy/build.bat b/Microphone.Nancy/build.bat index e2dd2e4..b1d0cb8 100644 --- a/Microphone.Nancy/build.bat +++ b/Microphone.Nancy/build.bat @@ -1,3 +1,3 @@ del *.nupkg nuget pack Microphone.Nancy.csproj -IncludeReferencedProjects -Prop Configuration=Release -nuget push Microphone.Nancy.0.1.1.0.nupkg +nuget push Microphone.Nancy.0.1.2.0.nupkg diff --git a/Microphone.WebApi/Properties/AssemblyInfo.cs b/Microphone.WebApi/Properties/AssemblyInfo.cs index 6eb7a25..bbc98dc 100644 --- a/Microphone.WebApi/Properties/AssemblyInfo.cs +++ b/Microphone.WebApi/Properties/AssemblyInfo.cs @@ -6,7 +6,7 @@ // set of attributes. Change these attribute values to modify the information // associated with an assembly. [assembly: AssemblyTitle("Microphone.WebApi")] -[assembly: AssemblyDescription("Microservice framework using WebApi and Consul")] +[assembly: AssemblyDescription("Microservice framework with powerful service discovery using Consul")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("Roger Johansson")] [assembly: AssemblyProduct("Microphone.WebApi")] @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.1.1.0")] -[assembly: AssemblyFileVersion("0.1.1.0")] +[assembly: AssemblyVersion("0.1.2.0")] +[assembly: AssemblyFileVersion("0.1.2.0")] diff --git a/Microphone.WebApi/build.bat b/Microphone.WebApi/build.bat index afe036f..19078b3 100644 --- a/Microphone.WebApi/build.bat +++ b/Microphone.WebApi/build.bat @@ -1,3 +1,3 @@ del *.nupkg nuget pack Microphone.WebApi.csproj -IncludeReferencedProjects -Prop Configuration=Release -nuget push Microphone.WebApi.0.1.1.0.nupkg +nuget push Microphone.WebApi.0.1.2.0.nupkg diff --git a/Service1/Program.cs b/Service1/Program.cs index e466fc7..45a150d 100644 --- a/Service1/Program.cs +++ b/Service1/Program.cs @@ -19,7 +19,8 @@ public class MyService : NancyModule public MyService() { Get["/"] = _ => - { + { + var res = Cluster.FindService("Service2"); return "Hello"; }; } From b6264d1c1b82a000b73914176f2ed251ffcf8a50 Mon Sep 17 00:00:00 2001 From: rogeralsing Date: Sun, 18 Oct 2015 15:36:30 +0200 Subject: [PATCH 7/7] 1.0.3 --- Microphone.Core/Microphone.Core.csproj | 1 + Microphone.Core/Properties/AssemblyInfo.cs | 4 ++-- Microphone.Core/build.bat | 2 +- Microphone.Nancy/Microphone.Nancy.csproj | 1 + Microphone.Nancy/Microphone.Nancy.nuspec | 2 +- Microphone.Nancy/Properties/AssemblyInfo.cs | 4 ++-- Microphone.Nancy/build.bat | 2 +- Microphone.WebApi/Microphone.WebApi.csproj | 1 + Microphone.WebApi/Microphone.WebApi.nuspec | 2 +- Microphone.WebApi/Properties/AssemblyInfo.cs | 4 ++-- Microphone.WebApi/build.bat | 2 +- 11 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Microphone.Core/Microphone.Core.csproj b/Microphone.Core/Microphone.Core.csproj index d1c4f83..404219a 100644 --- a/Microphone.Core/Microphone.Core.csproj +++ b/Microphone.Core/Microphone.Core.csproj @@ -63,6 +63,7 @@ + diff --git a/Microphone.Core/Properties/AssemblyInfo.cs b/Microphone.Core/Properties/AssemblyInfo.cs index fc63403..985d530 100644 --- a/Microphone.Core/Properties/AssemblyInfo.cs +++ b/Microphone.Core/Properties/AssemblyInfo.cs @@ -33,5 +33,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.1.2.0")] -[assembly: AssemblyFileVersion("0.1.2.0")] +[assembly: AssemblyVersion("0.1.3.0")] +[assembly: AssemblyFileVersion("0.1.3.0")] diff --git a/Microphone.Core/build.bat b/Microphone.Core/build.bat index 16ff470..ef021c6 100644 --- a/Microphone.Core/build.bat +++ b/Microphone.Core/build.bat @@ -1,3 +1,3 @@ del *.nupkg nuget pack Microphone.Core.csproj -IncludeReferencedProjects -Prop Configuration=Release -nuget push Microphone.Core.0.1.2.0.nupkg +nuget push Microphone.Core.0.1.3.0.nupkg diff --git a/Microphone.Nancy/Microphone.Nancy.csproj b/Microphone.Nancy/Microphone.Nancy.csproj index b1d6402..7c39c40 100644 --- a/Microphone.Nancy/Microphone.Nancy.csproj +++ b/Microphone.Nancy/Microphone.Nancy.csproj @@ -65,6 +65,7 @@ + diff --git a/Microphone.Nancy/Microphone.Nancy.nuspec b/Microphone.Nancy/Microphone.Nancy.nuspec index a33f796..f0a112c 100644 --- a/Microphone.Nancy/Microphone.Nancy.nuspec +++ b/Microphone.Nancy/Microphone.Nancy.nuspec @@ -15,7 +15,7 @@ Copyright 2015 Nancy WebApi Microservices Services ServiceDiscovery - + \ No newline at end of file diff --git a/Microphone.Nancy/Properties/AssemblyInfo.cs b/Microphone.Nancy/Properties/AssemblyInfo.cs index 92b5e29..132e419 100644 --- a/Microphone.Nancy/Properties/AssemblyInfo.cs +++ b/Microphone.Nancy/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.1.2.0")] -[assembly: AssemblyFileVersion("0.1.2.0")] +[assembly: AssemblyVersion("0.1.3.0")] +[assembly: AssemblyFileVersion("0.1.3.0")] diff --git a/Microphone.Nancy/build.bat b/Microphone.Nancy/build.bat index b1d0cb8..2ca8e34 100644 --- a/Microphone.Nancy/build.bat +++ b/Microphone.Nancy/build.bat @@ -1,3 +1,3 @@ del *.nupkg nuget pack Microphone.Nancy.csproj -IncludeReferencedProjects -Prop Configuration=Release -nuget push Microphone.Nancy.0.1.2.0.nupkg +nuget push Microphone.Nancy.0.1.3.0.nupkg diff --git a/Microphone.WebApi/Microphone.WebApi.csproj b/Microphone.WebApi/Microphone.WebApi.csproj index 17402bf..0bae799 100644 --- a/Microphone.WebApi/Microphone.WebApi.csproj +++ b/Microphone.WebApi/Microphone.WebApi.csproj @@ -62,6 +62,7 @@ + diff --git a/Microphone.WebApi/Microphone.WebApi.nuspec b/Microphone.WebApi/Microphone.WebApi.nuspec index a33f796..f0a112c 100644 --- a/Microphone.WebApi/Microphone.WebApi.nuspec +++ b/Microphone.WebApi/Microphone.WebApi.nuspec @@ -15,7 +15,7 @@ Copyright 2015 Nancy WebApi Microservices Services ServiceDiscovery - + \ No newline at end of file diff --git a/Microphone.WebApi/Properties/AssemblyInfo.cs b/Microphone.WebApi/Properties/AssemblyInfo.cs index bbc98dc..2416f66 100644 --- a/Microphone.WebApi/Properties/AssemblyInfo.cs +++ b/Microphone.WebApi/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.1.2.0")] -[assembly: AssemblyFileVersion("0.1.2.0")] +[assembly: AssemblyVersion("0.1.3.0")] +[assembly: AssemblyFileVersion("0.1.3.0")] diff --git a/Microphone.WebApi/build.bat b/Microphone.WebApi/build.bat index 19078b3..fcbdf2e 100644 --- a/Microphone.WebApi/build.bat +++ b/Microphone.WebApi/build.bat @@ -1,3 +1,3 @@ del *.nupkg nuget pack Microphone.WebApi.csproj -IncludeReferencedProjects -Prop Configuration=Release -nuget push Microphone.WebApi.0.1.2.0.nupkg +nuget push Microphone.WebApi.0.1.3.0.nupkg