public GrpcExampleHostedService( IServerTracer tracer, GrpcExampleService.GrpcExampleServiceBase grpcServiceBase) { _tracer = tracer; _grpcServiceBase = grpcServiceBase; }
public GrpcExampleHostedService( IServerTracer tracer, GrpcExampleService.GrpcExampleServiceBase grpcServiceBase, IConfiguration configuration) { _tracer = tracer; _grpcServiceBase = grpcServiceBase; _configuration = configuration; }
/// <summary> /// Grpc服务启动,注册多个服务实现 /// </summary> /// <param name="services">grpc service definition</param> /// <param name="tracer">拦截器记录</param> /// <param name="interceptors">其他拦截器</param> /// <param name="channelOptions">Channel配置</param> /// <param name="whenException">==null => throw</param> /// <param name="configPath">配置文件路径 default: dllconfig/{namespace}.dll.[config/json]</param> public static void Start( IEnumerable <ServerServiceDefinition> services, IServerTracer tracer = null, List <Interceptor> interceptors = null, List <ChannelOption> channelOptions = null, Action <Exception> whenException = null, Action <GrpcOptions> grpcConfigAction = null, string serviceIdSuffix = "") { try { #region 启动服务 var serviceElement = ResolveServiceConfiguration(grpcConfigAction); if (tracer != null) { tracer.ServiceName = serviceElement.Name; interceptors = interceptors ?? new List <Interceptor>(); interceptors.Add(new ServerTracerInterceptor(tracer)); } server = new Server(channelOptions) { Ports = { new ServerPort("0.0.0.0", serviceElement.Port, ServerCredentials.Insecure) } }; foreach (var service in services) { if (interceptors?.Count > 0) { server.Services.Add(service.Intercept(interceptors.ToArray())); } else { server.Services.Add(service); } } server.Start(); #endregion #region 注册服务 var address = ResolveConsulConfiguration(serviceElement); if (string.IsNullOrEmpty(address)) { return; } serverRegister = new ServerRegister(address); serverRegister.Register(serviceElement, entry => discoveryEntry = entry, serviceIdSuffix: serviceIdSuffix); #endregion } catch (Exception ex) { Stop(); InvokeException(ex, whenException); } }
/// <summary> /// Grpc服务启动 /// </summary> /// <param name="service">grpc service definition</param> /// <param name="tracer">拦截器记录</param> /// <param name="interceptors">其他拦截器</param> /// <param name="configPath">配置文件路径 default: dllconfig/{namespace}.dll.[config/json]</param> /// <param name="channelOptions">Channel配置</param> /// <param name="whenException">==null => throw</param> public static void Start( ServerServiceDefinition service, IServerTracer tracer = null, List <Interceptor> interceptors = null, string configPath = "", List <ChannelOption> channelOptions = null, Action <Exception> whenException = null) { try { #region 启动服务 var serviceElement = ResolveServiceConfiguration(configPath); if (tracer != null) { tracer.ServiceName = serviceElement.Name; interceptors = interceptors ?? new List <Interceptor>(); interceptors.Add(new ServerTracerInterceptor(tracer)); } if (interceptors?.Count > 0) { service = service.Intercept(interceptors.ToArray()); } server = new Server(channelOptions) { Services = { service }, Ports = { new ServerPort("0.0.0.0", serviceElement.Port, ServerCredentials.Insecure) } }; server.Start(); #endregion #region 注册服务 var address = ResolveConsulConfiguration(serviceElement); if (string.IsNullOrEmpty(address)) { return; } serverRegister = new ServerRegister(address); serverRegister.Register(serviceElement, entry => discoveryEntry = entry); #endregion } catch (Exception ex) { Stop(); InvokeException(ex, whenException); } }
/// <summary> /// Grpc服务启动 /// </summary> /// <param name="service">grpc service definition</param> /// <param name="tracer">拦截器记录</param> /// <param name="interceptors">其他拦截器</param> /// <param name="channelOptions">Channel配置</param> /// <param name="whenException">==null => throw</param> /// <param name="configPath">配置文件路径 default: dllconfig/{namespace}.dll.[config/json]</param> public static void Start( ServerServiceDefinition service, IServerTracer tracer = null, List <Interceptor> interceptors = null, List <ChannelOption> channelOptions = null, Action <Exception> whenException = null, Action <GrpcOptions> grpcConfigAction = null) { if (service == null) { throw new ArgumentNullException("service"); } var services = new List <ServerServiceDefinition>() { service }; Start(services, tracer, interceptors, channelOptions, whenException, grpcConfigAction); }
public InterceptedServerHandler(IServerTracer tracer, ServerCallContext context) { _tracer = tracer; _context = context; }
public ServerTracerInterceptor(IServerTracer tracer) { _tracer = tracer; }