Пример #1
0
        /// <summary>
        /// 获取阻塞式的配置数据
        /// </summary>
        /// <param name="appIDs"></param>
        /// <param name="cluster"></param>
        void GetApolloConfigs(string[] appIDs, string cluster = "default")
        {
            var tasks = new List <Task>();

            foreach (var serviceConfig in _serviceConfigs)
            {
                var task = Task.Run(() =>
                {
                    try
                    {
                        foreach (var appID in appIDs)
                        {
                            var url = TaskUrlHelper.GetLongPollingUrl(serviceConfig.HomePageUrl, appID, cluster, _notificationId);

                            for (int i = 0; i < _apolloConfig.MaxRetries; i++)
                            {
                                var response = _httpHelper.DoGet <LongPollings>(new HttpRequest(url, 600 * 1000));

                                if (response.StatusCode == 200 && response.Body != null)
                                {
                                    _getConfigTask.GetConfig(appIDs, serviceConfig, cluster);

                                    var res = response.Body.FirstOrDefault();

                                    if (res != null)
                                    {
                                        _notificationId = res.NotificationId;
                                    }

                                    break;
                                }
                                else if (response.StatusCode == 304)
                                {
                                    break;
                                }
                                else
                                {
                                    throw new ApolloConfigException($"url:{url},response.StatusCode:{response.StatusCode}");
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        OnError?.Invoke(new ApolloConfigException("apollo轮询配置中心出现异常", ex));
                        Thread.Sleep(10 * 1000);
                    }
                });

                tasks.Add(task);
            }
            Task.WaitAll(tasks.ToArray());
        }
Пример #2
0
        /// <summary>
        /// 开始
        /// </summary>
        public void Start()
        {
            try
            {
                if (_longPollingTask == null)
                {
                    _configConfirmTask = new ConfigConfirmTask(Config);

                    var serviceConfigs = _configConfirmTask.GetServices();

                    if (serviceConfigs != null && serviceConfigs.Any())
                    {
                        _apolloServiceConfigTask = new ApolloServiceConfigTask(Config);

                        foreach (var serviceConfig in serviceConfigs)
                        {
                            _apolloServiceConfigTask.GetConfig(_appIDs, serviceConfig, _cluster);
                        }
                    }
                    else
                    {
                        throw new Exception("获取配置服务 is Null or Empty!");
                    }

                    _longPollingTask          = new LongPollingTask(Config, serviceConfigs);
                    _longPollingTask.OnError += _longPollingTask_OnError;

                    _longPollingTask.Start(_appIDs, _cluster);
                }
            }
            catch (Exception ex)
            {
                OnError?.Invoke(new ApolloConfigException("Wenli.Config.ApolloClient 长轮询过程中出现异常,正在等待重试!", ex));

                Thread.Sleep(10 * 1000);

                Start();
            }
        }