public async Task <EurekaHttpResponse> RegisterAsync(InstanceInfo info)
        {
            var client     = GetHttpClient(_config);
            var requestUri = GetRequestUri(_serviceUrl + "apps/" + Uri.EscapeDataString(info.AppName));
            var request    = GetRequestMessage(HttpMethod.Post, requestUri);

            request.Content = GetRequestContent(new JsonInstanceInfoRoot(info.ToJsonInstance()));

            using (HttpResponseMessage response = await client.SendAsync(request))
            {
                Trace.TraceInformation("RegisterAsync {0}, status: {1}", requestUri.ToString(), response.StatusCode);
                EurekaHttpResponse resp = new EurekaHttpResponse(response.StatusCode)
                {
                    Headers = response.Headers
                };
                if (response.StatusCode == HttpStatusCode.InternalServerError)
                {
                    var jsonError = await response.Content.ReadAsStringAsync();

                    Trace.TraceInformation($"Something goes wrong in registering: {jsonError}");
                }

                return(resp);
            }
        }
Esempio n. 2
0
        public void ToJsonInstance_DefaultInstanceConfig_Correct()
        {
            EurekaInstanceConfig config = new EurekaInstanceConfig();
            InstanceInfo         info   = InstanceInfo.FromInstanceConfig(config);

            Assert.NotNull(info);

            JsonInstanceInfo jinfo = info.ToJsonInstance();

            // Verify
            Assert.Equal(config.GetHostName(false), jinfo.InstanceId);
            Assert.Equal(EurekaInstanceConfig.Default_Appname.ToUpperInvariant(), jinfo.AppName);
            Assert.Null(jinfo.AppGroupName);
            Assert.Equal(config.IpAddress, jinfo.IpAddr);
            Assert.Equal("na", jinfo.Sid);
            Assert.NotNull(jinfo.Port);
            Assert.Equal(80, jinfo.Port.Port);
            Assert.True(Boolean.Parse(jinfo.Port.Enabled));
            Assert.NotNull(jinfo.SecurePort);
            Assert.Equal(443, jinfo.SecurePort.Port);
            Assert.False(Boolean.Parse(jinfo.SecurePort.Enabled));
            Assert.Equal("http://" + config.GetHostName(false) + ":" + 80 + "/", jinfo.HomePageUrl);
            Assert.Equal("http://" + config.GetHostName(false) + ":" + 80 + "/Status", jinfo.StatusPageUrl);
            Assert.Equal("http://" + config.GetHostName(false) + ":" + 80 + "/healthcheck", jinfo.HealthCheckUrl);
            Assert.Null(jinfo.SecureHealthCheckUrl);
            Assert.Equal(config.GetHostName(false) + ":" + 80, jinfo.VipAddress);
            Assert.Equal(config.GetHostName(false) + ":" + 443, jinfo.SecureVipAddress);
            Assert.Equal(1, jinfo.CountryId);
            Assert.NotNull(jinfo.DataCenterInfo);
            Assert.Equal("MyOwn", jinfo.DataCenterInfo.Name);
            Assert.Equal("com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo", jinfo.DataCenterInfo.ClassName);
            Assert.Equal(config.GetHostName(false), jinfo.HostName);
            Assert.Equal(InstanceStatus.STARTING, jinfo.Status);
            Assert.Equal(InstanceStatus.UNKNOWN, jinfo.OverriddenStatus);
            Assert.NotNull(jinfo.LeaseInfo);
            Assert.Equal(30, jinfo.LeaseInfo.RenewalIntervalInSecs);
            Assert.Equal(90, jinfo.LeaseInfo.DurationInSecs);
            Assert.Equal(0, jinfo.LeaseInfo.RegistrationTimestamp);
            Assert.Equal(0, jinfo.LeaseInfo.LastRenewalTimestamp);
            Assert.Equal(0, jinfo.LeaseInfo.LastRenewalTimestampLegacy);
            Assert.Equal(0, jinfo.LeaseInfo.EvictionTimestamp);
            Assert.Equal(0, jinfo.LeaseInfo.ServiceUpTimestamp);
            Assert.False(jinfo.IsCoordinatingDiscoveryServer);
            Assert.NotNull(jinfo.Metadata);
            Assert.Equal(1, jinfo.Metadata.Count);
            Assert.True(jinfo.Metadata.ContainsKey("@class"));
            Assert.True(jinfo.Metadata.ContainsValue("java.util.Collections$EmptyMap"));
            Assert.Equal(jinfo.LastDirtyTimestamp, jinfo.LastUpdatedTimestamp);
            Assert.Equal(ActionType.ADDED, jinfo.Actiontype);
            Assert.Null(jinfo.AsgName);
        }
Esempio n. 3
0
        public virtual async Task <EurekaHttpResponse> RegisterAsync(InstanceInfo info)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

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

#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
            {
                request.Content = GetRequestContent(new JsonInstanceInfoRoot(info.ToJsonInstance()));

                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    _logger?.LogDebug("RegisterAsync {0}, status: {1}", requestUri.ToString(), response.StatusCode);
                    EurekaHttpResponse resp = new EurekaHttpResponse(response.StatusCode);
                    resp.Headers = response.Headers;
                    return(resp);
                }
            }
            catch (Exception e)
            {
                _logger?.LogError("RegisterAsync Exception:", e);
                throw;
            }

            finally
            {
                DisposeHttpClient(client);
#if NET452
                ServicePointManager.ServerCertificateValidationCallback = prevValidator;
#endif
            }
        }
Esempio n. 4
0
        public async void RegisterAsync_SendsValidPOSTData()
        {
            IHostingEnvironment envir = new HostingEnvironment();

            TestConfigServerStartup.Response     = "";
            TestConfigServerStartup.ReturnStatus = 204;
            var builder = new WebHostBuilder().UseStartup <TestConfigServerStartup>().UseEnvironment(envir.EnvironmentName);
            var server  = new TestServer(builder);


            var uri = "http://localhost:8888/";

            server.BaseAddress = new Uri(uri);
            EurekaInstanceConfig config = new EurekaInstanceConfig()
            {
                AppName = "foobar"
            };

            InstanceInfo info = InstanceInfo.FromInstanceConfig(config);

            var cconfig = new EurekaClientConfig()
            {
                EurekaServerServiceUrls = uri
            };
            var client = new EurekaHttpClient(cconfig, server.CreateClient());
            EurekaHttpResponse resp = await client.RegisterAsync(info);

            Assert.NotNull(TestConfigServerStartup.LastRequest);
            Assert.Equal("POST", TestConfigServerStartup.LastRequest.Method);
            Assert.Equal("localhost:8888", TestConfigServerStartup.LastRequest.Host.Value);
            Assert.Equal("/apps/FOOBAR", TestConfigServerStartup.LastRequest.Path.Value);

            // Check JSON payload
            JsonInstanceInfoRoot recvJson = JsonInstanceInfoRoot.Deserialize(TestConfigServerStartup.RequestBody);

            Assert.NotNull(recvJson);
            Assert.NotNull(recvJson.Instance);

            // Compare a few random values
            JsonInstanceInfo sentJsonObj = info.ToJsonInstance();

            Assert.Equal(sentJsonObj.Actiontype, recvJson.Instance.Actiontype);
            Assert.Equal(sentJsonObj.AppName, recvJson.Instance.AppName);
            Assert.Equal(sentJsonObj.HostName, recvJson.Instance.HostName);
        }