예제 #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 (string.IsNullOrWhiteSpace(options.Scheme))
            {
                options.Scheme = service.Scheme;
            }

            options.GrpcChannelOptions ??= 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="address"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        private static IEndpointStrategy ResolveStickyConfiguration(string address, GrpcClientOptions options)
        {
            var stickyEndpointDiscovery = new StickyEndpointDiscovery(options, address);

            EndpointStrategy.Instance.AddServiceDiscovery(stickyEndpointDiscovery);
            return(EndpointStrategy.Instance);
        }
예제 #3
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;
        }
        public StickyEndpointDiscovery(GrpcClientOptions options, string address, bool startWatch = true)
        {
            if (string.IsNullOrWhiteSpace(address))
            {
                throw new ArgumentNullException("consul address");
            }

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

            Options = options;

            if (startWatch)
            {
                StartWatchService();
            }
        }
예제 #5
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);
            }
        }
예제 #6
0
 public GrpcClientFactory(IOptions <GrpcClientOptions <T> > options = null)
 {
     _options            = options?.Value ?? new GrpcClientOptions <T>();
     _options.ConfigPath = GetConfigPath(_options.ConfigPath);
 }
예제 #7
0
        /// <summary>
        /// 解析Endpoint配置
        /// </summary>
        /// <param name="service"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        private static IEndpointStrategy ResolveEndpointConfiguration(GrpcServiceElement service, GrpcClientOptions options)
        {
            var ipEndPoints         = service.Discovery.EndPoints.Select(oo => Tuple.Create(oo.Host, oo.Port)).ToList();
            var iPEndpointDiscovery = new IPEndpointDiscovery(options, ipEndPoints);

            EndpointStrategy.Instance.AddServiceDiscovery(iPEndpointDiscovery);
            return(EndpointStrategy.Instance);
        }