MetaService.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using Grpc.Core;
  6. using Grpc.Extension.BaseService.Model;
  7. using System.Linq;
  8. using Grpc.Extension.Common;
  9. using TEAMModelOS.SDK.Extension;
  10. using System.Text.Json;
  11. namespace Grpc.Extension.BaseService
  12. {
  13. /// <summary>
  14. /// Grpc元数据服务
  15. /// </summary>
  16. public class MetaService : IGrpcBaseService
  17. {
  18. /// <summary>
  19. /// 服务基本信息
  20. /// </summary>
  21. public Task<InfoRS> Info(InfoRQ rq, ServerCallContext context)
  22. {
  23. return Task.Run(() =>
  24. {
  25. var methods = MetaModel.Methods.Select(q => q.FullName?.Trim()).ToList();
  26. if (!string.IsNullOrWhiteSpace(rq.MethodName))
  27. {
  28. methods = methods?.Where(q => q.ToLower().Contains(rq.MethodName.Trim().ToLower())).ToList();
  29. }
  30. var methodInfos = new List<GrpcMethodInfo>();
  31. foreach (var m in methods)
  32. {
  33. var info = new GrpcMethodInfo { Name = m };
  34. info.IsThrottled = ThrottleManager.Instance.IsThrottled(m);
  35. info.SaveResponseEnable = MonitorManager.Instance.SaveResponseMethodEnable(m);
  36. methodInfos.Add(info);
  37. }
  38. return new InfoRS
  39. {
  40. IpAndPort = $"{MetaModel.Ip}:{MetaModel.Port}",
  41. StartTime = MetaModel.StartTime.ToUnixTimestamp(),
  42. MethodInfos = methodInfos
  43. };
  44. });
  45. }
  46. /// <summary>
  47. /// 服务方法的详细信息
  48. /// </summary>
  49. public Task<MethodInfoRS> MethodInfo(MethodInfoRQ rq, ServerCallContext context)
  50. {
  51. return Task.Run(() =>
  52. {
  53. var methodInfo = MetaModel.Methods.FirstOrDefault(q => q.FullName == rq.FullName?.Trim());
  54. if (methodInfo == null)
  55. {
  56. return new MethodInfoRS();
  57. }
  58. return new MethodInfoRS
  59. {
  60. RequestJson = Activator.CreateInstance(methodInfo.RequestType).FillProp().ToJsonString(new JsonSerializerOptions() { IgnoreNullValues = false }),
  61. ResponseJson = Activator.CreateInstance(methodInfo.ResponseType).FillProp().ToJsonString(new JsonSerializerOptions() { IgnoreNullValues = false })
  62. };
  63. });
  64. }
  65. //TODO 此段代碼後續檢視
  66. /// <summary>
  67. /// 服务方法调用
  68. /// </summary>
  69. //public Task<MethodInvokeRS> MethodInvoke(MethodInvokeRQ rq, ServerCallContext context)
  70. //{
  71. // //return Task.Run(() =>
  72. // //{
  73. // // var methodInfo = MetaModel.Methods.FirstOrDefault(q => q.FullName == rq.FullName?.Trim());
  74. // // if (methodInfo == null)
  75. // // {
  76. // // return new MethodInvokeRS() { ResponseJson = $"not fount method by fullname:{rq.FullName}" };
  77. // // }
  78. // // var task = (Task)methodInfo.Handler.DynamicInvoke(rq.RequestJson?.Trim().FromJson(methodInfo.RequestType), context);
  79. // // task.Wait();
  80. // // dynamic result = task;
  81. // // return new MethodInvokeRS { ResponseJson = ((object)result.Result).ToJson(ignoreNullValue: false, isIndented: true) };
  82. // //});
  83. //}
  84. }
  85. }