示例#1
0
        public async ValueTask <ConsulClient> GetClient()
        {
            ConsulClient result  = null;
            var          address = new List <AddressModel>();

            foreach (var addressModel in _config.Addresses)
            {
                _healthCheckService.Monitor(addressModel);
                var task = _healthCheckService.IsHealth(addressModel);
                if (!(task.IsCompletedSuccessfully ? task.Result : await task))
                {
                    continue;
                }
                address.Add(addressModel);
            }

            var vt = _consulAddressSelector.SelectAsync(new AddressSelectContext
            {
                Descriptor = new ServiceDescriptor {
                    Id = nameof(DefaultConsulClientProvider)
                },
                Address = address
            });
            var addr = vt.IsCompletedSuccessfully ? vt.Result : await vt;

            if (addr != null)
            {
                var ipAddress = addr as IpAddressModel;
                result = _consulClients.GetOrAdd(ipAddress, new ConsulClient(config =>
                {
                    config.Address = new Uri($"http://{ipAddress.Ip}:{ipAddress.Port}");
                }, null, h => { h.UseProxy = false; h.Proxy = null; }));
            }
            return(result);
        }
示例#2
0
        public async ValueTask <IpAddressModel> FindServiceEntry(string serverName)
        {
            var client = _consulClientProvider.GetConsulClient();

            ServiceEntry[] healths = _dictionary.GetOrAdd(serverName, (await client.Health.Service(serverName, "", true)).Response);

            var ipAddressModels = new List <AddressModel>();

            healths.ToList().ForEach(service =>
            {
                ipAddressModels.Add(new IpAddressModel()
                {
                    Ip   = service.Service.Address,
                    Port = service.Service.Port
                });
            });

            var ipAddressModel = await _consulAddressSelector.SelectAsync(new AddressSelectContext()
            {
                Address = ipAddressModels,
                Name    = serverName
            });

            return(ipAddressModel as IpAddressModel);
        }
        public async Task <ConsulClient> GetClient()
        {
            ConsulClient result  = null;
            var          address = new List <AddressModel>();

            foreach (var addressModel in _config.Addresses)
            {
                await _healthCheckService.Monitor(addressModel);

                var isHealth = await _healthCheckService.IsHealth(addressModel);

                if (!isHealth)
                {
                    _logger.LogWarning($"服务注册中心地址{addressModel.ToString()}不健康。");
                    continue;
                }
                address.Add(addressModel);
            }
            if (!address.Any())
            {
                if (_logger.IsEnabled(Level.Warning))
                {
                    _logger.LogWarning($"找不到可用的注册中心地址。");
                }
                return(null);
            }

            var addr = await _consulAddressSelector.SelectAsync(new AddressSelectContext
            {
                Descriptor = new ServiceDescriptor {
                    Id = nameof(DefaultConsulClientProvider)
                },
                Address = address
            });

            if (addr != null)
            {
                var ipAddress = addr as IpAddressModel;
                result = _consulClients.GetOrAdd(ipAddress, new ConsulClient(config =>
                {
                    config.Address = new Uri($"http://{ipAddress.Ip}:{ipAddress.Port}");
                }, null, h => { h.UseProxy = false; h.Proxy = null; }));
            }
            return(result);
        }