示例#1
0
        /// <summary>
        /// 解析配置文件
        /// </summary>
        /// <param name="configFile"></param>
        /// <returns></returns>
        private static Exitus ResolveConfiguration(GrpcClientOptions options)
        {
            var service = ResolveServiceConfiguration(options.ConfigPath);

            if (string.IsNullOrWhiteSpace(options.ServiceName))
            {
                options.ServiceName = service.Name;
            }
            if (options.MaxRetry <= 0)
            {
                options.MaxRetry = service.MaxRetry;
            }
            options.ChannelOptions = options.ChannelOptions ?? Constants.DefaultChannelOptions;

            IEndpointStrategy endpointStrategy;

            if (EnableConsul(service.Discovery, out string address))
            {
                endpointStrategy = ResolveStickyConfiguration(address, options);
            }
            else
            {
                endpointStrategy = ResolveEndpointConfiguration(service, options);
            }
            return(new Exitus(options.ServiceName, endpointStrategy));
        }
示例#2
0
        /// <summary>
        /// 解析Consul配置
        /// </summary>
        /// <param name="serviceElement"></param>
        /// <returns></returns>
        private static IEndpointStrategy ResolveStickyConfiguration(string address, GrpcClientOptions options)
        {
            // consul
            var stickyEndpointDiscovery = new StickyEndpointDiscovery(options, address);

            EndpointStrategy.Instance.AddServiceDiscovery(stickyEndpointDiscovery);
            return(EndpointStrategy.Instance);
        }
示例#3
0
 /// <summary>
 /// 初始化配置
 /// </summary>
 private void InitOptions()
 {
     _options            = _options ?? new GrpcClientOptions <T>();
     _options.ConfigPath = GetConfigPath(_options.ConfigPath);
     if (_options.Tracer != null)
     {
         _options.Interceptors.Add(new ClientTracerInterceptor(_options.Tracer));
     }
 }
示例#4
0
        /// <summary>
        /// 获取客户端
        /// </summary>
        /// <param name="configPath">配置文件路径</param>
        /// <param name="options">配置信息</param>
        /// <param name="callInvokers">自定义invoker获取策略</param>
        /// <returns></returns>
        public static T Get(string configPath = "", GrpcClientOptions options = default, Func <List <ServerCallInvoker>, ServerCallInvoker> callInvokers = null)
        {
            options = options ?? new GrpcClientOptions();
            if (!string.IsNullOrWhiteSpace(configPath))
            {
                options.ConfigPath = configPath;
            }

            return(Get(options, callInvokers));
        }
示例#5
0
        public ClientCallInvoker(
            GrpcClientOptions options,
            IEndpointStrategy strategy,
            Func <List <ServerCallInvoker>, ServerCallInvoker> callInvokers = default)
        {
            _options  = options;
            _strategy = strategy;

            _callInvokers = callInvokers;
        }
示例#6
0
        public IPEndpointDiscovery(GrpcClientOptions options, List <Tuple <string, int> > ipEndPoints)
        {
            if ((ipEndPoints?.Count ?? 0) <= 0)
            {
                throw new ArgumentNullException("no ip endpoints availble");
            }

            _ipEndPoints = ipEndPoints;

            Options = options;
        }
示例#7
0
        /// <summary>
        /// 获取客户端
        /// </summary>
        /// <param name="configPath">配置文件路径</param>
        /// <param name="tracer">tracer拦截器</param>
        /// <param name="interceptors">自定义拦截器</param>
        /// <param name="callInvokers">自定义invoker获取策略</param>
        /// <returns></returns>
        public static T Get(string configPath = "", IClientTracer tracer = null, List <Interceptor> interceptors = null, Func <List <ServerCallInvoker>, ServerCallInvoker> callInvokers = null)
        {
            var options = new GrpcClientOptions()
            {
                Tracer     = tracer,
                ConfigPath = configPath,
            };

            if (interceptors?.Count > 0)
            {
                options.Interceptors.AddRange(interceptors);
            }
            return(Get(options, callInvokers));
        }
        public StickyEndpointDiscovery(GrpcClientOptions options, string address, bool startWatch = true)
        {
            if (string.IsNullOrEmpty(address))
            {
                throw new ArgumentNullException("consul address");
            }

            _client = new ConsulClient((cfg) =>
            {
                var uriBuilder = new UriBuilder(address);
                cfg.Address    = uriBuilder.Uri;
            });

            Options = options;

            if (startWatch)
            {
                StartWatchService();
            }
        }
示例#9
0
        /// <summary>
        /// 获取EndpointStrategy
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="options"></param>
        /// <returns></returns>
        public static Exitus Get <T>(GrpcClientOptions options) where T : ClientBase
        {
            if (_exitusMap.TryGetValue(typeof(T), out Exitus exitus) &&
                exitus?.EndpointStrategy != null)
            {
                return(exitus);
            }

            lock (_lockHelper)
            {
                if (_exitusMap.TryGetValue(typeof(T), out exitus) &&
                    exitus?.EndpointStrategy != null)
                {
                    return(exitus);
                }

                exitus = ResolveConfiguration(options);
                _exitusMap.AddOrUpdate(typeof(T), exitus, (k, v) => exitus);
                return(exitus);
            }
        }
示例#10
0
 public GrpcClientFactory(GrpcClientOptions options)
 {
     _options = new GrpcClientOptions <T>(options);
     InitOptions();
 }
示例#11
0
 public GrpcClientFactory(IOptions <GrpcClientOptions <T> > options)
 {
     _options = options?.Value;
     InitOptions();
 }
示例#12
0
        /// <summary>
        /// 解析Endpoint配置
        /// </summary>
        /// <param name="service"></param>
        /// <returns></returns>
        private static IEndpointStrategy ResolveEndpointConfiguration(Client.GrpcServiceElement service, GrpcClientOptions options)
        {
            var discovery = service.Discovery;

            List <Tuple <string, int> > ipEndPoints = null;

#if !ASP_NET_CORE
            ipEndPoints = discovery.EndPoints.ToList();
#else
            ipEndPoints = discovery.EndPoints.Select(oo => Tuple.Create(oo.Host, oo.Port)).ToList();
#endif
            var iPEndpointDiscovery = new IPEndpointDiscovery(options, ipEndPoints);
            EndpointStrategy.Instance.AddServiceDiscovery(iPEndpointDiscovery);
            return(EndpointStrategy.Instance);
        }
示例#13
0
 public GrpcClientFactory(IOptions <GrpcClientOptions <T> > options = null, IClientTracer tracer = null, IOptions <GrpcClientOptions> grpcOptions = null)
 {
     _options      = options?.Value;
     _tracer       = tracer;
     _interceptors = grpcOptions?.Value?.Interceptors;
 }
示例#14
0
 public GrpcClientFactory(IOptions <GrpcClientOptions <T> > options = null, IClientTracer tracer = null)
 {
     _options = options?.Value;
     _tracer  = tracer;
 }
示例#15
0
        /// <summary>
        /// 获取客户端
        /// </summary>
        /// <param name="configPath">配置文件路径</param>
        /// <param name="tracer">tracer拦截器</param>
        /// <param name="interceptors">自定义拦截器</param>
        /// <param name="callInvokers">自定义invoker获取策略</param>
        /// <returns></returns>
        public static T Get(GrpcClientOptions options, Func <List <ServerCallInvoker>, ServerCallInvoker> callInvokers = null)
        {
            var factory = _clientFactoryCache.GetOrAdd(typeof(T), key => new GrpcClientFactory <T>(options));

            return(_clientCache.GetOrAdd(typeof(T), key => factory.Get(callInvokers)));
        }