internal HealthStatus AddHeartbeatStatus(IEurekaClientConfig clientConfig, IEurekaInstanceConfig instanceConfig, HealthCheckResult result, long lastGoodHeartbeatTimeTicks)
        {
            if (clientConfig != null && clientConfig.ShouldRegisterWithEureka)
            {
                var lastGoodHeartbeatPeriod = GetLastGoodHeartbeatTimePeriod(lastGoodHeartbeatTimeTicks);
                if (lastGoodHeartbeatPeriod <= 0)
                {
                    result.Details.Add("heartbeat", "Not yet successfully connected");
                    result.Details.Add("heartbeatStatus", HealthStatus.UNKNOWN.ToString());
                    result.Details.Add("heartbeatTime", "UNKNOWN");
                    return(HealthStatus.UNKNOWN);
                }
                else if (lastGoodHeartbeatPeriod > ((instanceConfig.LeaseRenewalIntervalInSeconds * TimeSpan.TicksPerSecond) * 2))
                {
                    result.Details.Add("heartbeat", "Reporting failures connecting");
                    result.Details.Add("heartbeatStatus", HealthStatus.DOWN.ToString());
                    result.Details.Add("heartbeatTime", new DateTime(lastGoodHeartbeatTimeTicks).ToString("s"));
                    result.Details.Add("heartbeatFailures", lastGoodHeartbeatPeriod / (instanceConfig.LeaseRenewalIntervalInSeconds * TimeSpan.TicksPerSecond));
                    return(HealthStatus.DOWN);
                }

                result.Details.Add("heartbeat", "Successful");
                result.Details.Add("heartbeatStatus", HealthStatus.UP.ToString());
                result.Details.Add("heartbeatTime", new DateTime(lastGoodHeartbeatTimeTicks).ToString("s"));
                return(HealthStatus.UP);
            }

            result.Details.Add("heartbeatStatus", "Not registering");
            return(HealthStatus.UNKNOWN);
        }
        protected EurekaDiscoveryClientBase(EurekaClientOptions clientOptions, EurekaInstanceOptions instOptions, IEurekaHttpClient httpClient, IApplicationLifetime lifeCycle = null, ILoggerFactory logFactory = null)
        {
            if (clientOptions == null)
            {
                throw new ArgumentNullException(nameof(clientOptions));
            }

            ClientConfig = clientOptions;
            InstConfig   = instOptions;

            if (InstConfig == null)
            {
                DiscoveryManager.Instance.Initialize(ClientConfig, httpClient, logFactory);
            }
            else
            {
                ConfigureInstanceIfNeeded(InstConfig);
                DiscoveryManager.Instance.Initialize(ClientConfig, InstConfig, httpClient, logFactory);
            }

            if (lifeCycle != null)
            {
                lifeCycle.ApplicationStopping.Register(() => { ShutdownAsync(); });
            }

            Client = DiscoveryManager.Instance.Client;
        }
Beispiel #3
0
 public EurekaHttpClient(IOptionsMonitor <EurekaClientOptions> config, IEurekaDiscoveryClientHandlerProvider handlerProvider = null, ILoggerFactory logFactory = null)
 {
     _config          = null;
     _configOptions   = config ?? throw new ArgumentNullException(nameof(config));
     _handlerProvider = handlerProvider;
     Initialize(new Dictionary <string, string>(), logFactory);
 }
        internal HealthStatus AddFetchStatus(IEurekaClientConfig clientConfig, HealthCheckResult result, long lastGoodFetchTimeTicks)
        {
            if (clientConfig != null && clientConfig.ShouldFetchRegistry)
            {
                var lastGoodFetchPeriod = GetLastGoodRegistryFetchTimePeriod(lastGoodFetchTimeTicks);
                if (lastGoodFetchPeriod <= 0)
                {
                    result.Details.Add("fetch", "Not yet successfully connected");
                    result.Details.Add("fetchStatus", HealthStatus.UNKNOWN.ToString());
                    result.Details.Add("fetchTime", "UNKNOWN");
                    return(HealthStatus.UNKNOWN);
                }
                else if (lastGoodFetchPeriod > ((clientConfig.RegistryFetchIntervalSeconds * TimeSpan.TicksPerSecond) * 2))
                {
                    result.Details.Add("fetch", "Reporting failures connecting");
                    result.Details.Add("fetchStatus", HealthStatus.DOWN.ToString());
                    result.Details.Add("fetchTime", new DateTime(lastGoodFetchTimeTicks).ToString("s"));
                    result.Details.Add("fetchFailures", lastGoodFetchPeriod / (clientConfig.RegistryFetchIntervalSeconds * TimeSpan.TicksPerSecond));
                    return(HealthStatus.DOWN);
                }

                result.Details.Add("fetch", "Successful");
                result.Details.Add("fetchStatus", HealthStatus.UP.ToString());
                result.Details.Add("fetchTime", new DateTime(lastGoodFetchTimeTicks).ToString("s"));
                return(HealthStatus.UP);
            }

            result.Details.Add("fetchStatus", "Not fetching");
            return(HealthStatus.UNKNOWN);
        }
