Beispiel #1
0
        /// <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="services">grpc service definition</param>
        /// <param name="whenException">==null => throw</param>
        public static void Start(
            IEnumerable <ServerServiceDefinition> services,
            Action <GrpcOptions> grpcOptionBuilder = null,
            Action <Exception> whenException       = null)
        {
            try
            {
                var grpcOptions = new GrpcOptions();
                grpcOptionBuilder?.Invoke(grpcOptions);

                #region 启动服务
                var serviceElement = ResolveServiceConfiguration(grpcOptions);
                if (grpcOptions?.Tracer != null)
                {
                    grpcOptions.Tracer.ServiceName = serviceElement.Name;
                    grpcOptions.Interceptors.Add(new ServerTracerInterceptor(grpcOptions.Tracer));
                }
                server = new Server(grpcOptions.ChannelOptions)
                {
                    Ports = { new ServerPort("0.0.0.0", serviceElement.Port, ServerCredentials.Insecure) }
                };
                foreach (var service in services)
                {
                    if (grpcOptions.Interceptors?.Count > 0)
                    {
                        server.Services.Add(service.Intercept(grpcOptions.Interceptors.ToArray()));
                    }
                    else
                    {
                        server.Services.Add(service);
                    }
                }
                server.Start();
                #endregion

                #region 注册服务
                var address = ResolveConsulConfiguration(serviceElement);
                if (string.IsNullOrEmpty(address))
                {
                    return;
                }
                serverRegister = new ServerRegister(address, grpcOptions.GenServiceId);
                serverRegister.Register(serviceElement, entry => discoveryEntry = entry);
                #endregion
            }
            catch (Exception ex)
            {
                Stop();
                InvokeException(ex, whenException);
            }
        }
Beispiel #3
0
 public Entry(ServerRegister serverRegister, string serviceId)
 {
     ServiceId       = serviceId;
     _serverRegister = serverRegister;
 }