Example #1
0
        /// <summary>
        /// 解析服务地址。
        /// </summary>
        /// <param name="serviceId">服务Id。</param>
        /// <returns>服务地址模型。</returns>
        public async Task <AddressModel> Resolver(string serviceId)
        {
            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug($"准备为服务id:{serviceId},解析可用地址。");
            }

            var address = Parse(serviceId);

            var hasAddress = address.Any();

            if (!hasAddress)
            {
                if (_logger.IsEnabled(LogLevel.Warning))
                {
                    _logger.LogWarning($"根据服务id:{serviceId},找不到可用的地址。");
                }
                return(null);
            }

            if (_logger.IsEnabled(LogLevel.Information))
            {
                _logger.LogInformation($"根据服务id:{serviceId},找到以下可用地址:{string.Join(",", address.Select(i => i.ToString()))}。");
            }

            return(await _addressSelector.SelectAsync(new AddressSelectContext
            {
                Descriptor = new ServiceDescriptor()
                {
                    Id = serviceId
                },
                Address = address
            }));
        }
        /// <summary>
        /// 解析服务地址
        /// </summary>
        /// <param name="serviceId">服务Id</param>
        /// <returns>服务地址模型</returns>
        public async Task <AddressModel> Resolver(string serviceId)
        {
            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug($"准备为服务id:{serviceId},解析可用地址");
            }
            var descriptors = await _serviceRouteManager.GetRoutesAsync();

            var descriptor = descriptors.FirstOrDefault(i => i.ServiceDescriptor.Id == serviceId);

            if (descriptor == null)
            {
                if (_logger.IsEnabled(LogLevel.Warning))
                {
                    _logger.LogWarning($"根据服务id:{serviceId},找不到相关服务信息");
                }
                return(null);
            }

            var address = new List <AddressModel>();

            foreach (var addressModel in descriptor.Address)
            {
                await _healthCheckService.Monitor(addressModel);

                if (!await _healthCheckService.IsHealth(addressModel))
                {
                    continue;
                }

                address.Add(addressModel);
            }

            var hasAddress = address.Any();

            if (!hasAddress)
            {
                if (_logger.IsEnabled(LogLevel.Warning))
                {
                    _logger.LogWarning($"根据服务id:{serviceId},找不到可用的地址");
                }
                return(null);
            }

            if (_logger.IsEnabled(LogLevel.Information))
            {
                _logger.LogInformation($"根据服务id:{serviceId},找到以下可用地址:{string.Join(",", address.Select(i => i.ToString()))}");
            }

            return(await _addressSelector.SelectAsync(new AddressSelectContext
            {
                Descriptor = descriptor.ServiceDescriptor,
                Address = address
            }));
        }
Example #3
0
        public async Task <AddressModel> ResolverAsync(string serviceId)
        {
            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug($"正在准备为服务{serviceId}解析地址");
            }

            var serviceRoutes = await _manager.GetRoutesAsync();

            var serviceRoute = serviceRoutes.Where(s => s.ServiceDescriptor.Id == serviceId).FirstOrDefault();

            if (serviceRoute == null)
            {
                if (_logger.IsEnabled(LogLevel.Warning))
                {
                    _logger.LogWarning($"服务{serviceId}没有找到对应的服务");
                }
                return(null);
            }

            var address = new List <AddressModel>();

            foreach (var am in serviceRoute.Address)
            {
                await _healthChecksService.Monitor(am);

                if (!await _healthChecksService.IsHealth(am))
                {
                    continue;
                }
                address.Add(am);
            }

            if (!address.Any())
            {
                if (_logger.IsEnabled(LogLevel.Warning))
                {
                    _logger.LogWarning($"服务{serviceId}没有找到对应的服务");
                }
                return(null);
            }
            else
            {
                return(await _selector.SelectAsync(new AddressSelectorContext()
                {
                    Descriptor = serviceRoute.ServiceDescriptor,
                    Address = address
                }));
            }
        }
Example #4
0
        public async Task <string> Locate(string routePath, string key)
        {
            var route = await _serviceRouteProvider.SearchRoute(routePath);

            AddressModel result = new IpAddressModel();

            if (route != null)
            {
                result = await _addressSelector.SelectAsync(new AddressSelectContext()
                {
                    Address    = route.Address,
                    Descriptor = route.ServiceDescriptor,
                    HashCode   = _hashAlgorithm.Hash(key)
                });
            }
            return(result.ToString());
        }
