예제 #1
0
        private ServerServiceDefinition _addInterceptors(ServerServiceDefinition service)
        {
            foreach (var interceptor in _interceptorChain)
            {
                service = service.Intercept(interceptor);
            }

            return(service
                   .Intercept(new LoggingInterceptor()));
        }
 public static ServerServiceDefinition UseBaseInterceptor(this ServerServiceDefinition serverServiceDefinition)
 {
     //性能监控,熔断处理
     return(serverServiceDefinition
            .Intercept(new MonitorInterceptor())    //性能监控
            .Intercept(new ThrottleInterceptor())); //熔断处理
 }
예제 #3
0
        /// <summary>
        /// Starts gRPC's server by binding the peer services, sets options and adds interceptors.
        /// </summary>
        internal Task StartListeningAsync()
        {
            ServerServiceDefinition serviceDefinition = PeerService.BindService(_serverService);

            if (_authInterceptor != null)
            {
                serviceDefinition = serviceDefinition.Intercept(_authInterceptor);
            }

            var serverOptions = new List <ChannelOption>
            {
                new ChannelOption(ChannelOptions.MaxSendMessageLength, GrpcConstants.DefaultMaxSendMessageLength),
                new ChannelOption(ChannelOptions.MaxReceiveMessageLength, GrpcConstants.DefaultMaxReceiveMessageLength)
            };

            // setup service
            _server = new Server(serverOptions);
            _server.Services.Add(serviceDefinition);

            var serverCredentials = CreateCredentials();

            // setup encrypted endpoint
            _server.Ports.Add(new ServerPort(IPAddress.Any.ToString(), NetworkOptions.ListeningPort, serverCredentials));

            return(Task.Run(() =>
            {
                _server.Start();

                foreach (var port in _server.Ports)
                {
                    Logger.LogDebug($"Server listening on {port.Host}:{port.BoundPort}.");
                }
            }));
        }
예제 #4
0
 //
 // Summary:
 //     Allows server side tracing to be sent to Application Insights by hooking
 //     Petabridge.Tracing.ApplicationInsights.ApplicationInsightsTracer into the gRPC service definition interceptor.
 public static ServerServiceDefinition UseApplicationInsights(this ServerServiceDefinition server, string instrumentationKey)
 {
     return(server.Intercept(
                new ServerTracingInterceptor(
                    new ApplicationInsightsTracer(
                        new TelemetryConfiguration(instrumentationKey)))));
 }
예제 #5
0
 //
 // Summary:
 //     Allows server side tracing to be sent to Application Insights by hooking
 //     Petabridge.Tracing.ApplicationInsights.ApplicationInsightsTracer into the gRPC service definition interceptor.
 //     Gets the instrumentation key from the APPINSIGHTS_INSTRUMENTATIONKEY environment variable.
 public static ServerServiceDefinition UseApplicationInsights(this ServerServiceDefinition server)
 {
     return(server.Intercept(
                new ServerTracingInterceptor(
                    new ApplicationInsightsTracer(
                        TelemetryConfiguration.Active))));
 }
예제 #6
0
        public async Task StartAsync()
        {
            ServerServiceDefinition serviceDefinition = PeerService.BindService(_serverService);

            if (_authInterceptor != null)
            {
                serviceDefinition = serviceDefinition.Intercept(_authInterceptor);
            }

            _server = new Server
            {
                Services = { serviceDefinition },
                Ports    =
                {
                    new ServerPort(IPAddress.Any.ToString(), _networkOptions.ListeningPort, ServerCredentials.Insecure)
                }
            };

            await Task.Run(() => _server.Start());

            // Add the provided boot nodes
            if (_networkOptions.BootNodes != null && _networkOptions.BootNodes.Any())
            {
                List <Task <bool> > taskList = _networkOptions.BootNodes.Select(_peerPool.AddPeerAsync).ToList();
                await Task.WhenAll(taskList.ToArray <Task>());
            }
            else
            {
                Logger.LogWarning("Boot nodes list is empty.");
            }
        }
예제 #7
0
        /// <summary>
        ///
        /// </summary>
        public void Start(params Interceptor[] interceptors)
        {
            ServerServiceDefinition definition = m_Service.Build();

            if (interceptors != null)
            {
                definition = definition.Intercept(interceptors);
            }

            m_Server.Services.Add(definition);
            m_Server.Start();
        }
        /// <summary>
        /// Intercept the service.
        /// </summary>
        /// <param name="service">The service.</param>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public ServerServiceDefinition Intercept(ServerServiceDefinition service, RpcConfigurationContext context)
        {
            Interceptor[] interceptors = RpcConfigurationUtility.CreateInterceptors(context, Interceptors, ExtraInterceptors);

            if (interceptors.Length > 0)
            {
                return(service.Intercept(interceptors));
            }
            else
            {
                return(service);
            }
        }
예제 #9
0
        /// <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);
            }
        }
예제 #10
0
        private ServerServiceDefinition ResolveInterceptors(ServerServiceDefinition definition)
        {
            if (_builder.Interceptors != null && _builder.Interceptors.Count > 0)
            {
                var interceptors = new List <Interceptor>();
                foreach (var interceptorType in _builder.Interceptors)
                {
                    var interceptor = (Interceptor)GrpcServerBuilder.ServiceProvider.GetService(interceptorType);
                    if (interceptor != null)
                    {
                        interceptors.Add(interceptor);
                    }
                }

                if (interceptors.Count > 0)
                {
                    definition = definition.Intercept(interceptors.ToArray());
                }
            }
            return(definition);
        }
예제 #11
0
        /// <summary>
        /// Starts gRPC's server by binding the peer services, sets options and adds interceptors.
        /// </summary>
        internal Task StartListeningAsync()
        {
            ServerServiceDefinition serviceDefinition = PeerService.BindService(_serverService);

            if (_authInterceptor != null)
            {
                serviceDefinition = serviceDefinition.Intercept(_authInterceptor);
            }

            _server = new Server(new List <ChannelOption>
            {
                new ChannelOption(ChannelOptions.MaxSendMessageLength, GrpcConstants.DefaultMaxSendMessageLength),
                new ChannelOption(ChannelOptions.MaxReceiveMessageLength, GrpcConstants.DefaultMaxReceiveMessageLength)
            })
            {
                Services = { serviceDefinition },
                Ports    =
                {
                    new ServerPort(IPAddress.Any.ToString(), NetworkOptions.ListeningPort, ServerCredentials.Insecure)
                }
            };

            return(Task.Run(() => _server.Start()));
        }