Example #1
0
        public async Task Get()
        {
            try
            {
                var islocalcache = false;
                var apiurl       = _httpUrlRepository.GetApiUrl();
                var client       = _httpClientFactory.CreateClient();
                var response     = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, apiurl));

                response.EnsureSuccessStatusCode();
                var content = await response.Content.ReadAsStringAsync();

                var output = _jsonHelper.DeserializeObject <ApiInfo>(content);
                if (output != null && output.Value != null && output.Value.Count > 0)
                {
                    _dataList.WriteFullFence(output.Value);
                    islocalcache = true;
                }
                if (islocalcache)
                {
                    _localDataRepository.Set(_dataList.ReadFullFence());
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"加载配置错误");
                _dataList.WriteFullFence(_localDataRepository.Get());
            }
        }
Example #2
0
        private void UpdateConfigServices()
        {
            lock (this)
            {
                string url = AssembleMetaServiceUrl();

                Com.Ctrip.Framework.Apollo.Util.Http.HttpRequest request = new Com.Ctrip.Framework.Apollo.Util.Http.HttpRequest(url);
                int       maxRetries = 5;
                Exception exception  = null;

                for (int i = 0; i < maxRetries; i++)
                {
                    try
                    {
                        HttpResponse <IList <ServiceDTO> > response = m_httpUtil.DoGet <IList <ServiceDTO> >(request);
                        IList <ServiceDTO> services = response.Body;
                        if (services == null || services.Count == 0)
                        {
                            continue;
                        }
                        m_configServices.WriteFullFence(services);
                        return;
                    }
                    catch (Exception ex)
                    {
                        logger.Warn(ex);
                        exception = ex;
                    }

                    Thread.Sleep(1000); //sleep 1 second
                }

                throw new ApolloConfigException(string.Format("Get config services failed from {0}", url), exception);
            }
        }
        private async Task Sync()
        {
            try
            {
                var previous = _configCache.ReadFullFence();
                var current  = await LoadApolloConfig();

                //reference equals means HTTP 304
                if (!ReferenceEquals(previous, current))
                {
                    Logger.Debug("Remote Config refreshed!");
                    _configCache.WriteFullFence(current);
                    FireRepositoryChange(Namespace, GetConfig());
                }

                if (!_resetEvent.IsSet)
                {
                    _resetEvent.Set();
                }
            }
            catch (Exception e)
            {
                Logger.Warn(e);
            }
        }
Example #4
0
        private async Task UpdateConfigServices(int times)
        {
            var url = AssembleMetaServiceUrl();

            Exception exception = null;

            for (var i = 0; i < times; i++)
            {
                try
                {
                    var response = await _httpUtil.DoGetAsync <IList <ServiceDto> >(url, 2000).ConfigureAwait(false);

                    var services = response.Body;
                    if (services == null || services.Count == 0)
                    {
                        continue;
                    }

                    _configServices.WriteFullFence(services);
                    return;
                }
                catch (Exception ex)
                {
                    Logger.Warn(ex);
                    exception = ex;
                }
            }

            throw new ApolloConfigException($"Get config services failed from {url}", exception);
        }
Example #5
0
 public void OnLongPollNotified(ServiceDTO longPollNotifiedServiceDto)
 {
     m_longPollServiceDto.WriteFullFence(longPollNotifiedServiceDto);
     m_executorService.QueueWorkItem(() =>
     {
         TrySync();
     });
 }
Example #6
0
 private void Initialize()
 {
     try
     {
         _configProperties.WriteFullFence(_configRepository.GetConfig());
     }
     catch (Exception ex)
     {
         Logger.Warn($"Init Apollo Local Config failed - namespace: {_namespace}", ex);
     }
     finally
     {
         //register the change listener no matter config repository is working or not
         //so that whenever config repository is recovered, config could get changed
         _configRepository.AddChangeListener(this);
     }
 }
 public void OnLongPollNotified(ServiceDTO longPollNotifiedServiceDto, ApolloNotificationMessages remoteMessages)
 {
     m_longPollServiceDto.WriteFullFence(longPollNotifiedServiceDto);
     m_remoteMessages.WriteFullFence(remoteMessages);
     m_executorService.QueueWorkItem(() =>
     {
         TrySync();
     });
 }
Example #8
0
 private void Initialize()
 {
     try
     {
         m_configProperties.WriteFullFence(m_configRepository.GetConfig());
     }
     catch (Exception ex)
     {
         logger.Warn(
             string.Format("Init Apollo Local Config failed - namespace: {0}, reason: {1}.",
                           m_namespace, ExceptionUtil.GetDetailMessage(ex)));
     }
     finally
     {
         //register the change listener no matter config repository is working or not
         //so that whenever config repository is recovered, config could get changed
         m_configRepository.AddChangeListener(this);
     }
 }
Example #9
0
        private async Task Sync(bool isFirst)
        {
            var previous = _configCache.ReadFullFence();
            var current  = await LoadApolloConfig(isFirst).ConfigureAwait(false);

            //reference equals means HTTP 304
            if (!ReferenceEquals(previous, current))
            {
                Logger.Debug("Remote Config refreshed!");
                _configCache.WriteFullFence(current);
                FireRepositoryChange(Namespace, GetConfig());
            }
        }
        public void OnLongPollNotified(ServiceDto longPollNotifiedServiceDto, ApolloNotificationMessages remoteMessages)
        {
            _longPollServiceDto.WriteFullFence(longPollNotifiedServiceDto);
            _remoteMessages.WriteFullFence(remoteMessages);

            ExecutorService.StartNew(async() =>
            {
                try
                {
                    await Sync();
                }
                catch (Exception ex)
                {
                    Logger.Warn($"Sync config failed, will retry. Repository {GetType()}, reason: {ex.GetDetailMessage()}");
                }
            });
        }
Example #11
0
        private async Task UpdateConfigServices()
        {
            await _semaphore.WaitAsync();

            try
            {
                var url = AssembleMetaServiceUrl();

                var       maxRetries = 5;
                Exception exception  = null;

                for (var i = 0; i < maxRetries; i++)
                {
                    try
                    {
                        var response = await _httpUtil.DoGetAsync <IList <ServiceDto> >(url);

                        var services = response.Body;
                        if (services == null || services.Count == 0)
                        {
                            continue;
                        }

                        _configServices.WriteFullFence(services);
                        return;
                    }
                    catch (Exception ex)
                    {
                        Logger.Warn(ex);
                        exception = ex;
                    }

                    await Task.Delay(1000); //sleep 1 second
                }

                throw new ApolloConfigException($"Get config services failed from {url}", exception);
            }
            finally
            {
                _semaphore.Release();
            }
        }
        protected override void Sync()
        {
            lock (this)
            {
                try
                {
                    ApolloConfig previous = m_configCache.ReadFullFence();
                    ApolloConfig current  = LoadApolloConfig();

                    //reference equals means HTTP 304
                    if (!object.ReferenceEquals(previous, current))
                    {
                        logger.Debug("Remote Config refreshed!");
                        m_configCache.WriteFullFence(current);
                        this.FireRepositoryChange(m_namespace, this.GetConfig());
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }