/// <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()); }
/// <summary> /// 获取配置 /// </summary> /// <param name="serverUrl"></param> /// <param name="appID"></param> /// <param name="env"></param> /// <returns></returns> public RemoteConfig GetConfigFromServer(string serverUrl, string appID, string env) { var url = TaskUrlHelper.GetConfigUrl(serverUrl, appID, env); for (int i = 0; i < _apolloConfig.MaxRetries; i++) { var remoteConfig = _httpHelper.DoGet <RemoteConfig>(new HttpRequest($"{url}&rnd={Environment.TickCount}")).Body; if (remoteConfig != null) { return(remoteConfig); } } return(null); }
/// <summary> /// 获取服务集合 /// </summary> /// <returns></returns> public ServiceConfigs GetServices() { var url = TaskUrlHelper.GetServiceUrl(_apolloConfig.ServerUrl); for (int i = 0; i < _apolloConfig.MaxRetries; i++) { var services = _httpHelper.DoGet <ServiceConfigs>(new HttpRequest(url)).Body; if (services != null && services.Any()) { return(services); } if (i == _apolloConfig.MaxRetries - 1) { throw new ApolloConfigException("ConfigConfirmTask.GetServices 已超出重试次数!"); } } return(null); }