Ejemplo n.º 1
0
        protected virtual async Task <EurekaHttpResponse <InstanceInfo> > DoGetInstanceAsync(string path)
        {
            var        requestUri = GetRequestUri(_serviceUrl + path);
            var        request    = GetRequestMessage(HttpMethod.Get, requestUri);
            HttpClient client     = GetHttpClient(_config);

#if NET452
            // If certificate validation is disabled, inject a callback to handle properly
            RemoteCertificateValidationCallback prevValidator = null;
            if (!_config.ValidateCertificates)
            {
                prevValidator = ServicePointManager.ServerCertificateValidationCallback;
                ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
            }
#endif
            try
            {
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    Stream stream = await response.Content.ReadAsStreamAsync();

                    JsonInstanceInfoRoot jroot = JsonInstanceInfoRoot.Deserialize(stream);

                    InstanceInfo infoResp = null;
                    if (jroot != null)
                    {
                        infoResp = InstanceInfo.FromJsonInstance(jroot.Instance);
                    }
                    _logger?.LogDebug("DoGetInstanceAsync {0}, status: {1}, instanceInfo: {2}",
                                      requestUri.ToString(), response.StatusCode, ((infoResp != null) ? infoResp.ToString() : "null"));
                    EurekaHttpResponse <InstanceInfo> resp = new EurekaHttpResponse <InstanceInfo>(response.StatusCode, infoResp);
                    resp.Headers = response.Headers;
                    return(resp);
                }
            }
            catch (Exception e)
            {
                _logger?.LogError("DoGetInstanceAsync Exception: {0}", e);
                throw;
            }

            finally
            {
                DisposeHttpClient(client);
#if NET452
                ServicePointManager.ServerCertificateValidationCallback = prevValidator;
#endif
            }
        }
        protected async Task <EurekaHttpResponse <InstanceInfo> > DoGetInstanceAsync(string path)
        {
            var        requestUri = GetRequestUri(_serviceUrl + path);
            var        request    = GetRequestMessage(HttpMethod.Get, requestUri);
            HttpClient client     = GetHttpClient(_config);

            try
            {
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    Stream stream = await response.Content.ReadAsStreamAsync();

                    JsonInstanceInfoRoot jroot = JsonInstanceInfoRoot.Deserialize(stream);

                    InstanceInfo infoResp = null;
                    if (jroot != null)
                    {
                        infoResp = InstanceInfo.FromJsonInstance(jroot.Instance);
                    }

                    Trace.TraceInformation("DoGetInstanceAsync {0}, status: {1}, instanceInfo: {2}", requestUri.ToString(), response.StatusCode, (infoResp != null) ? infoResp.ToString() : "null");
                    EurekaHttpResponse <InstanceInfo> resp = new EurekaHttpResponse <InstanceInfo>(response.StatusCode, infoResp)
                    {
                        Headers = response.Headers
                    };
                    return(resp);
                }
            }
            catch (Exception e)
            {
                Trace.TraceError("DoGetInstanceAsync Exception: {0}", e);
                throw;
            }
            finally
            {
                DisposeHttpClient(client);
            }
        }
Ejemplo n.º 3
0
        public void FromJsonInstance_Correct()
        {
            JsonInstanceInfo jinfo = new JsonInstanceInfo()
            {
                InstanceId           = "InstanceId",
                AppName              = "AppName",
                AppGroupName         = "AppGroupName",
                IpAddr               = "IpAddr",
                Sid                  = "Sid",
                Port                 = new JsonInstanceInfo.JsonPortWrapper(true, 100),
                SecurePort           = new JsonInstanceInfo.JsonPortWrapper(false, 100),
                HomePageUrl          = "HomePageUrl",
                StatusPageUrl        = "StatusPageUrl",
                HealthCheckUrl       = "HealthCheckUrl",
                SecureHealthCheckUrl = "SecureHealthCheckUrl",
                VipAddress           = "VipAddress",
                SecureVipAddress     = "SecureVipAddress",
                CountryId            = 1,
                DataCenterInfo       = new JsonInstanceInfo.JsonDataCenterInfo("", "MyOwn"),
                HostName             = "HostName",
                Status               = InstanceStatus.DOWN,
                OverriddenStatus     = InstanceStatus.OUT_OF_SERVICE,
                LeaseInfo            = new JsonLeaseInfo()
                {
                    RenewalIntervalInSecs      = 1,
                    DurationInSecs             = 2,
                    RegistrationTimestamp      = 1457973741708,
                    LastRenewalTimestamp       = 1457973741708,
                    LastRenewalTimestampLegacy = 1457973741708,
                    EvictionTimestamp          = 1457973741708,
                    ServiceUpTimestamp         = 1457973741708
                },
                IsCoordinatingDiscoveryServer = false,
                Metadata = new Dictionary <string, string>()
                {
                    { "@class", "java.util.Collections$EmptyMap" }
                },
                LastUpdatedTimestamp = 1457973741708,
                LastDirtyTimestamp   = 1457973741708,
                Actiontype           = ActionType.ADDED,
                AsgName = "AsgName"
            };

            InstanceInfo info = InstanceInfo.FromJsonInstance(jinfo);

            Assert.NotNull(info);

            // Verify
            Assert.Equal("InstanceId", info.InstanceId);
            Assert.Equal("AppName", info.AppName);
            Assert.Equal("AppGroupName", info.AppGroupName);
            Assert.Equal("IpAddr", info.IpAddr);
            Assert.Equal("Sid", info.Sid);
            Assert.Equal(100, info.Port);
            Assert.True(info.IsUnsecurePortEnabled);
            Assert.Equal(100, info.SecurePort);
            Assert.False(info.IsSecurePortEnabled);
            Assert.Equal("HomePageUrl", info.HomePageUrl);
            Assert.Equal("StatusPageUrl", info.StatusPageUrl);
            Assert.Equal("HealthCheckUrl", info.HealthCheckUrl);
            Assert.Equal("SecureHealthCheckUrl", info.SecureHealthCheckUrl);
            Assert.Equal("VipAddress", info.VipAddress);
            Assert.Equal("SecureVipAddress", info.SecureVipAddress);
            Assert.Equal(1, info.CountryId);
            Assert.Equal("MyOwn", info.DataCenterInfo.Name.ToString());
            Assert.Equal("HostName", info.HostName);
            Assert.Equal(InstanceStatus.DOWN, info.Status);
            Assert.Equal(InstanceStatus.OUT_OF_SERVICE, info.OverriddenStatus);
            Assert.NotNull(info.LeaseInfo);
            Assert.Equal(1, info.LeaseInfo.RenewalIntervalInSecs);
            Assert.Equal(2, info.LeaseInfo.DurationInSecs);
            Assert.Equal(635935705417080000L, info.LeaseInfo.RegistrationTimestamp);
            Assert.Equal(635935705417080000L, info.LeaseInfo.LastRenewalTimestamp);
            Assert.Equal(635935705417080000L, info.LeaseInfo.LastRenewalTimestampLegacy);
            Assert.Equal(635935705417080000L, info.LeaseInfo.EvictionTimestamp);
            Assert.Equal(635935705417080000L, info.LeaseInfo.ServiceUpTimestamp);
            Assert.False(info.IsCoordinatingDiscoveryServer);
            Assert.NotNull(info.Metadata);
            Assert.Equal(0, info.Metadata.Count);
            Assert.Equal(635935705417080000L, info.LastUpdatedTimestamp);
            Assert.Equal(635935705417080000L, info.LastDirtyTimestamp);
            Assert.Equal(ActionType.ADDED, info.Actiontype);
            Assert.Equal("AsgName", info.AsgName);
        }
Ejemplo n.º 4
0
        public virtual async Task <EurekaHttpResponse <InstanceInfo> > SendHeartBeatAsync(string appName, string id, InstanceInfo info, InstanceStatus overriddenStatus)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            if (string.IsNullOrEmpty(appName))
            {
                throw new ArgumentException(nameof(appName));
            }

            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException(nameof(id));
            }

            var queryArgs = new Dictionary <string, string>()
            {
                { "status", info.Status.ToString() },
                { "lastDirtyTimestamp", DateTimeConversions.ToJavaMillis(new DateTime(info.LastDirtyTimestamp, DateTimeKind.Utc)).ToString() }
            };

            if (overriddenStatus != InstanceStatus.UNKNOWN)
            {
                queryArgs.Add("overriddenstatus", overriddenStatus.ToString());
            }

            HttpClient client     = GetHttpClient(_config);
            var        requestUri = GetRequestUri(_serviceUrl + "apps/" + info.AppName + "/" + id, queryArgs);
            var        request    = GetRequestMessage(HttpMethod.Put, requestUri);

#if NET451
            // If certificate validation is disabled, inject a callback to handle properly
            RemoteCertificateValidationCallback prevValidator = null;
            if (!_config.ValidateCertificates)
            {
                prevValidator = ServicePointManager.ServerCertificateValidationCallback;
                ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
            }
#endif
            try
            {
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    Stream stream = await response.Content.ReadAsStreamAsync();

                    JsonInstanceInfo jinfo = JsonInstanceInfo.Deserialize(stream);



                    InstanceInfo infoResp = null;
                    if (jinfo != null)
                    {
                        infoResp = InstanceInfo.FromJsonInstance(jinfo);
                    }

                    _logger?.LogDebug("SendHeartbeatAsync {0}, status: {1}, instanceInfo: {2}",
                                      requestUri.ToString(), response.StatusCode, ((infoResp != null) ? infoResp.ToString() : "null"));
                    EurekaHttpResponse <InstanceInfo> resp = new EurekaHttpResponse <InstanceInfo>(response.StatusCode, infoResp);
                    resp.Headers = response.Headers;
                    return(resp);
                }
            }
            catch (Exception e)
            {
                _logger?.LogError("SendHeartbeatAsync Exception:", e);
                throw;
            }
#if NET451
            finally
            {
                ServicePointManager.ServerCertificateValidationCallback = prevValidator;
            }
#endif
        }
        public async Task <EurekaHttpResponse <InstanceInfo> > SendHeartBeatAsync(string appName, string id, InstanceInfo info, InstanceStatus overriddenStatus)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            if (string.IsNullOrEmpty(appName))
            {
                throw new ArgumentException(nameof(appName));
            }

            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException(nameof(id));
            }

            var queryArgs = new Dictionary <string, string>()
            {
                { "status", info.Status.ToString() },
                { "lastDirtyTimestamp", DateTimeConversions.ToJavaMillis(new DateTime(info.LastDirtyTimestamp, DateTimeKind.Utc)).ToString() }
            };

            if (overriddenStatus != InstanceStatus.UNKNOWN)
            {
                queryArgs.Add("overriddenstatus", overriddenStatus.ToString());
            }

            HttpClient client     = GetHttpClient(_config);
            var        requestUri = GetRequestUri(_serviceUrl + "apps/" + Uri.EscapeDataString(info.AppName) + "/" + Uri.EscapeDataString(id), queryArgs);
            var        request    = GetRequestMessage(HttpMethod.Put, requestUri);


            try
            {
                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    Stream stream = await response.Content.ReadAsStreamAsync();

                    JsonInstanceInfo jinfo = JsonInstanceInfo.Deserialize(stream);

                    InstanceInfo infoResp = null;
                    if (jinfo != null)
                    {
                        infoResp = InstanceInfo.FromJsonInstance(jinfo);
                    }

                    Trace.TraceInformation(
                        "SendHeartbeatAsync {0}, status: {1}, instanceInfo: {2}",
                        requestUri.ToString(),
                        response.StatusCode,
                        (infoResp != null) ? infoResp.ToString() : "null");
                    EurekaHttpResponse <InstanceInfo> resp = new EurekaHttpResponse <InstanceInfo>(response.StatusCode, infoResp)
                    {
                        Headers = response.Headers
                    };
                    return(resp);
                }
            }
            catch (Exception e)
            {
                Trace.TraceError("SendHeartbeatAsync Exception: {0}", e);
                throw;
            }
            finally
            {
                DisposeHttpClient(client);
            }
        }