Beispiel #1
0
        private async Task Load()
        {
            var newConsulResult = await ConsulClient.GetEndPoints(DeploymentName).ConfigureAwait(false);
            lock (_lastResultLocker)
            {
                var oldConsulResult = _lastConsulResult;
                _lastConsulResult = newConsulResult;

                if (newConsulResult.Error != null)
                {
                    if (_firstTime)
                    {
                        _firstTime = false;
                        Result = newConsulResult;
                    }                
                    return;
                }
                
                var newEndPoints = newConsulResult
                    .EndPoints
                    .OrderBy(x => x.HostName)
                    .ThenBy(x => x.Port)
                    .ToArray();
                bool isEndPointChanged = false;
                if (newEndPoints.SequenceEqual(Result.EndPoints) == false)
                {
                    newConsulResult.EndPoints = newEndPoints;
                    Result = newConsulResult;

                    _log.Info(_ => _("Obtained new list endpoints for service from Consul", unencryptedTags: new
                    {
                        serviceName = DeploymentName,
                        endpoints = string.Join(", ", newEndPoints.Select(e => e.HostName + ':' + (e.Port?.ToString() ?? "")))
                    }));

                    isEndPointChanged = true;
                }

                bool isDeploymentDefinedChanged = IsDeploymentDefined(oldConsulResult) != IsDeploymentDefined(newConsulResult);


                if (!_firstTime && (isEndPointChanged || isDeploymentDefinedChanged))
                    EndPointsChanged?.Post(newConsulResult);


                _firstTime = false;

                _initialized = Task.FromResult(1);
            }
        }
        public void SetEndPoints(string endPoints)
        {
            EndPoints = new EndPoint[0];
            if (!string.IsNullOrWhiteSpace(endPoints))
            {
                EndPoints = endPoints.Split(',').Select(_ => _.Trim())
                            .Where(a => !string.IsNullOrWhiteSpace(a))
                            .Select(_ => new EndPoint {
                    HostName = _
                })
                            .ToArray();
            }

            EndPointsChanged.Post(new EndPointsResult {
                EndPoints = EndPoints
            });
            Task.Delay(100).Wait();
        }
        private async Task Load()
        {
            var lastConsulResult = await ConsulClient.GetEndPoints(DeploymentName).ConfigureAwait(false);

            lock (_lastResultLocker)
            {
                if (lastConsulResult.Error != null)
                {
                    _lastConsulResult = lastConsulResult;
                    return;
                }
                var newEndPoints = lastConsulResult
                                   .EndPoints
                                   .OrderBy(x => x.HostName)
                                   .ThenBy(x => x.Port)
                                   .ToArray();

                if (newEndPoints.SequenceEqual(EndPoints) == false)
                {
                    EndPoints = newEndPoints;

                    _log.Info(_ => _("Obtained new list endpoints for service from Consul", unencryptedTags: new
                    {
                        serviceName = DeploymentName,
                        endpoints   = string.Join(", ", EndPoints.Select(e => e.HostName + ':' + (e.Port?.ToString() ?? "")))
                    }));

                    if (!_firstTime || _lastConsulResult?.Error != null)
                    {
                        EndPointsChanged?.Post(lastConsulResult);
                    }
                }

                _firstTime        = false;
                _lastConsulResult = lastConsulResult;
                _initialized      = Task.FromResult(1);
            }
        }
Beispiel #4
0
 public virtual void ShutDown()
 {
     EndPointsChanged?.Complete();
 }