/// <summary> /// Daemon工作状态的主方法 /// </summary> /// <param name="args"></param> public static void StartUp(string[] args) { AppDomain.CurrentDomain.ProcessExit += (s, e) => { if (Monitor.State) { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} AnnoCenter Service is being stopped·····"); Monitor.Stop(); Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} AnnoCenter The service has stopped!"); Console.ResetColor(); } }; Monitor.Start(); var tc = ThriftConfig.CreateInstance(); Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss}:服务注册、发现、健康检查、负载均衡中心,端口:{tc.Port}(AnnoCenter)已启动!"); Console.ResetColor(); //阻止daemon进程退出 while (true) { tc.ServiceInfoList.Distinct().Where(s => s.Checking == false).ToList().ForEach(service => { Task.Run(() => { Distribute.HealthCheck(service); }); }); Thread.Sleep(3000); } }
/// <summary> /// Daemon工作状态的主方法 /// </summary> /// <param name="args"></param> /// <param name="Notice">通知</param> /// <param name="ChangeNotice">变更通知</param> public static void StartUp(string[] args, Action <ServiceInfo, NoticeType> Notice = null, Action <ServiceInfo, ServiceInfo> ChangeNotice = null) { var tc = ThriftConfig.CreateInstance(); OutputLogo(tc); AppDomain.CurrentDomain.ProcessExit += (s, e) => { if (Monitor.State) { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} AnnoCenter Service is being stopped·····"); Monitor.Stop(); Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} AnnoCenter The service has stopped!"); Console.ResetColor(); } }; Monitor.Start(); #region 务上线 下线 变更通知 tc.ChangeNotice += (ServiceInfo newService, ServiceInfo oldService) => { try { tc.RefreshServiceMd5(); ChangeNotice?.Invoke(newService, oldService); } finally { } }; tc.OnlineNotice += (ServiceInfo service, NoticeType noticeType) => { try { tc.RefreshServiceMd5(); Notice?.Invoke(service, noticeType); } finally { } }; Distribute.CheckNotice += (ServiceInfo service, NoticeType noticeType) => { try { tc.RefreshServiceMd5(); Notice?.Invoke(service, noticeType); } finally { } }; #endregion Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss}:服务注册、发现、健康检查、负载均衡中心,端口:{tc.Port}(AnnoCenter)已启动!"); Console.ResetColor(); //阻止daemon进程退出 while (true) { tc.ServiceInfoList.Distinct().Where(s => s.Checking == false).ToList().ForEach(service => { Task.Run(() => { Distribute.HealthCheck(service); }); }); Thread.Sleep(3000); } }
/// <summary> /// Daemon工作状态的主方法 /// </summary> /// <param name="args"></param> /// <param name="Notice">通知</param> /// <param name="ChangeNotice">变更通知</param> public static void StartUp(string[] args, Action <ServiceInfo, NoticeType> Notice = null, Action <ServiceInfo, ServiceInfo> ChangeNotice = null) { var tc = ThriftConfig.CreateInstance(); OutputLogo(tc); AppDomain.CurrentDomain.ProcessExit += (s, e) => { if (Monitor.State) { Log.WriteLine("AnnoCenter Service is being stopped·····", ConsoleColor.DarkGreen); Monitor.Stop(); Log.WriteLine("AnnoCenter The service has stopped!", ConsoleColor.DarkGreen); } }; Monitor.Start(); #region 务上线 下线 变更通知 tc.ChangeNotice += (ServiceInfo newService, ServiceInfo oldService) => { try { tc.RefreshServiceMd5(); ChangeNotice?.Invoke(newService, oldService); } finally { } }; tc.OnlineNotice += (ServiceInfo service, NoticeType noticeType) => { try { tc.RefreshServiceMd5(); Notice?.Invoke(service, noticeType); } finally { } }; Distribute.CheckNotice += (ServiceInfo service, NoticeType noticeType) => { try { tc.RefreshServiceMd5(); Notice?.Invoke(service, noticeType); } finally { } }; #endregion Log.WriteLine($"服务注册、发现、健康检查、KV存储、API文档、负载均衡中心,端口:{tc.Port}(AnnoCenter)已启动!", ConsoleColor.DarkGreen); CronDaemon.AddJob("*/5 * * * * ? *", () => { Parallel.ForEach( tc.ServiceInfoList.Distinct().Where(s => s.Checking == false) , new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount } , service => { Task.Factory.StartNew(() => { Distribute.HealthCheck(service); }, TaskCreationOptions.LongRunning); }); }); CronDaemon.Start(); //阻止daemon进程退出 new AutoResetEvent(false).WaitOne(); }