Exemplo n.º 1
0
        public Nacos.V2.Naming.Dtos.ServiceInfo GetService(string key)
        {
            if (!serviceMap.TryGetValue(key, out var serviceInfo))
            {
                serviceInfo      = new Nacos.V2.Naming.Dtos.ServiceInfo();
                serviceInfo.Name = key;
            }

            return(serviceInfo);
        }
Exemplo n.º 2
0
        public async Task RunFailoverFileRead()
        {
            var domMap = new ConcurrentDictionary <string, Nacos.V2.Naming.Dtos.ServiceInfo>();

            try
            {
                var cacheDir = new DirectoryInfo(_failoverDir);
                if (!cacheDir.Exists)
                {
                    try
                    {
                        cacheDir.Create();
                    }
                    catch (Exception)
                    {
                        throw new Exception($"failed to create cache dir: {_failoverDir}");
                    }
                }

                var files = cacheDir.GetFiles();
                if (files == null)
                {
                    return;
                }

                foreach (var file in files)
                {
                    if (file.Name.Equals(UtilAndComs.FAILOVER_SWITCH))
                    {
                        continue;
                    }

                    var dom = new Nacos.V2.Naming.Dtos.ServiceInfo(file.Name);

                    try
                    {
                        var dataString = await file.ReadFileAsync().ConfigureAwait(false);

                        using (StringReader reader = new (dataString))
                        {
                            var json = reader.ReadLine();

                            if (json.IsNotNullOrWhiteSpace())
                            {
                                try
                                {
                                    dom = json.ToObj <Nacos.V2.Naming.Dtos.ServiceInfo>();
                                }
                                catch (Exception e)
                                {
                                    _logger?.LogError(e, "[NA] error while parsing cached dom : {0}", json);
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                        // ignore
                    }

                    if (dom.Hosts != null && dom.Hosts.Any())
                    {
                        domMap[dom.GetKey()] = dom;
                    }
                }
            }
            catch (Exception ex)
            {
                _logger?.LogError(ex, "[NA] failed to read cache file");
            }

            if (domMap != null && domMap.Any())
            {
                serviceMap = domMap;
            }
        }