Beispiel #5
0
        protected virtual HttpClient GetHttpClient(IEurekaClientConfig config)
        {
            if (_client != null)
            {
                return(_client);
            }

            HttpClient client = null;

#if NET451
            client = new HttpClient();
#else
            if (config != null && !config.ValidateCertificates)
            {
                var handler = new HttpClientHandler();
                handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
                client = new HttpClient(handler);
            }
            else
            {
                client = new HttpClient();
            }
#endif

            if (config != null)
            {
                client.Timeout = new TimeSpan(0, 0, config.EurekaServerConnectTimeoutSeconds);
            }

            return(client);
        }
Beispiel #6
0
        protected virtual HttpClient GetHttpClient(IEurekaClientConfig config)
        {
            if (_client != null)
            {
                return(_client);
            }

            HttpClient client = null;

#if NET451
            client = new HttpClient();
#else
            // TODO: For coreclr, disabling certificate validation only works on windows platform
            // https://github.com/dotnet/corefx/issues/4476
            if (config != null && !config.ValidateCertificates)
            {
                var handler = new WinHttpHandler();
                handler.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
                return(new HttpClient(handler));
            }
            else
            {
                client = new HttpClient();
            }
#endif

            if (config != null)
            {
                client.Timeout = new TimeSpan(0, 0, config.EurekaServerConnectTimeoutSeconds);
            }

            return(client);
        }
        private HttpClient GetHttpClient(IEurekaClientConfig config)
        {
            if (_client != null)
            {
                return(_client);
            }

            return(new HttpClient());
        }
 public LookupClient(IEurekaClientConfig clientConfig, IEurekaHttpClient httpClient = null, ILoggerFactory logFactory = null)
     : base(clientConfig, httpClient, logFactory)
 {
     if (_cacheRefreshTimer != null)
     {
         _cacheRefreshTimer.Dispose();
         _cacheRefreshTimer = null;
     }
 }
Beispiel #9
0
 public void Initialize(IEurekaClientConfig clientConfig, IEurekaHttpClient httpClient, ILoggerFactory logFactory = null)
 {
     if (clientConfig == null)
     {
         throw new ArgumentNullException(nameof(clientConfig));
     }
     _logger = logFactory?.CreateLogger<DiscoveryManager>();
     ClientConfig = clientConfig;
     Client = new DiscoveryClient(clientConfig, httpClient, logFactory);
 }
        public virtual void Initialize(IEurekaClientConfig clientConfig, IEurekaInstanceConfig instanceConfig, IEurekaHttpClient httpClient, ILoggerFactory logFactory = null)
        {
            _logger        = logFactory?.CreateLogger <DiscoveryManager>();
            ClientConfig   = clientConfig ?? throw new ArgumentNullException(nameof(clientConfig));
            InstanceConfig = instanceConfig ?? throw new ArgumentNullException(nameof(instanceConfig));

            if (ApplicationInfoManager.Instance.InstanceInfo == null)
            {
                ApplicationInfoManager.Instance.Initialize(instanceConfig, logFactory);
            }

            Client = new DiscoveryClient(clientConfig, httpClient, logFactory);
        }
Beispiel #11
0
        internal IList <string> GetMonitoredApplications(IEurekaClientConfig clientConfig)
        {
            IList <string> configApps = GetApplicationsFromConfig(clientConfig);

            if (configApps != null)
            {
                return(configApps);
            }

            var regApps = _discoveryClient.Applications.GetRegisteredApplications();

            return(regApps.Select((app) => app.Name).ToList());
        }
