private ApolloConfig LoadApolloConfig() { string appId = m_configUtil.AppId; string cluster = m_configUtil.Cluster; string dataCenter = m_configUtil.DataCenter; int maxRetries = 2; Exception exception = null; IList <ServiceDTO> configServices = ConfigServices; string url = null; for (int i = 0; i < maxRetries; i++) { IList <ServiceDTO> randomConfigServices = new List <ServiceDTO>(configServices); randomConfigServices.Shuffle(); //Access the server which notifies the client first if (m_longPollServiceDto.ReadFullFence() != null) { randomConfigServices.Insert(0, m_longPollServiceDto.AtomicExchange(null)); } foreach (ServiceDTO configService in randomConfigServices) { url = AssembleQueryConfigUrl(configService.HomepageUrl, appId, cluster, m_namespace, dataCenter, m_remoteMessages.ReadFullFence(), m_configCache.ReadFullFence()); logger.Debug(string.Format("Loading config from {0}", url)); Com.Ctrip.Framework.Apollo.Util.Http.HttpRequest request = new Com.Ctrip.Framework.Apollo.Util.Http.HttpRequest(url); try { HttpResponse <ApolloConfig> response = m_httpUtil.DoGet <ApolloConfig>(request); if (response.StatusCode == 304) { logger.Debug("Config server responds with 304 HTTP status code."); return(m_configCache.ReadFullFence()); } ApolloConfig result = response.Body; logger.Debug( string.Format("Loaded config for {0}: {1}", m_namespace, result)); return(result); } catch (ApolloConfigStatusCodeException ex) { ApolloConfigStatusCodeException statusCodeException = ex; //config not found if (ex.StatusCode == 404) { string message = string.Format("Could not find config for namespace - appId: {0}, cluster: {1}, namespace: {2}, please check whether the configs are released in Apollo!", appId, cluster, m_namespace); statusCodeException = new ApolloConfigStatusCodeException(ex.StatusCode, message); } logger.Warn(statusCodeException); exception = statusCodeException; } catch (Exception ex) { logger.Warn(ex); exception = ex; } } Thread.Sleep(1000); //sleep 1 second } string fallbackMessage = string.Format("Load Apollo Config failed - appId: {0}, cluster: {1}, namespace: {2}, url: {3}", appId, cluster, m_namespace, url); throw new ApolloConfigException(fallbackMessage, exception); }
private async Task <ApolloConfig> LoadApolloConfig() { var appId = _options.AppId; var cluster = _options.Cluster; var dataCenter = _options.DataCenter; var configServices = await _serviceLocator.GetConfigServices(); Exception exception = null; string url = null; var notFound = false; for (var i = 0; i < 2; i++) { IList <ServiceDto> randomConfigServices = new List <ServiceDto>(configServices); randomConfigServices.Shuffle(); //Access the server which notifies the client first if (_longPollServiceDto.ReadFullFence() != null) { randomConfigServices.Insert(0, _longPollServiceDto.AtomicExchange(null)); } foreach (var configService in randomConfigServices) { url = AssembleQueryConfigUrl(configService.HomepageUrl, appId, cluster, Namespace, dataCenter, _remoteMessages.ReadFullFence(), _configCache.ReadFullFence()); Logger.Debug($"Loading config from {url}"); try { var response = await _httpUtil.DoGetAsync <ApolloConfig>(url); if (response.StatusCode == HttpStatusCode.NotModified) { Logger.Debug("Config server responds with 304 HTTP status code."); return(_configCache.ReadFullFence()); } var result = response.Body; Logger.Debug( $"Loaded config for {Namespace}: {result?.Configurations?.Count ?? 0}"); return(result); } catch (ApolloConfigStatusCodeException ex) { var statusCodeException = ex; //config not found if (ex.StatusCode == HttpStatusCode.NotFound) { notFound = true; var message = $"Could not find config for namespace - appId: {appId}, cluster: {cluster}, namespace: {Namespace}, please check whether the configs are released in Apollo!"; statusCodeException = new ApolloConfigStatusCodeException(ex.StatusCode, message); } Logger.Warn(statusCodeException); exception = statusCodeException; } catch (Exception ex) { Logger.Warn(ex); exception = ex; } } await Task.Delay(1000); //sleep 1 second } if (notFound) { return(null); } var fallbackMessage = $"Load Apollo Config failed - appId: {appId}, cluster: {cluster}, namespace: {Namespace}, url: {url}"; throw new ApolloConfigException(fallbackMessage, exception); }