public override string GetProperty(string key, string defaultValue) { // step 1: check system properties, i.e. -Dkey=value //TODO looks like .Net doesn't have such system property? string value = null; // step 2: check local cached properties file if (m_configProperties.ReadFullFence() != null) { value = m_configProperties.ReadFullFence().GetProperty(key); } /// <summary> /// step 3: check env variable, i.e. PATH=... /// normally system environment variables are in UPPERCASE, however there might be exceptions. /// so the caller should provide the key in the right case /// </summary> if (value == null) { value = System.Environment.GetEnvironmentVariable(key); } //TODO step 4: check properties file from classpath if (value == null && m_configProperties.ReadFullFence() == null) { logger.Warn(string.Format("Could not load config for namespace {0} from Apollo, please check whether the configs are released in Apollo! Return default value now!", m_namespace)); } return(value == null ? defaultValue : value); }
public override Properties GetConfig() { if (m_configCache.ReadFullFence() == null) { this.Sync(); } return(TransformApolloConfigToProperties(m_configCache.ReadFullFence())); }
/// <summary> /// Get the config service info from remote meta server. /// </summary> /// <returns> the services dto </returns> public IList <ServiceDTO> GetConfigServices() { if (m_configServices.ReadFullFence().Count == 0) { UpdateConfigServices(); } return(m_configServices.ReadFullFence()); }
public override Properties GetConfig() { if (_configCache.ReadFullFence() == null) { AsyncHelper.RunSync(SchedulePeriodicRefresh); } return(TransformApolloConfigToProperties(_configCache.ReadFullFence())); }
public override Properties GetConfig() { if (_syncException != null) { throw _syncException; } return(TransformApolloConfigToProperties(_configCache.ReadFullFence())); }
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()); } }
/// <summary> /// Get the config service info from remote meta server. /// </summary> /// <returns> the services dto </returns> public async Task <IList <ServiceDto> > GetConfigServices() { var services = _configServices.ReadFullFence(); if (services.Count == 0) { await UpdateConfigServices(); } services = _configServices.ReadFullFence(); if (services.Count == 0) { throw new ApolloConfigException("No available config service"); } return(services); }
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); }