ServiceCollectionExtensions.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Grpc.AspNetCore.Server;
  2. using Grpc.AspNetCore.Server.Model;
  3. using Grpc.Extension;
  4. using Grpc.Extension.AspNetCore;
  5. using Grpc.Extension.Client;
  6. using Grpc.Extension.Interceptors;
  7. using Grpc.Extension.AspNetCore.Internal;
  8. using Microsoft.Extensions.Configuration;
  9. using Microsoft.Extensions.DependencyInjection;
  10. using Microsoft.Extensions.DependencyInjection.Extensions;
  11. using Microsoft.Extensions.Logging;
  12. using Microsoft.Extensions.Options;
  13. using OpenTracing;
  14. using System;
  15. namespace Grpc.Extension.AspNetCore
  16. {
  17. public static class ServiceCollectionExtensions
  18. {
  19. /// <summary>
  20. /// 添加Grpc扩展
  21. /// </summary>
  22. /// <param name="services"></param>
  23. /// <param name="conf"></param>
  24. public static void AddGrpcExtensions(this IServiceCollection services, IConfiguration conf) => AddGrpcExtensions(services, conf, null);
  25. /// <summary>
  26. /// 添加Grpc扩展
  27. /// </summary>
  28. /// <param name="services"></param>
  29. /// <param name="conf"></param>
  30. /// <param name="configureOptions"></param>
  31. /// <returns></returns>
  32. public static IGrpcServerBuilder AddGrpcExtensions(this IServiceCollection services, IConfiguration conf, Action<GrpcServiceOptions> configureOptions)
  33. {
  34. //注入配制
  35. services.Configure<GrpcServerOptions>(conf.GetSection("GrpcServer"));
  36. //AddGrpc
  37. var builder = services.AddGrpc(options => {
  38. options.Interceptors.Add<MonitorInterceptor>();
  39. options.Interceptors.Add<ThrottleInterceptor>();
  40. //Jaeger
  41. options.AddJaegerInterceptor(conf);
  42. //执行配制
  43. configureOptions?.Invoke(options);
  44. });
  45. //ServiceMethodProvider
  46. services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IServiceMethodProvider<>), typeof(BinderServiceMethodProvider<>)));
  47. //添加Jaeger
  48. services.AddJaeger(conf);
  49. //添加GrpcClient扩展
  50. services.AddGrpcClientExtensions(conf);
  51. //注册到服务发现
  52. services.AddHostedService<RegisterServiceHosted>();
  53. return builder;
  54. }
  55. /// <summary>
  56. /// 添加Jaeger
  57. /// </summary>
  58. /// <param name="services"></param>
  59. /// <param name="conf"></param>
  60. /// <returns></returns>
  61. private static IServiceCollection AddJaeger(this IServiceCollection services, IConfiguration conf)
  62. {
  63. var key = "GrpcServer:Jaeger";
  64. var jaegerOptions = conf.GetSection(key).Get<JaegerOptions>();
  65. if (jaegerOptions == null || jaegerOptions.Enable == false)
  66. return services;
  67. //jaeger
  68. services.AddSingleton<ITracer>(sp => {
  69. var options = sp.GetService<IOptions<GrpcServerOptions>>().Value.Jaeger;
  70. var serviceName = options.ServiceName;
  71. var tracer = new Jaeger.Tracer.Builder(serviceName)
  72. .WithLoggerFactory(sp.GetService<ILoggerFactory>())
  73. .WithSampler(new Jaeger.Samplers.ConstSampler(true))
  74. .WithReporter(new Jaeger.Reporters.RemoteReporter.Builder()
  75. .WithFlushInterval(TimeSpan.FromSeconds(5))
  76. .WithMaxQueueSize(5)
  77. .WithSender(new Jaeger.Senders.UdpSender(jaegerOptions.AgentIp, jaegerOptions.AgentPort, 1024 * 5)).Build())
  78. .Build();
  79. return tracer;
  80. });
  81. return services;
  82. }
  83. /// <summary>
  84. /// 添加jaeger中间件
  85. /// </summary>
  86. /// <param name="grpcServiceOptions"></param>
  87. /// <param name="conf"></param>
  88. private static void AddJaegerInterceptor(this GrpcServiceOptions grpcServiceOptions, IConfiguration conf)
  89. {
  90. var key = "GrpcServer:Jaeger";
  91. var jaegerOptions = conf.GetSection(key).Get<JaegerOptions>();
  92. if (jaegerOptions == null || jaegerOptions.Enable == false)
  93. return;
  94. //添加jaeger中间件
  95. grpcServiceOptions.Interceptors.Add<JaegerTracingInterceptor>();
  96. }
  97. }
  98. }