using Common; using Infrastructure; using Nacos.V2; using Nacos.V2.DependencyInjection; public class RefreshService : BackgroundService { private readonly INacosNamingService _namingService; public RefreshService(INacosNamingService namingService) { _namingService = namingService; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { try { var options = App.OptionsSetting; foreach(var service in options.Services) { string serviceName = service.Name; var list = await _namingService.GetAllInstances(serviceName, "DEFAULT_GROUP"); var healthy = list.Where(h => h.Healthy && h.Enabled).ToList(); if (healthy.Count > 0) { foreach(var sub in healthy) { if(!Utils.FeignUrl.ContainsKey(serviceName)) { Utils.FeignUrl.Add(serviceName, "http://" + sub.Ip + ":" + sub.Port); } else { Utils.FeignUrl[serviceName] = "http://" + sub.Ip + ":" + sub.Port; } } } } } catch(Exception ex) { Utils.WriteLog(ex.ToString(), "实时同步nacos服务数据异常"); } await Task.Delay(10000, stoppingToken); } } }