/// <summary> /// 通过http从server拉取所有配置到本地,尝试5次 /// </summary> public bool Load() { const int maxTry = 5; int tryCount = 0; while (tryCount < maxTry) { tryCount++; try { var op = new AgileHttp.RequestOptions() { Headers = new Dictionary <string, string>() { { "appid", AppId }, { "Authorization", GenerateBasicAuthorization(AppId, Secret) } } }; var apiUrl = $"{PickOneServer()}/api/config/app/{AppId}"; using (var result = AgileHttp.HTTP.Send(apiUrl, "GET", null, op)) { if (result.StatusCode == System.Net.HttpStatusCode.OK) { var respContent = result.GetResponseContent(); ReloadDataDict(respContent); WriteConfigsToLocal(respContent); Logger?.LogTrace("AgileConfig Client Loaded all the configs success from {0} , Try count: {1}.", apiUrl, tryCount); return(true); } else { //load remote configs err . var ex = result.Exception ?? new Exception("AgileConfig Client Load all the configs failed ."); throw ex; } } } catch (Exception ex) { if (tryCount == maxTry) { //load configs from local file var fileContent = ReadConfigsFromLocal(); if (!string.IsNullOrEmpty(fileContent)) { ReloadDataDict(fileContent); Logger?.LogTrace("AgileConfig Client load all configs from local file ."); return(true); } } Logger?.LogError(ex, "AgileConfig Client try to load all configs failed . TryCount: {0}", tryCount); } } return(false); }
/// <summary> /// 通过http从server拉取所有配置到本地 /// </summary> public bool Load() { int failCount = 0; var randomServer = new RandomServers(ServerNodes); while (!randomServer.IsComplete) { var url = randomServer.Next(); try { var op = new AgileHttp.RequestOptions() { Headers = new Dictionary <string, string>() { { "appid", AppId }, { "Authorization", GenerateBasicAuthorization(AppId, Secret) } } }; var apiUrl = $"{url}/api/config/app/{AppId}"; using (var result = AgileHttp.HTTP.Send(apiUrl, "GET", null, op)) { if (result.StatusCode == System.Net.HttpStatusCode.OK) { var respContent = result.GetResponseContent(); ReloadDataDictFromContent(respContent); WriteConfigsToLocal(respContent); _isLoadFromLocal = false; Logger?.LogTrace("AgileConfig Client Loaded all the configs success from {0} , Try count: {1}.", apiUrl, failCount); return(true); } else { //load remote configs err . var ex = result.Exception ?? new Exception("AgileConfig Client Load all the configs failed ."); throw ex; } } } catch { failCount++; } } if (failCount == randomServer.ServerCount) { LoadConfigsFromLoacl(); } return(false); }