/// <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)); }
/// <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); }
/// <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)); } }
/// <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)); }
public ClientCallInvoker( GrpcClientOptions options, IEndpointStrategy strategy, Func <List <ServerCallInvoker>, ServerCallInvoker> callInvokers = default) { _options = options; _strategy = strategy; _callInvokers = callInvokers; }
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; }
/// <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(); } }
/// <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); } }
public GrpcClientFactory(GrpcClientOptions options) { _options = new GrpcClientOptions <T>(options); InitOptions(); }
public GrpcClientFactory(IOptions <GrpcClientOptions <T> > options) { _options = options?.Value; InitOptions(); }
/// <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); }
public GrpcClientFactory(IOptions <GrpcClientOptions <T> > options = null, IClientTracer tracer = null, IOptions <GrpcClientOptions> grpcOptions = null) { _options = options?.Value; _tracer = tracer; _interceptors = grpcOptions?.Value?.Interceptors; }
public GrpcClientFactory(IOptions <GrpcClientOptions <T> > options = null, IClientTracer tracer = null) { _options = options?.Value; _tracer = tracer; }
/// <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))); }