Exemple #1
0
        internal Dtos.ServiceInfo ProcessServiceInfo(Dtos.ServiceInfo serviceInfo)
        {
            serviceInfoMap.TryGetValue(serviceInfo.GetKey(), out var oldService);

            if (serviceInfo.Hosts == null || !serviceInfo.Hosts.Any() || !serviceInfo.Validate())
            {
                return(oldService);
            }

            serviceInfoMap.AddOrUpdate(serviceInfo.GetKey(), serviceInfo, (x, y) => serviceInfo);

            bool changed = IsChangedServiceInfo(oldService, serviceInfo);

            if (string.IsNullOrWhiteSpace(serviceInfo.JsonFromServer))
            {
                serviceInfo.JsonFromServer = serviceInfo.ToJsonString();
            }

            if (changed)
            {
                _logger?.LogInformation("current ips:({0}) service: {1} -> {2}", serviceInfo.IpCount(), serviceInfo.GetKey(), serviceInfo.Hosts.ToJsonString());

                if (_notifier != null)
                {
                    _notifier.OnEvent(new InstancesChangeEvent(serviceInfo.Name, serviceInfo.GroupName, serviceInfo.Clusters, serviceInfo.Hosts));
                }

                DiskCache.WriteAsync(serviceInfo, cacheDir)
                .ConfigureAwait(false).GetAwaiter().GetResult();
            }

            return(serviceInfo);
        }
Exemple #2
0
        public ServiceInfoHolder(ILogger logger, string @namespace, NacosSdkOptions nacosOptions, InstancesChangeNotifier notifier = null)
        {
            this._logger   = logger;
            this._notifier = notifier;

            InitCacheDir(@namespace);

            if (IsLoadCacheAtStart(nacosOptions))
            {
                var data = DiskCache.ReadAsync(this.cacheDir).ConfigureAwait(false).GetAwaiter().GetResult();
                this.serviceInfoMap = new ConcurrentDictionary <string, Dtos.ServiceInfo>(data);
            }
            else
            {
                this.serviceInfoMap = new ConcurrentDictionary <string, Dtos.ServiceInfo>();
            }

            this.failoverReactor = new FailoverReactor(this, cacheDir);
        }