ServiceCenter_GoverExtensions.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using Sers.Core.Module.App;
  2. using Sers.Gover.Base;
  3. using Vit.Core.Module.Log;
  4. using Vit.Core.Util.Threading.Timer;
  5. namespace Vit.Extensions
  6. {
  7. public static partial class ServiceCenter_GoverExtensions
  8. {
  9. /// <summary>
  10. /// 使用 Gover 服务治理 模块
  11. /// </summary>
  12. /// <param name="serviceCenter"></param>
  13. public static void UseGover(this Sers.ServiceCenter.ServiceCenter serviceCenter)
  14. {
  15. if (null == serviceCenter) return;
  16. serviceCenter.apiCenterService = GoverApiCenterService.Instance;
  17. serviceCenter.LoadSsApi(typeof(ServiceCenter_GoverExtensions).Assembly);
  18. //注册站点关闭回调
  19. SersApplication.onStop += (GoverApiCenterService.SaveToFile);
  20. #region ApiStation Qps 定时计算
  21. SersApplication.onStart += (QpsTimer_Start);
  22. SersApplication.onStop += (QpsTimer_Stop);
  23. #endregion
  24. }
  25. #region QpsTimer
  26. static int SaveToFile_tick = 0;
  27. [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
  28. static void QpsTimer_Calc()
  29. {
  30. //(x.1)计算ApiStation qps
  31. try
  32. {
  33. foreach (var item in GoverApiCenterService.Instance.ApiStation_GetAll())
  34. {
  35. item.QpsCalc();
  36. }
  37. }
  38. catch (System.Exception ex)
  39. {
  40. Logger.Error(ex);
  41. }
  42. //(x.2)计算ServiceStation qps
  43. try
  44. {
  45. foreach (var item in GoverApiCenterService.Instance.serviceStationMng.serviceStationCollection)
  46. {
  47. item.QpsCalc();
  48. }
  49. }
  50. catch (System.Exception ex)
  51. {
  52. Logger.Error(ex);
  53. }
  54. //(x.3)按需保存计数信息到文件
  55. try
  56. {
  57. SaveToFile_tick++;
  58. if (SaveToFile_tick > 10)
  59. {
  60. SaveToFile_tick = 0;
  61. GoverApiCenterService.SaveToFile();
  62. }
  63. }
  64. catch (System.Exception ex)
  65. {
  66. Logger.Error(ex);
  67. }
  68. }
  69. static VitTimer timer = null;
  70. [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
  71. static void QpsTimer_Start()
  72. {
  73. if (null != timer) return;
  74. timer = new VitTimer
  75. {
  76. intervalMs = 2000,
  77. timerCallback =
  78. (object obj) =>
  79. {
  80. QpsTimer_Calc();
  81. }
  82. };
  83. timer.Start();
  84. }
  85. [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
  86. static void QpsTimer_Stop()
  87. {
  88. try
  89. {
  90. if (null != timer)
  91. {
  92. timer.Stop();
  93. timer.Dispose();
  94. timer = null;
  95. }
  96. }
  97. catch (System.Exception ex)
  98. {
  99. Logger.Error(ex);
  100. }
  101. }
  102. #endregion
  103. }
  104. }