コード例 #1
0
        /// <summary>
        /// 解析服务地址。
        /// </summary>
        /// <param name="serviceId">服务Id。</param>
        /// <returns>服务地址模型。</returns>
        /// 1.从字典中拿到serviceroute对象
        /// 2.从字典中拿到服务描述符集合
        /// 3.获取或添加serviceroute
        /// 4.添加服务id到白名单
        /// 5.根据服务描述符得到地址并判断地址是否是可用的(地址应该是多个)
        /// 6.添加到集合中
        /// 7.拿到服务命今
        /// 8.根据负载分流策略拿到一个选择器
        /// 9.返回addressmodel
        public async ValueTask <AddressModel> Resolver(string serviceId, string item)
        {
            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug($"准备为服务id:{serviceId},解析可用地址。");
            }

            var serviceRouteTask = _serviceRouteProvider.Locate(serviceId);
            var serviceRoute     = serviceRouteTask.IsCompletedSuccessfully ? serviceRouteTask.Result : await serviceRouteTask;

            if (serviceRoute == null)
            {
                if (_logger.IsEnabled(LogLevel.Warning))
                {
                    _logger.LogWarning($"根据服务id:{serviceId},找不到相关服务信息。");
                }
                return(null);
            }
            _serviceHeartbeatManager.AddWhitelist(serviceId);
            var address = new List <AddressModel>();

            foreach (var addressModel in serviceRoute.Address)
            {
                _healthCheckService.Monitor(addressModel);
                var task = _healthCheckService.IsHealth(addressModel);
                if (!(task.IsCompletedSuccessfully ? task.Result : await task))
                {
                    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()))}。");
            }
            var vtCommand       = _commandProvider.GetCommand(serviceId);
            var command         = vtCommand.IsCompletedSuccessfully ? vtCommand.Result : await vtCommand;
            var addressSelector = _addressSelectors[command.ShuntStrategy.ToString()];

            var vt = addressSelector.SelectAsync(new AddressSelectContext
            {
                Descriptor = serviceRoute.ServiceDescriptor,
                Address    = address,
                Item       = item
            });

            return(vt.IsCompletedSuccessfully ? vt.Result : await vt);
        }
コード例 #2
0
        public async Task <IpAddressModel> Locate(string key, string routePath)
        {
            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,
                });

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

            return(ipAddress);
        }
コード例 #3
0
        /// <summary>
        /// 解析服务地址。
        /// </summary>
        /// <param name="serviceId">服务Id。</param>
        /// <returns>服务地址模型。</returns>
        /// 1.从字典中拿到serviceroute对象
        /// 2.从字典中拿到服务描述符集合
        /// 3.获取或添加serviceroute
        /// 4.添加服务id到白名单
        /// 5.根据服务描述符得到地址并判断地址是否是可用的(地址应该是多个)
        /// 6.添加到集合中
        /// 7.拿到服务命今
        /// 8.根据负载分流策略拿到一个选择器
        /// 9.返回addressmodel
        public async Task <AddressModel> Resolver(string serviceId, string item)
        {
            if (_logger.IsEnabled(LogLevel.Debug))
            {
                _logger.LogDebug($"准备为服务id:{serviceId},解析可用地址。");
            }

            var isFailoverInvoke = IsFailoverInvoke();
            var serviceRoute     = await _serviceRouteProvider.Locate(serviceId, !isFailoverInvoke);

            if (serviceRoute == null)
            {
                throw new CPlatformException($"根据服务id:{serviceId},找不到相关服务信息。【fromCache:{!isFailoverInvoke}】");
            }
            var address = await GetHealthAddress(serviceRoute);

            if (!address.Any())
            {
                throw new CPlatformException($"根据服务id:{serviceId},找不到可用的服务提供者的地址");
            }

            _serviceHeartbeatManager.AddWhitelist(serviceId);

            var command = await _commandProvider.GetCommand(serviceId);

            var addressSelector = _addressSelectors[command.ShuntStrategy.ToString()];

            var selectAddress = await addressSelector.SelectAsync(new AddressSelectContext
            {
                Descriptor = serviceRoute.ServiceDescriptor,
                Address    = address,
                Item       = item
            });

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

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

                descriptor = descriptors.FirstOrDefault(i => i.ServiceDescriptor.Id == serviceId);
                if (descriptor != null)
                {
                    _concurrent.GetOrAdd(serviceId, descriptor);
                    _serviceHeartbeatManager.AddWhitelist(serviceId);
                }
                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)
            {
                _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()))}。");
            }
            var command = await _commandProvider.GetCommand(serviceId);

            var addressSelector = _addressSelectors[command.ShuntStrategy.ToString()];

            return(await addressSelector.SelectAsync(new AddressSelectContext
            {
                Descriptor = descriptor.ServiceDescriptor,
                Address = address,
                Item = item
            }));
        }