private void LoadConfig() { var islocalcache = false; var localcachepath = System.IO.Path.Combine(AppContext.BaseDirectory, "localconfig.json"); try { var client = _httpClientFactory.CreateClient(); var serverUrl = _serviceLocator.GetConfigService(); // 配置请求地址 var url = AssembleQueryConfigUrl(serverUrl); // 死锁问题 var response = client.SendAsync(new HttpRequestMessage(HttpMethod.Get, url)).ConfigureAwait(false).GetAwaiter().GetResult(); if (!response.IsSuccessStatusCode) { _logger.LogError($"{_setting.AppId} config request error status {response.StatusCode}"); } if (response.IsSuccessStatusCode) { var configdto = _jsonHelper.DeserializeObject <HttpConfigResult>(response.Content.ReadAsStringAsync().Result); if (configdto.Version > _config.Version) { if (_config.KV == null) { _config.KV = new ConcurrentDictionary <string, string>(); } foreach (var kv in configdto.KV) { _config.KV.AddOrUpdate(kv.Key, kv.Value, (x, y) => kv.Value); } _config.AppName = configdto.AppName; _config.Version = configdto.Version; islocalcache = true; } _logger.LogInformation($"{_setting.AppId} loaded config {configdto}"); } // 本地缓存 if (islocalcache) { _logger.LogInformation($"配置中心配置信息写入本地文件:{localcachepath}"); string dir = System.IO.Path.GetDirectoryName(localcachepath); if (!System.IO.Directory.Exists(dir)) { System.IO.Directory.CreateDirectory(dir); } var json = _jsonHelper.SerializeObject(_config); System.IO.File.WriteAllText(localcachepath, json); } } catch (Exception ex) { _logger.LogError($"config load error from appid {_setting.AppId}", ex); if (System.IO.File.Exists(localcachepath)) { var json = System.IO.File.ReadAllText(localcachepath); _config = _jsonHelper.DeserializeObject <BucketConfig>(json); } _logger.LogInformation($"config load error from appid {_setting.AppId},local disk cache recovery success."); } }
/// <summary> /// 加载配置 /// </summary> public async Task LoadConfig(bool reload = false) { var islocalcache = false; var localcachepath = System.IO.Path.Combine(AppContext.BaseDirectory, "localconfig.json"); // 容灾配置文件地址 try { if (reload) { _config.Version = 0; // 重载 } var client = _httpClientFactory.CreateClient(); // 创建http请求 var serverUrl = _serviceLocator.GetConfigService(); // 配置请求地址 var url = AssembleQueryConfigUrl(serverUrl); var response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, url)); response.EnsureSuccessStatusCode(); var content = await response.Content.ReadAsStringAsync(); var configdto = _jsonHelper.DeserializeObject <HttpConfigResult>(content); // 请求成功并有更新配置 if (configdto.ErrorCode == "000000" && configdto.Version > _config.Version) { foreach (var kv in configdto.KV) { _config.KV.AddOrUpdate(kv.Key, kv.Value, (x, y) => kv.Value); } _config.AppName = configdto.AppName; _config.Version = configdto.Version; islocalcache = true; } _logger.LogInformation($"{_setting.AppId} loaded config {configdto}"); // 本地缓存 if (islocalcache) { _logger.LogInformation($"配置中心配置信息写入本地文件:{localcachepath}"); string dir = System.IO.Path.GetDirectoryName(localcachepath); if (!System.IO.Directory.Exists(dir)) { System.IO.Directory.CreateDirectory(dir); } var json = _jsonHelper.SerializeObject(_config); System.IO.File.WriteAllText(localcachepath, json); } } catch (Exception ex) { _logger.LogError(ex, $"config load error from appid {_setting.AppId}"); if (System.IO.File.Exists(localcachepath)) { var json = System.IO.File.ReadAllText(localcachepath); _config = _jsonHelper.DeserializeObject <BucketConfig>(json); } _logger.LogInformation($"config load error from appid {_setting.AppId},local disk cache recovery success."); } initialized = true; // 初始化成功 }