ServiceCenter_GoverExtensions.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Sers.Core.Module.App;
  2. using Sers.Gover.Base;
  3. using Vit.Core.Module.Log;
  4. using Vit.Core.Util.Threading;
  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 = GoverManage.Instance;
  17. serviceCenter.LoadSsApi(typeof(ServiceCenter_GoverExtensions).Assembly);
  18. //注册站点关闭回调
  19. SersApplication.onStop += (GoverManage.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. static void QpsTimer_Calc()
  28. {
  29. //(x.1)计算ApiStation qps
  30. try
  31. {
  32. foreach (var item in GoverManage.Instance.ApiStation_GetAll())
  33. {
  34. item.QpsCalc();
  35. }
  36. }
  37. catch (System.Exception ex)
  38. {
  39. Logger.Error(ex);
  40. }
  41. //(x.2)计算ServiceStation qps
  42. try
  43. {
  44. foreach (var item in GoverManage.Instance.serviceStationMng.serviceStationCollection)
  45. {
  46. item.QpsCalc();
  47. }
  48. }
  49. catch (System.Exception ex)
  50. {
  51. Logger.Error(ex);
  52. }
  53. //(x.3)按需保存计数信息到文件
  54. try
  55. {
  56. SaveToFile_tick++;
  57. if (SaveToFile_tick > 10)
  58. {
  59. SaveToFile_tick = 0;
  60. GoverManage.SaveToFile();
  61. }
  62. }
  63. catch (System.Exception ex)
  64. {
  65. Logger.Error(ex);
  66. }
  67. }
  68. static SersTimer timer = null;
  69. static void QpsTimer_Start()
  70. {
  71. if (null != timer) return;
  72. timer = new SersTimer
  73. {
  74. intervalMs = 2000,
  75. timerCallback =
  76. (object obj) =>
  77. {
  78. QpsTimer_Calc();
  79. }
  80. };
  81. timer.Start();
  82. }
  83. static void QpsTimer_Stop()
  84. {
  85. try
  86. {
  87. if (null != timer)
  88. {
  89. timer.Stop();
  90. timer.Dispose();
  91. timer = null;
  92. }
  93. }
  94. catch (System.Exception ex)
  95. {
  96. Logger.Error(ex);
  97. }
  98. }
  99. #endregion
  100. }
  101. }