/// <summary> /// 插件启动配置 /// </summary> /// <param name="iocAction">用于用户自定义做依赖注入</param> public static void Bootstrap(Action iocAction, Loader.IocType iocType) { Const.SettingService.InitConfig(); Loader.IocLoader.RegisterIoc(iocType); iocAction?.Invoke(); PreConfigurationBootstrap(); Loader.IocLoader.Build(); var bootstraps = Loader.IocLoader.Resolve <IEnumerable <IPlugsConfigurationBootstrap> >(); if (bootstraps != null) { foreach (var plugsConfigurationBootstrap in bootstraps) { plugsConfigurationBootstrap.ConfigurationBootstrap(); } } BuilderRouterInfo(); }
/// <summary> /// 启动 server /// </summary> /// <param name="args"></param> /// <param name="diAction"></param> /// <param name="iocType">依赖注入类型</param> public static void StartUp(string[] args, Action diAction, Action startUpCallBack = null, Loader.IocType iocType = Loader.IocType.Autofac) { IocType = iocType; var reStar = false; reStart: try { Enter(args, diAction, reStar); startUpCallBack?.Invoke(); AppDomain.CurrentDomain.ProcessExit += (s, e) => { if (Server.State) { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine( $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} {Const.SettingService.AppName} Service is being stopped·····"); Server.Stop(); Console.WriteLine( $"{DateTime.Now:yyyy-MM-dd HH:mm:ss} {Const.SettingService.AppName} The service has stopped!"); Console.ResetColor(); } }; //阻止daemon进程退出 while (true) { Thread.Sleep(1000); } } catch (Exception e) { Log.Log.Error(e); if (e is Thrift.TException) { reStar = true; } else { throw; } } //服务因为传输协议异常自动退出,需要重启启动。如果是配置错误弹出配置错误信息 goto reStart; }
/// <summary> /// 启动服务 /// </summary> /// <param name="args"></param> /// <param name="diAction"></param> /// <param name="reStart">异常之中回复启动,true。正常启动,false</param> static void Enter(string[] args, Action diAction, bool reStart, Loader.IocType iocType) { if (!reStart) { EngineData.AnnoBootstrap.Bootstrap(diAction, iocType); } #region 设置监听端口(可以通过参数 设置。没有取配置文件) int.TryParse(ArgsValue.GetValueByName("-p", args), out int port); if (port > 0) { Const.SettingService.Local.Port = port; } long.TryParse(ArgsValue.GetValueByName("-t", args), out long timeout); if (timeout > 0) { Const.SettingService.TimeOut = timeout; } int.TryParse(ArgsValue.GetValueByName("-w", args), out int weight); if (weight > 0) { Const.SettingService.Weight = weight; } var host = ArgsValue.GetValueByName("-h", args); if (host != null) { System.Net.IPAddress.TryParse(host, out System.Net.IPAddress ipAddress); if (ipAddress != null) { Const.SettingService.Local.IpAddress = host; } else { Log.WriteLine("-h 参数错误!", ConsoleColor.DarkYellow); } } var traceOnOffStr = ArgsValue.GetValueByName("-tr", args); if (!string.IsNullOrWhiteSpace(traceOnOffStr)) { bool.TryParse(traceOnOffStr, out bool traceOnOff); Const.SettingService.TraceOnOff = traceOnOff; } #endregion if (!Server.State) { Server.Start(); } Log.WriteLine($"节点【{Const.SettingService.AppName}】(端口:{Const.SettingService.Local.Port})已启动", ConsoleColor.DarkGreen); foreach (var f in Anno.Const.SettingService.FuncName.Split(',')) { Log.WriteLine($"{f}", ConsoleColor.DarkGreen); } Log.WriteLine($"{"权重:" + Const.SettingService.Weight}", ConsoleColor.DarkGreen); Log.WriteLineNoDate("-----------------------------------------------------------------------------"); Const.SettingService.Ts.ForEach(t => { new Client.Register().ToCenter(t, 60); }); /* * 1、 Const.SettingService.Local 在AnnoService(服务提供方)中 作为 本机信息 * 2、Const.SettingService.Local 在客户端 作为 负载均衡地址信息(AnnoCenter) * 3、此处为了让AnnoService 可以调用外部服务 Local使用完毕后更改为 AnnoCenter 地址 */ Const.Target target = Const.SettingService.Ts?.First(); Client.DefaultConfigManager.SetDefaultConfiguration(Const.SettingService.AppName, target.IpAddress, target.Port, Const.SettingService.TraceOnOff); }
/// <summary> /// 启动 server /// </summary> /// <param name="args"></param> /// <param name="diAction"></param> /// <param name="iocType">依赖注入类型</param> public static void StartUp(string[] args, Action diAction, Action startUpCallBack = null, Loader.IocType iocType = Loader.IocType.Autofac) { var reStar = false; reStart: try { Enter(args, diAction, reStar, iocType); try { startUpCallBack?.Invoke(); } catch (Exception ex) { Log.WriteLineAlignNoDate(ex.Message); } AppDomain.CurrentDomain.ProcessExit += (s, e) => { if (Server.State) { Log.WriteLine($"{Const.SettingService.AppName} Service is being stopped·····", ConsoleColor.DarkGreen); Server.Stop(); Log.WriteLine($"{Const.SettingService.AppName} The service has stopped!", ConsoleColor.DarkGreen); } }; //阻止daemon进程退出 while (true) { Thread.Sleep(1000); } } catch (Exception e) { Log.Error(e); if (e is Grpc.Core.RpcException) { reStar = true; } else { throw; } } //服务因为传输协议异常自动退出,需要重启启动。如果是配置错误弹出配置错误信息 goto reStart; }
/// <summary> /// 启动 server /// </summary> /// <param name="args"></param> /// <param name="diAction"></param> /// <param name="iocType">依赖注入类型</param> public static void StartUp(string[] args, Action diAction, Action startUpCallBack = null, Loader.IocType iocType = Loader.IocType.Autofac) { var reStar = false; reStart: try { if (args != null && args.Contains("--reg")) { regRegister = true; } Enter(args, diAction, reStar, iocType); if (regRegister) { Log.WriteLine("重新注册完成,3秒后窗口自动关闭"); Task.Delay(3000).Wait(); return; } try { startUpCallBack?.Invoke(); } catch (Exception ex) { Log.WriteLineAlignNoDate(ex.Message); } AppDomain.CurrentDomain.ProcessExit += (s, e) => { if (Server.State) { Log.WriteLine($"{Const.SettingService.AppName} Service is being stopped·····", ConsoleColor.DarkGreen); Server.Stop(); Log.WriteLine($"{Const.SettingService.AppName} The service has stopped!", ConsoleColor.DarkGreen); } }; //阻止daemon进程退出 (new AutoResetEvent(false)).WaitOne(); } catch (Exception e) { Log.Error(e); if (e is Grpc.Core.RpcException) { reStar = true; } else { throw; } } //服务因为传输协议异常自动退出,需要重启启动。如果是配置错误弹出配置错误信息 goto reStart; }