Example #5
0
        public async Task <IpAddressModel> Locate(string routePath, string key)
        {
            var route = await _serviceRouteProvider.SearchRoute(routePath);

            AddressModel result = new IpAddressModel();

            if (route != null)
            {
                result = await _addressSelector.SelectAsync(new AddressSelectContext()
                {
                    Address    = route.Address,
                    Descriptor = route.ServiceDescriptor,
                    Item       = key,
                });
            }
            var ipAddress = result as IpAddressModel;

            return(ipAddress);
        }
        public async Task <IpAddressModel> Locate(string key, string routePath, HttpMethod httpMethod)
        {
            var route = await _serviceRouteProvider.SearchRoute(routePath, httpMethod.ToString());

            AddressModel result = new IpAddressModel();

            if (route != null)
            {
                result = await _addressSelector.SelectAsync(new AddressSelectContext()
                {
                    Address    = route.Address,
                    Descriptor = route.ServiceDescriptor,
                    Item       = key,
                });

                _serviceHeartbeatManager.AddWhitelist(route.ServiceDescriptor.Id);
            }
            var ipAddress = result as IpAddressModel;

            return(ipAddress);
        }
Example #7
0
        /// <summary>
        /// 解析服务地址。
        /// </summary>
        /// <param name="serviceId">服务Id。</param>
        /// <returns>服务地址模型。</returns>
        public async Task <AddressModel> Resolver(string serviceId)
        {
            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.Debug($"准备为服务id:{serviceId},解析可用地址。");
            }
            var descriptors = await _serviceRouteManager.GetRoutesAsync();

            var descriptor = descriptors.FirstOrDefault(i => i.ServiceDescriptor.Id == serviceId);

            if (descriptor == null)
            {
                if (_logger.IsEnabled(LogLevel.Warning))
                {
                    _logger.Warning($"根据服务id:{serviceId},找不到相关服务信息。");
                }
                return(null);
            }

            var hasAddress = descriptor.Address?.Any();

            if (!hasAddress.HasValue || !hasAddress.Value)
            {
                if (_logger.IsEnabled(LogLevel.Warning))
                {
                    _logger.Warning($"根据服务id:{serviceId},找不到可用的地址。");
                }
                return(null);
            }

            if (_logger.IsEnabled(LogLevel.Information))
            {
                _logger.Information($"根据服务id:{serviceId},找到以下可用地址:{string.Join(",", descriptor.Address.Select(i => i.ToString()))}。");
            }

            return(await _addressSelector.SelectAsync(new AddressSelectContext
            {
                ServiceRoute = descriptor
            }));
        }
        /// <summary>
        /// 解析服务地址。
        /// </summary>
        /// <param name="serviceId">服务Id。</param>
        /// <returns>服务地址模型。</returns>
        public async Task <AddressModel> Resolver(string serviceId, int hashCode)
        {
            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug($"准备为服务id:{serviceId},解析可用地址。");
            }
            ServiceRoute descriptor;

            _concurrent.TryGetValue(serviceId, out descriptor);
            if (descriptor == null)
            {
                var descriptors = await _serviceRouteManager.GetRoutesAsync();

                descriptor = descriptors.FirstOrDefault(i => i.ServiceDescriptor.Id == serviceId);
                if (descriptor != null)
                {
                    _concurrent.GetOrAdd(serviceId, descriptor);
                }
                else
                {
                    if (descriptor == null)
                    {
                        if (_logger.IsEnabled(LogLevel.Warning))
                        {
                            _logger.LogWarning($"根据服务id:{serviceId},找不到相关服务信息。");
                        }
                        return(null);
                    }
                }
            }

            var address = new List <AddressModel>();

            foreach (var addressModel in descriptor.Address)
            {
                await _healthCheckService.Monitor(addressModel);

                if (!await _healthCheckService.IsHealth(addressModel))
                {
                    continue;
                }

                address.Add(addressModel);
            }

            if (address.Count == 0)
            {
                if (_logger.IsEnabled(LogLevel.Warning))
                {
                    _logger.LogWarning($"根据服务id:{serviceId},找不到可用的地址。");
                }
                return(null);
            }

            if (_logger.IsEnabled(LogLevel.Information))
            {
                _logger.LogInformation($"根据服务id:{serviceId},找到以下可用地址:{string.Join(",", address.Select(i => i.ToString()))}。");
            }

            return(await _addressSelector.SelectAsync(new AddressSelectContext
            {
                Descriptor = descriptor.ServiceDescriptor,
                Address = address,
                HashCode = hashCode
            }));
        }