123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- using Sers.Core.Module.App;
- using Sers.Gover.Base;
- using Vit.Core.Module.Log;
- using Vit.Core.Util.Threading.Timer;
- namespace Vit.Extensions
- {
- public static partial class ServiceCenter_GoverExtensions
- {
- /// <summary>
- /// 使用 Gover 服务治理 模块
- /// </summary>
- /// <param name="serviceCenter"></param>
- public static void UseGover(this Sers.ServiceCenter.ServiceCenter serviceCenter)
- {
- if (null == serviceCenter) return;
- serviceCenter.apiCenterService = GoverApiCenterService.Instance;
- serviceCenter.LoadSsApi(typeof(ServiceCenter_GoverExtensions).Assembly);
- //注册站点关闭回调
- SersApplication.onStop += (GoverApiCenterService.SaveToFile);
- #region ApiStation Qps 定时计算
- SersApplication.onStart += (QpsTimer_Start);
- SersApplication.onStop += (QpsTimer_Stop);
- #endregion
- }
- #region QpsTimer
- static int SaveToFile_tick = 0;
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
- static void QpsTimer_Calc()
- {
- //(x.1)计算ApiStation qps
- try
- {
- foreach (var item in GoverApiCenterService.Instance.ApiStation_GetAll())
- {
- item.QpsCalc();
- }
- }
- catch (System.Exception ex)
- {
- Logger.Error(ex);
- }
- //(x.2)计算ServiceStation qps
- try
- {
- foreach (var item in GoverApiCenterService.Instance.serviceStationMng.serviceStationCollection)
- {
- item.QpsCalc();
- }
- }
- catch (System.Exception ex)
- {
- Logger.Error(ex);
- }
- //(x.3)按需保存计数信息到文件
- try
- {
- SaveToFile_tick++;
- if (SaveToFile_tick > 10)
- {
- SaveToFile_tick = 0;
- GoverApiCenterService.SaveToFile();
- }
- }
- catch (System.Exception ex)
- {
- Logger.Error(ex);
- }
- }
- static VitTimer timer = null;
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
- static void QpsTimer_Start()
- {
- if (null != timer) return;
- timer = new VitTimer
- {
- intervalMs = 2000,
- timerCallback =
- (object obj) =>
- {
- QpsTimer_Calc();
- }
- };
- timer.Start();
- }
- [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
- static void QpsTimer_Stop()
- {
- try
- {
- if (null != timer)
- {
- timer.Stop();
- timer.Dispose();
- timer = null;
- }
- }
- catch (System.Exception ex)
- {
- Logger.Error(ex);
- }
- }
- #endregion
- }
- }
|