Пример #1
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);
            }
        }
Пример #2
0
 public Entry(ServerRegister serverRegister, string serviceId)
 {
     ServiceId       = serviceId;
     _serverRegister = serverRegister;
 }