Beispiel #12
0
        public DiscoveryClient(IEurekaClientConfig clientConfig, IEurekaHttpClient httpClient = null, ILoggerFactory logFactory = null)
            : this(ApplicationInfoManager.Instance, logFactory)
        {
            _config = clientConfig ?? throw new ArgumentNullException(nameof(clientConfig));

            _httpClient = httpClient;

            if (_httpClient == null)
            {
                _httpClient = new EurekaHttpClient(clientConfig, logFactory);
            }

            Initialize();
        }
Beispiel #13
0
        public EurekaHttpClient(IEurekaClientConfig config, IDictionary <string, string> headers, ILoggerFactory logFactory = null)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            if (headers == null)
            {
                throw new ArgumentNullException(nameof(headers));
            }
            _logger     = logFactory?.CreateLogger <EurekaHttpClient>();
            _config     = config;
            _serviceUrl = MakeServiceUrl(config.EurekaServerServiceUrls);
            _headers    = headers;
        }
Beispiel #14
0
        public DiscoveryClient(IEurekaClientConfig clientConfig, IEurekaHttpClient httpClient = null, ILoggerFactory logFactory = null)
        {
            if (clientConfig == null)
            {
                throw new ArgumentNullException(nameof(clientConfig));
            }

            _logger          = logFactory?.CreateLogger <DiscoveryClient>();
            ClientConfig     = clientConfig;
            _localRegionApps = new Applications();
            _localRegionApps.ReturnUpInstancesOnly = ClientConfig.ShouldFilterOnlyUpInstances;
            _httpClient = httpClient;

            if (_httpClient == null)
            {
                _httpClient = new EurekaHttpClient(clientConfig, logFactory);
            }

            if (!ClientConfig.ShouldRegisterWithEureka && !ClientConfig.ShouldFetchRegistry)
            {
                return;
            }

            if (ClientConfig.ShouldRegisterWithEureka)
            {
                var result = RegisterAsync();
                result.Wait();

                var intervalInMilli = ApplicationInfoManager.Instance.InstanceInfo.LeaseInfo.RenewalIntervalInSecs * 1000;
                _heartBeatTimer = StartTimer("HeartBeat", intervalInMilli, this.HeartBeatTaskAsync);
                if (ClientConfig.ShouldOnDemandUpdateStatusChange)
                {
                    ApplicationInfoManager.Instance.StatusChangedEvent += Instance_StatusChangedEvent;
                }
            }

            if (ClientConfig.ShouldFetchRegistry)
            {
                var result = FetchRegistryAsync(true);
                result.Wait();

                var intervalInMilli = ClientConfig.RegistryFetchIntervalSeconds * 1000;
                _cacheRefreshTimer = StartTimer("Query", intervalInMilli, this.CacheRefreshTaskAsync);
            }
        }
Beispiel #15
0
        internal IList <string> GetApplicationsFromConfig(IEurekaClientConfig clientConfig)
        {
            if (clientConfig is EurekaClientConfig config)
            {
                var monitoredApps = config.HealthMonitoredApps?.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (monitoredApps != null && monitoredApps.Length > 0)
                {
                    List <string> results = new List <string>();
                    foreach (var str in monitoredApps)
                    {
                        results.Add(str.Trim());
                    }

                    return(results);
                }
            }

            return(null);
        }
Beispiel #16
0
 public EurekaHttpClient(IEurekaClientConfig config, HttpClient client, ILoggerFactory logFactory = null)
     : this(config, new Dictionary <string, string>(), logFactory) => _client = client;
Beispiel #17
0
 public EurekaHttpClient(IEurekaClientConfig config, ILoggerFactory logFactory = null) : base(config, logFactory)
 {
 }
 public EurekaHttpClient(IEurekaClientConfig config, IDictionary <string, string> headers = null)
 {
     _config     = config ?? throw new ArgumentNullException(nameof(config));
     _headers    = headers;
     _serviceUrl = MakeServiceUrl(_config.EurekaServerServiceUrls);
 }
 public virtual void Initialize(IEurekaClientConfig clientConfig, IEurekaInstanceConfig instanceConfig, ILoggerFactory logFactory = null)
 {
     Initialize(clientConfig, instanceConfig, null, logFactory);
 }
 public virtual void Initialize(IEurekaClientConfig clientConfig, ILoggerFactory logFactory = null)
 {
     Initialize(clientConfig, (IEurekaHttpClient)null, logFactory);
 }