public void GetRequestMessage_Adds_Auth_When_JustPassword_In_Url() { var config = new EurekaClientConfig() { EurekaServerServiceUrls = "http://:pass@boo:123/eureka/" }; var client = new EurekaHttpClient(config); var result = client.GetRequestMessage(HttpMethod.Post, new Uri(config.EurekaServerServiceUrls)); Assert.Equal(HttpMethod.Post, result.Method); Assert.Equal(new Uri("http://*****:*****@boo:123/eureka/" }; var optionsMonitor = new TestOptionMonitorWrapper <EurekaClientOptions>(clientOptions); client = new EurekaHttpClient(optionsMonitor); // act result = client.GetRequestMessage(HttpMethod.Post, new Uri(clientOptions.EurekaServerServiceUrls)); // assert Assert.Equal(HttpMethod.Post, result.Method); Assert.Equal(new Uri("http://boo:123/eureka/"), result.RequestUri); Assert.True(result.Headers.Contains("Authorization")); }
public void Constructor_Intializes_Defaults() { EurekaClientOptions opts = new EurekaClientOptions(); Assert.True(opts.Enabled); Assert.Equal(EurekaClientOptions.Default_RegistryFetchIntervalSeconds, opts.RegistryFetchIntervalSeconds); Assert.Equal(EurekaClientOptions.Default_InstanceInfoReplicationIntervalSeconds, opts.InstanceInfoReplicationIntervalSeconds); Assert.Null(opts.ProxyHost); Assert.Equal(0, opts.ProxyPort); Assert.Null(opts.ProxyUserName); Assert.Null(opts.ProxyPassword); Assert.True(opts.ShouldGZipContent); Assert.Equal(EurekaClientOptions.Default_EurekaServerConnectTimeoutSeconds, opts.EurekaServerConnectTimeoutSeconds); Assert.True(opts.ShouldRegisterWithEureka); Assert.False(opts.AllowRedirects); Assert.False(opts.ShouldDisableDelta); Assert.True(opts.ShouldFilterOnlyUpInstances); Assert.True(opts.ShouldFetchRegistry); Assert.Null(opts.RegistryRefreshSingleVipAddress); Assert.True(opts.ShouldOnDemandUpdateStatusChange); Assert.Equal(EurekaClientOptions.Default_ServerServiceUrl, opts.EurekaServerServiceUrls); Assert.NotNull(opts.Health); Assert.True(opts.Health.Enabled); // Health contrib enabled Assert.True(opts.Health.CheckEnabled); // Health check enabled Assert.Null(opts.Health.MonitoredApps); }
public void Constructor_ThrowsIfClientOptionsNull() { // Arrange EurekaClientOptions clientOptions = null; // Act and Assert var ex = Assert.Throws <ArgumentNullException>(() => new EurekaDiscoveryClient(clientOptions, null)); Assert.Contains(nameof(clientOptions), ex.Message); }
public void Constructor_Initializes_Correctly() { var instOptions = new EurekaInstanceOptions(); var clientOptions = new EurekaClientOptions(); var wrapInst = new TestOptionMonitorWrapper<EurekaInstanceOptions>(instOptions); var wrapClient = new TestOptionMonitorWrapper<EurekaClientOptions>(clientOptions); var appMgr = new EurekaApplicationInfoManager(wrapInst); var client = new EurekaDiscoveryClient(wrapClient, wrapInst, appMgr); var mgr = new EurekaDiscoveryManager(wrapClient, wrapInst, client); Assert.Equal(instOptions, mgr.InstanceConfig); Assert.Equal(clientOptions, mgr.ClientConfig); Assert.Equal(client, mgr.Client); }
static void Main(string[] args) { // Build application configuration ConfigurationBuilder builder = new ConfigurationBuilder(); builder.SetBasePath(Environment.CurrentDirectory); builder.AddJsonFile("appsettings.json"); builder.AddCloudFoundry(); var configuration = builder.Build(); // Setup logging var factory = new LoggerFactory(); factory.AddConsole(configuration.GetSection("Logging")); // Build Eureka clients config from configuration var clientOpts = new EurekaClientOptions(); ConfigurationBinder.Bind(configuration.GetSection("eureka:client"), clientOpts); // if a Cloud Foundry Service Binding is found, update config var si = configuration.GetServiceInfos <EurekaServiceInfo>(); if (si.Any()) { EurekaPostConfigurer.UpdateConfiguration(configuration, si.First(), clientOpts); } // Create the Eureka client, start fetching registry in background thread var client = new DiscoveryClient(clientOpts, null, factory); do { // Get what applications have been fetched var apps = client.Applications; // Try to find app with name "MyApp", it is registered in the Register console application var app = apps.GetRegisteredApplication("MyApp"); if (app != null) { // Print the instance info, and then exit loop Console.WriteLine("Successfully fetched application: {0} ", app); break; } } while (true); Console.ReadLine(); }
public void GetRequestMessage_No_Auth_When_Creds_Not_In_Url() { // arrange var clientOptions = new EurekaClientOptions { ServiceUrl = "http://boo:123/eureka/" }; var optionsMonitor = new TestOptionMonitorWrapper <EurekaClientOptions>(clientOptions); var client = new TestPivotalEurekaHttpClient(optionsMonitor); // act var result = client.PublicGetRequestMessage(HttpMethod.Post, new Uri(clientOptions.EurekaServerServiceUrls)); // assert Assert.Equal(HttpMethod.Post, result.Method); Assert.Equal(new Uri("http://boo:123/eureka/"), result.RequestUri); Assert.False(result.Headers.Contains("Authorization")); }
static void Main(string[] args) { // Build application configuration ConfigurationBuilder builder = new ConfigurationBuilder(); builder.SetBasePath(Environment.CurrentDirectory); builder.AddJsonFile("appsettings.json"); builder.AddCloudFoundry(); var configuration = builder.Build(); // Setup logging var factory = new LoggerFactory(); factory.AddConsole(configuration.GetSection("Logging")); // Build Eureka clients config from configuration var clientOpts = new EurekaClientOptions(); ConfigurationBinder.Bind(configuration.GetSection(EurekaClientOptions.EUREKA_CLIENT_CONFIGURATION_PREFIX), clientOpts); // Build Eureka instance info config from configuration var instOpts = new EurekaInstanceOptions(); ConfigurationBinder.Bind(configuration.GetSection(EurekaInstanceOptions.EUREKA_INSTANCE_CONFIGURATION_PREFIX), instOpts); // if a Cloud Foundry Service Binding is found, update config var si = configuration.GetServiceInfos <EurekaServiceInfo>(); if (si.Any()) { EurekaPostConfigurer.UpdateConfiguration(configuration, si.First(), clientOpts); EurekaPostConfigurer.UpdateConfiguration(configuration, instOpts); } // Initialize ApplicationManager with instance configuration ApplicationInfoManager.Instance.Initialize(instOpts, factory); // Create the Eureka client, the Application is registered and renewed with registry var client = new DiscoveryClient(clientOpts, null, factory); // Hang and keep renewing the registration Console.ReadLine(); }
/// <summary> /// 添加服务注册法相扩展 /// </summary> /// <param name="services"></param> /// <param name="discoveryOptions"></param> /// <param name="lifecycle"></param> /// <returns></returns> public static IServiceCollection AddDiscoveryClient(this IServiceCollection services, DiscoveryOptions discoveryOptions, IDiscoveryLifecycle lifecycle = null) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (discoveryOptions == null) { throw new ArgumentNullException(nameof(discoveryOptions)); } if (discoveryOptions.ClientType == DiscoveryClientType.EUREKA) { EurekaClientOptions clientOptions = discoveryOptions.ClientOptions as EurekaClientOptions; if (clientOptions == null) { throw new ArgumentException("Missing Client Options"); } services.AddSingleton <IOptionsMonitor <EurekaClientOptions> >(new OptionsMonitorWrapper <EurekaClientOptions>(clientOptions)); var regOptions = discoveryOptions.RegistrationOptions as EurekaInstanceOptions; if (regOptions == null) { clientOptions.ShouldRegisterWithEureka = false; regOptions = new EurekaInstanceOptions(); } services.AddSingleton <IOptionsMonitor <EurekaInstanceOptions> >(new OptionsMonitorWrapper <EurekaInstanceOptions>(regOptions)); AddEurekaServices(services, lifecycle); } else { throw new ArgumentException("Client type UNKNOWN"); } services.TryAddTransient <DiscoveryHttpMessageHandler>(); return(services); }
public void ConfigureInstanceIfNeeded_Intializes_InstanceConfig() { var clientConfig = new EurekaClientOptions() { ShouldRegisterWithEureka = false, ShouldFetchRegistry = false }; var instConfig = new EurekaInstanceOptions(); var client = new EurekaDiscoveryClient(clientConfig, instConfig); Assert.NotNull(client.Client); Assert.NotNull(client.ClientConfig); Assert.NotNull(client.InstConfig); Assert.Equal("unknown", client.InstConfig.AppName); Assert.Equal("unknown", client.InstConfig.VirtualHostName); var expectId = client.InstConfig.GetHostName(false) + ":" + "unknown" + ":" + "8080"; Assert.Equal(expectId, client.InstConfig.InstanceId); }
private IDiscoveryClient CreateDiscoveryClient() { IConfigurationSection clientConfigSection = appContext.Configuration.GetSection(EurekaClientOptions.EUREKA_CLIENT_CONFIGURATION_PREFIX); if (clientConfigSection == null) { throw new ConfigurationErrorsException("Unable to load the Eureka client configuration section"); } IConfigurationSection instanceConfigSection = appContext.Configuration.GetSection(EurekaInstanceOptions.EUREKA_INSTANCE_CONFIGURATION_PREFIX); if (instanceConfigSection == null) { throw new ConfigurationErrorsException("Unable to load the Eureka instance configuration section"); } EurekaClientOptions clientOptions = new EurekaClientOptions(); clientConfigSection.Bind(clientOptions); EurekaInstanceOptions instanceOptions = new EurekaInstanceOptions(); instanceConfigSection.Bind(instanceOptions); EurekaServiceInfo svcInfo = GetSingletonDiscoveryServiceInfo() as EurekaServiceInfo; if (svcInfo != null) { EurekaPostConfigurer.UpdateConfiguration(appContext.Configuration, svcInfo, clientOptions); EurekaPostConfigurer.UpdateConfiguration(appContext.Configuration, svcInfo, instanceOptions); } EurekaApplicationInfoManager appInfoManager = new EurekaApplicationInfoManager (new OptionsMonitorWrapper <EurekaInstanceOptions>(instanceOptions), appContext.LoggerFactory); return(new EurekaDiscoveryClient(new OptionsMonitorWrapper <EurekaClientOptions>(clientOptions), new OptionsMonitorWrapper <EurekaInstanceOptions>(instanceOptions), appInfoManager, null, appContext.LoggerFactory)); }
private static IDiscoveryClient CreateDiscoveryClient(IServiceInfo info, IConfiguration config, IDiscoveryLifecycle lifecycle) { var clientConfigsection = config.GetSection(EUREKA_PREFIX); int childCount = clientConfigsection.GetChildren().Count(); if (childCount > 0) { EurekaServiceInfo einfo = info as EurekaServiceInfo; var clientSection = config.GetSection(EurekaClientOptions.EUREKA_CLIENT_CONFIGURATION_PREFIX); EurekaClientOptions clientOptions = new EurekaClientOptions(); ConfigurationBinder.Bind(clientSection, clientOptions); if (einfo != null) { PivotalEurekaConfigurer.UpdateConfiguration(config, einfo, clientOptions); } var instSection = config.GetSection(EurekaInstanceOptions.EUREKA_INSTANCE_CONFIGURATION_PREFIX); EurekaInstanceOptions instOptions = new EurekaInstanceOptions(); ConfigurationBinder.Bind(instSection, instOptions); if (einfo != null) { PivotalEurekaConfigurer.UpdateConfiguration(config, einfo, instOptions); } var manager = new EurekaApplicationInfoManager(new OptionsMonitorWrapper <EurekaInstanceOptions>(instOptions), LoggingConfig.LoggerFactory); return(new PivotalEurekaDiscoveryClient( new OptionsMonitorWrapper <EurekaClientOptions>(clientOptions), new OptionsMonitorWrapper <EurekaInstanceOptions>(instOptions), manager, null, LoggingConfig.LoggerFactory)); } else { throw new ArgumentException("Unable to create Eureka client"); } }
public void Constructor_Initializes_Correctly() { var clientConfig = new EurekaClientOptions() { ShouldRegisterWithEureka = false, ShouldFetchRegistry = false }; var cwrapper = new TestOptionMonitorWrapper <EurekaClientOptions>(clientConfig); var instConfig = new EurekaInstanceOptions(); var iwrapper = new TestOptionMonitorWrapper <EurekaInstanceOptions>(instConfig); var appMgr = new EurekaApplicationInfoManager(iwrapper); var client = new EurekaDiscoveryClient(cwrapper, iwrapper, appMgr); Assert.NotNull(client.ClientConfig); Assert.Equal(clientConfig, client.ClientConfig); Assert.NotNull(client.HttpClient); Assert.NotNull(client.Description); Assert.NotNull(client.Services); Assert.Empty(client.Services); var thisService = client.GetLocalServiceInstance(); Assert.NotNull(thisService); Assert.Equal(instConfig.GetHostName(false), thisService.Host); Assert.Equal(instConfig.SecurePortEnabled, thisService.IsSecure); Assert.NotNull(thisService.Metadata); Assert.Equal(instConfig.NonSecurePort, thisService.Port); Assert.Equal(instConfig.AppName, thisService.ServiceId); Assert.NotNull(thisService.Uri); string scheme = instConfig.SecurePortEnabled ? "https" : "http"; int uriPort = instConfig.SecurePortEnabled ? instConfig.SecurePort : instConfig.NonSecurePort; var uri = new Uri(scheme + "://" + instConfig.GetHostName(false) + ":" + uriPort.ToString()); Assert.Equal(uri, thisService.Uri); }
public void Constructor_Initializes_Correctly() { var clientConfig = new EurekaClientOptions() { ShouldRegisterWithEureka = false, ShouldFetchRegistry = false }; var instConfig = new EurekaInstanceOptions(); var client = new EurekaDiscoveryClient(clientConfig, instConfig); Assert.NotNull(client.Client); Assert.NotNull(client.ClientConfig); Assert.NotNull(client.InstConfig); Assert.NotNull(ApplicationInfoManager.Instance.InstanceConfig); Assert.NotNull(ApplicationInfoManager.Instance.InstanceInfo); Assert.NotNull(DiscoveryManager.Instance.ClientConfig); Assert.NotNull(DiscoveryManager.Instance.Client); Assert.NotNull(DiscoveryManager.Instance.InstanceConfig); }
public void Constructor_ConfiguresEurekaDiscovery_Correctly() { // Arrange var appsettings = @" { ""eureka"": { ""client"": { ""eurekaServer"": { ""proxyHost"": ""proxyHost"", ""proxyPort"": 100, ""proxyUserName"": ""proxyUserName"", ""proxyPassword"": ""proxyPassword"", ""shouldGZipContent"": true, ""connectTimeoutSeconds"": 100 }, ""allowRedirects"": true, ""shouldDisableDelta"": true, ""shouldFilterOnlyUpInstances"": true, ""shouldFetchRegistry"": true, ""registryRefreshSingleVipAddress"":""registryRefreshSingleVipAddress"", ""shouldOnDemandUpdateStatusChange"": true, ""shouldRegisterWithEureka"": true, ""registryFetchIntervalSeconds"": 100, ""instanceInfoReplicationIntervalSeconds"": 100, ""serviceUrl"": ""https://foo.bar:8761/eureka/"" }, ""instance"": { ""registrationMethod"" : ""foobar"", ""hostName"": ""myHostName"", ""instanceId"": ""instanceId"", ""appName"": ""appName"", ""appGroup"": ""appGroup"", ""instanceEnabledOnInit"": true, ""port"": 100, ""securePort"": 100, ""nonSecurePortEnabled"": true, ""securePortEnabled"": true, ""leaseExpirationDurationInSeconds"":100, ""leaseRenewalIntervalInSeconds"": 100, ""secureVipAddress"": ""secureVipAddress"", ""vipAddress"": ""vipAddress"", ""asgName"": ""asgName"", ""metadataMap"": { ""foo"": ""bar"", ""bar"": ""foo"" }, ""statusPageUrlPath"": ""statusPageUrlPath"", ""statusPageUrl"": ""statusPageUrl"", ""homePageUrlPath"":""homePageUrlPath"", ""homePageUrl"": ""homePageUrl"", ""healthCheckUrlPath"": ""healthCheckUrlPath"", ""healthCheckUrl"":""healthCheckUrl"", ""secureHealthCheckUrl"":""secureHealthCheckUrl"" } } }"; var path = TestHelpers.CreateTempFile(appsettings); var directory = Path.GetDirectoryName(path); var fileName = Path.GetFileName(path); var configurationBuilder = new ConfigurationBuilder(); configurationBuilder.SetBasePath(directory); configurationBuilder.AddJsonFile(fileName); var config = configurationBuilder.Build(); var clientSection = config.GetSection(EurekaClientOptions.EUREKA_CLIENT_CONFIGURATION_PREFIX); var co = new EurekaClientOptions(); clientSection.Bind(co); Assert.Equal("proxyHost", co.ProxyHost); Assert.Equal(100, co.ProxyPort); Assert.Equal("proxyPassword", co.ProxyPassword); Assert.Equal("proxyUserName", co.ProxyUserName); Assert.Equal(100, co.EurekaServerConnectTimeoutSeconds); Assert.Equal("https://foo.bar:8761/eureka/", co.EurekaServerServiceUrls); Assert.Equal(100, co.RegistryFetchIntervalSeconds); Assert.Equal("registryRefreshSingleVipAddress", co.RegistryRefreshSingleVipAddress); Assert.True(co.ShouldDisableDelta); Assert.True(co.ShouldFetchRegistry); Assert.True(co.ShouldFilterOnlyUpInstances); Assert.True(co.ShouldGZipContent); Assert.True(co.ShouldOnDemandUpdateStatusChange); Assert.True(co.ShouldRegisterWithEureka); Assert.NotNull(co.Health); Assert.True(co.Health.Enabled); // Health contrib enabled Assert.True(co.Health.CheckEnabled); // Health check enabled Assert.Null(co.Health.MonitoredApps); }
public static void UpdateConfiguration(IConfiguration config, EurekaServiceInfo si, EurekaClientOptions clientOptions) { Steeltoe.Discovery.Eureka.EurekaPostConfigurer.UpdateConfiguration(config, si, clientOptions); }
public void UpdateConfiguration_WithVCAPEnvVariables_AppName_Overrides_VCAPBinding() { var vcap_application = @" { ""limits"": { ""fds"": 16384, ""mem"": 512, ""disk"": 1024 }, ""application_name"": ""foo"", ""application_uris"": [ ""foo.apps.testcloud.com"" ], ""name"": ""foo"", ""space_name"": ""test"", ""space_id"": ""98c627e7-f559-46a4-9032-88cab63f8249"", ""uris"": [ ""foo.apps.testcloud.com"" ], ""users"": null, ""version"": ""4a439db9-4a82-47a3-aeea-8240465cff8e"", ""application_version"": ""4a439db9-4a82-47a3-aeea-8240465cff8e"", ""application_id"": ""ac923014-93a5-4aee-b934-a043b241868b"", ""instance_id"": ""instance_id"" }"; var vcap_services = @" { ""p-config-server"": [{ ""credentials"": { ""uri"": ""https://config-de211817-2e99-4c57-89e8-31fa7ca6a276.apps.testcloud.com"", ""client_id"": ""p-config-server-8f49dd26-e6cd-47a6-b2a0-7655cea20333"", ""client_secret"": ""vBDjqIf7XthT"", ""access_token_uri"": ""https://p-spring-cloud-services.uaa.system.testcloud.com/oauth/token"" }, ""syslog_drain_url"": null, ""label"": ""p-config-server"", ""provider"": null, ""plan"": ""standard"", ""name"": ""myConfigServer"", ""tags"": [ ""configuration"", ""spring-cloud"" ] }], ""p-service-registry"": [{ ""credentials"": { ""uri"": ""https://eureka-6a1b81f5-79e2-4d14-a86b-ddf584635a60.apps.testcloud.com"", ""client_id"": ""p-service-registry-06e28efd-24be-4ce3-9784-854ed8d2acbe"", ""client_secret"": ""dCsdoiuklicS"", ""access_token_uri"": ""https://p-spring-cloud-services.uaa.system.testcloud.com/oauth/token"" }, ""syslog_drain_url"": null, ""label"": ""p-service-registry"", ""provider"": null, ""plan"": ""standard"", ""name"": ""myDiscoveryService"", ""tags"": [ ""eureka"", ""discovery"", ""registry"", ""spring-cloud"" ] }] }"; var appsettings = @" { ""eureka"": { ""client"": { ""eurekaServer"": { ""proxyHost"": ""proxyHost"", ""proxyPort"": 100, ""proxyUserName"": ""proxyUserName"", ""proxyPassword"": ""proxyPassword"", ""shouldGZipContent"": true, ""connectTimeoutSeconds"": 100 }, ""allowRedirects"": true, ""shouldDisableDelta"": true, ""shouldFilterOnlyUpInstances"": true, ""shouldFetchRegistry"": true, ""registryRefreshSingleVipAddress"":""registryRefreshSingleVipAddress"", ""shouldOnDemandUpdateStatusChange"": true, ""shouldRegisterWithEureka"": true, ""registryFetchIntervalSeconds"": 100, ""instanceInfoReplicationIntervalSeconds"": 100, ""serviceUrl"": ""http://localhost:8761/eureka/"" }, ""instance"": { ""registrationMethod"": ""hostname"", ""instanceId"": ""instanceId"", ""appName"": ""appName"", ""appGroup"": ""appGroup"", ""instanceEnabledOnInit"": true, ""hostname"": ""myhostname"", ""port"": 100, ""securePort"": 100, ""nonSecurePortEnabled"": true, ""securePortEnabled"": true, ""leaseExpirationDurationInSeconds"":100, ""leaseRenewalIntervalInSeconds"": 100, ""secureVipAddress"": ""secureVipAddress"", ""vipAddress"": ""vipAddress"", ""asgName"": ""asgName"", ""metadataMap"": { ""foo"": ""bar"", ""bar"": ""foo"" }, ""statusPageUrlPath"": ""statusPageUrlPath"", ""statusPageUrl"": ""statusPageUrl"", ""homePageUrlPath"":""homePageUrlPath"", ""homePageUrl"": ""homePageUrl"", ""healthCheckUrlPath"": ""healthCheckUrlPath"", ""healthCheckUrl"":""healthCheckUrl"", ""secureHealthCheckUrl"":""secureHealthCheckUrl"" } } }"; Environment.SetEnvironmentVariable("VCAP_APPLICATION", vcap_application); Environment.SetEnvironmentVariable("VCAP_SERVICES", vcap_services); Environment.SetEnvironmentVariable("CF_INSTANCE_INDEX", "1"); Environment.SetEnvironmentVariable("CF_INSTANCE_GUID", "ac923014-93a5-4aee-b934-a043b241868b"); var path = TestHelpers.CreateTempFile(appsettings); string directory = Path.GetDirectoryName(path); string fileName = Path.GetFileName(path); ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.SetBasePath(directory); configurationBuilder.AddJsonFile(fileName); configurationBuilder.AddCloudFoundry(); var config = configurationBuilder.Build(); var sis = config.GetServiceInfos <EurekaServiceInfo>(); Assert.Single(sis); EurekaServiceInfo si = sis[0]; var clientOpts = new EurekaClientOptions(); var clientSection = config.GetSection(EurekaClientOptions.EUREKA_CLIENT_CONFIGURATION_PREFIX); clientSection.Bind(clientOpts); EurekaPostConfigurer.UpdateConfiguration(config, si, clientOpts); var co = clientOpts; Assert.NotNull(co); Assert.Equal("proxyHost", co.ProxyHost); Assert.Equal(100, co.ProxyPort); Assert.Equal("proxyPassword", co.ProxyPassword); Assert.Equal("proxyUserName", co.ProxyUserName); Assert.True(co.AllowRedirects); Assert.Equal(100, co.InstanceInfoReplicationIntervalSeconds); Assert.Equal(100, co.EurekaServerConnectTimeoutSeconds); Assert.Equal("https://eureka-6a1b81f5-79e2-4d14-a86b-ddf584635a60.apps.testcloud.com/eureka/", co.EurekaServerServiceUrls); Assert.Equal(100, co.RegistryFetchIntervalSeconds); Assert.Equal("registryRefreshSingleVipAddress", co.RegistryRefreshSingleVipAddress); Assert.True(co.ShouldDisableDelta); Assert.True(co.ShouldFetchRegistry); Assert.True(co.ShouldFilterOnlyUpInstances); Assert.True(co.ShouldGZipContent); Assert.True(co.ShouldOnDemandUpdateStatusChange); Assert.True(co.ShouldRegisterWithEureka); Assert.Equal("https://p-spring-cloud-services.uaa.system.testcloud.com/oauth/token", co.AccessTokenUri); Assert.Equal("p-service-registry-06e28efd-24be-4ce3-9784-854ed8d2acbe", co.ClientId); Assert.Equal("dCsdoiuklicS", co.ClientSecret); var instOpts = new EurekaInstanceOptions(); var instSection = config.GetSection(EurekaInstanceOptions.EUREKA_INSTANCE_CONFIGURATION_PREFIX); instSection.Bind(instOpts); EurekaPostConfigurer.UpdateConfiguration(config, si, instOpts); var ro = instOpts; Assert.Equal("hostname", ro.RegistrationMethod); Assert.Equal("myhostname:instance_id", ro.InstanceId); Assert.Equal("appName", ro.AppName); Assert.Equal("appGroup", ro.AppGroupName); Assert.True(ro.IsInstanceEnabledOnInit); Assert.Equal(100, ro.NonSecurePort); Assert.Equal("myhostname", ro.HostName); Assert.Equal(100, ro.SecurePort); Assert.True(ro.IsNonSecurePortEnabled); Assert.True(ro.SecurePortEnabled); Assert.Equal(100, ro.LeaseExpirationDurationInSeconds); Assert.Equal(100, ro.LeaseRenewalIntervalInSeconds); Assert.Equal("secureVipAddress", ro.SecureVirtualHostName); Assert.Equal("vipAddress", ro.VirtualHostName); Assert.Equal("asgName", ro.ASGName); Assert.Equal("statusPageUrlPath", ro.StatusPageUrlPath); Assert.Equal("statusPageUrl", ro.StatusPageUrl); Assert.Equal("homePageUrlPath", ro.HomePageUrlPath); Assert.Equal("homePageUrl", ro.HomePageUrl); Assert.Equal("healthCheckUrlPath", ro.HealthCheckUrlPath); Assert.Equal("healthCheckUrl", ro.HealthCheckUrl); Assert.Equal("secureHealthCheckUrl", ro.SecureHealthCheckUrl); var map = ro.MetadataMap; Assert.NotNull(map); Assert.Equal(6, map.Count); Assert.Equal("bar", map["foo"]); Assert.Equal("foo", map["bar"]); Assert.Equal("instance_id", map[EurekaPostConfigurer.INSTANCE_ID]); Assert.Equal("ac923014-93a5-4aee-b934-a043b241868b", map[EurekaPostConfigurer.CF_APP_GUID]); Assert.Equal("1", map[EurekaPostConfigurer.CF_INSTANCE_INDEX]); Assert.Equal(EurekaPostConfigurer.UNKNOWN_ZONE, map[EurekaPostConfigurer.ZONE]); }
public void Constructor_ConfiguresEurekaDiscovery_Correctly() { // Arrange var appsettings = @" { 'eureka': { 'client': { 'eurekaServer': { 'proxyHost': 'proxyHost', 'proxyPort': 100, 'proxyUserName': '******', 'proxyPassword': '******', 'shouldGZipContent': true, 'connectTimeoutSeconds': 100 }, 'allowRedirects': true, 'shouldDisableDelta': true, 'shouldFilterOnlyUpInstances': true, 'shouldFetchRegistry': true, 'registryRefreshSingleVipAddress':'registryRefreshSingleVipAddress', 'shouldOnDemandUpdateStatusChange': true, 'shouldRegisterWithEureka': true, 'registryFetchIntervalSeconds': 100, 'instanceInfoReplicationIntervalSeconds': 100, 'serviceUrl': 'https://foo.bar:8761/eureka/' }, 'instance': { 'registrationMethod' : 'foobar', 'hostName': 'myHostName', 'instanceId': 'instanceId', 'appName': 'appName', 'appGroup': 'appGroup', 'instanceEnabledOnInit': true, 'port': 100, 'securePort': 100, 'nonSecurePortEnabled': true, 'securePortEnabled': true, 'leaseExpirationDurationInSeconds':100, 'leaseRenewalIntervalInSeconds': 100, 'secureVipAddress': 'secureVipAddress', 'vipAddress': 'vipAddress', 'asgName': 'asgName', 'metadataMap': { 'foo': 'bar', 'bar': 'foo' }, 'statusPageUrlPath': 'statusPageUrlPath', 'statusPageUrl': 'statusPageUrl', 'homePageUrlPath':'homePageUrlPath', 'homePageUrl': 'homePageUrl', 'healthCheckUrlPath': 'healthCheckUrlPath', 'healthCheckUrl':'healthCheckUrl', 'secureHealthCheckUrl':'secureHealthCheckUrl' } } }"; var path = TestHelpers.CreateTempFile(appsettings); string directory = Path.GetDirectoryName(path); string fileName = Path.GetFileName(path); ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.SetBasePath(directory); configurationBuilder.AddJsonFile(fileName); var config = configurationBuilder.Build(); var clientSection = config.GetSection(EurekaClientOptions.EUREKA_CLIENT_CONFIGURATION_PREFIX); var co = new EurekaClientOptions(); clientSection.Bind(co); Assert.Equal("proxyHost", co.ProxyHost); Assert.Equal(100, co.ProxyPort); Assert.Equal("proxyPassword", co.ProxyPassword); Assert.Equal("proxyUserName", co.ProxyUserName); Assert.True(co.AllowRedirects); Assert.Equal(100, co.InstanceInfoReplicationIntervalSeconds); Assert.Equal(100, co.EurekaServerConnectTimeoutSeconds); Assert.Equal("https://foo.bar:8761/eureka/", co.EurekaServerServiceUrls); Assert.Equal(100, co.RegistryFetchIntervalSeconds); Assert.Equal("registryRefreshSingleVipAddress", co.RegistryRefreshSingleVipAddress); Assert.True(co.ShouldDisableDelta); Assert.True(co.ShouldFetchRegistry); Assert.True(co.ShouldFilterOnlyUpInstances); Assert.True(co.ShouldGZipContent); Assert.True(co.ShouldOnDemandUpdateStatusChange); Assert.True(co.ShouldRegisterWithEureka); Assert.NotNull(co.Health); Assert.True(co.Health.Enabled); // Health contrib enabled Assert.True(co.Health.CheckEnabled); // Health check enabled Assert.Null(co.Health.MonitoredApps); }
public void UpdateConfiguration_WithVCAPEnvVariables_Route_ConfiguresEurekaDiscovery_Correctly() { var vcap_application = @" { 'limits': { 'fds': 16384, 'mem': 512, 'disk': 1024 }, 'application_name': 'foo', 'application_uris': [ 'foo.apps.testcloud.com' ], 'name': 'foo', 'space_name': 'test', 'space_id': '98c627e7-f559-46a4-9032-88cab63f8249', 'uris': [ 'foo.apps.testcloud.com' ], 'users': null, 'version': '4a439db9-4a82-47a3-aeea-8240465cff8e', 'application_version': '4a439db9-4a82-47a3-aeea-8240465cff8e', 'application_id': 'ac923014-93a5-4aee-b934-a043b241868b', 'instance_id': 'instance_id' }"; var vcap_services = @" { 'p-config-server': [ { 'credentials': { 'uri': 'https://config-de211817-2e99-4c57-89e8-31fa7ca6a276.apps.testcloud.com', 'client_id': 'p-config-server-8f49dd26-e6cd-47a6-b2a0-7655cea20333', 'client_secret': 'vBDjqIf7XthT', 'access_token_uri': 'https://p-spring-cloud-services.uaa.system.testcloud.com/oauth/token' }, 'syslog_drain_url': null, 'label': 'p-config-server', 'provider': null, 'plan': 'standard', 'name': 'myConfigServer', 'tags': [ 'configuration', 'spring-cloud' ] } ], 'p-service-registry': [ { 'credentials': { 'uri': 'https://eureka-6a1b81f5-79e2-4d14-a86b-ddf584635a60.apps.testcloud.com', 'client_id': 'p-service-registry-06e28efd-24be-4ce3-9784-854ed8d2acbe', 'client_secret': 'dCsdoiuklicS', 'access_token_uri': 'https://p-spring-cloud-services.uaa.system.testcloud.com/oauth/token' }, 'syslog_drain_url': null, 'label': 'p-service-registry', 'provider': null, 'plan': 'standard', 'name': 'myDiscoveryService', 'tags': [ 'eureka', 'discovery', 'registry', 'spring-cloud' ] } ] }"; var appsettings = @" { 'eureka': { 'client': { 'eurekaServer': { 'proxyHost': 'proxyHost', 'proxyPort': 100, 'proxyUserName': '******', 'proxyPassword': '******', 'shouldGZipContent': true, 'connectTimeoutSeconds': 100 }, 'allowRedirects': true, 'shouldDisableDelta': true, 'shouldFilterOnlyUpInstances': true, 'shouldFetchRegistry': true, 'registryRefreshSingleVipAddress':'registryRefreshSingleVipAddress', 'shouldOnDemandUpdateStatusChange': true, 'shouldRegisterWithEureka': true, 'registryFetchIntervalSeconds': 100, 'instanceInfoReplicationIntervalSeconds': 100, 'serviceUrl': 'http://localhost:8761/eureka/' }, 'instance': { 'registrationMethod': 'route', 'instanceId': 'instanceId', 'appGroup': 'appGroup', 'instanceEnabledOnInit': true, 'hostname': 'myhostname', 'port': 100, 'securePort': 100, 'nonSecurePortEnabled': true, 'securePortEnabled': true, 'leaseExpirationDurationInSeconds':100, 'leaseRenewalIntervalInSeconds': 100, 'secureVipAddress': 'secureVipAddress', 'vipAddress': 'vipAddress', 'asgName': 'asgName', 'metadataMap': { 'foo': 'bar', 'bar': 'foo' }, 'statusPageUrlPath': 'statusPageUrlPath', 'statusPageUrl': 'statusPageUrl', 'homePageUrlPath':'homePageUrlPath', 'homePageUrl': 'homePageUrl', 'healthCheckUrlPath': 'healthCheckUrlPath', 'healthCheckUrl':'healthCheckUrl', 'secureHealthCheckUrl':'secureHealthCheckUrl' } } }"; Environment.SetEnvironmentVariable("VCAP_APPLICATION", vcap_application); Environment.SetEnvironmentVariable("VCAP_SERVICES", vcap_services); Environment.SetEnvironmentVariable("CF_INSTANCE_INDEX", "1"); Environment.SetEnvironmentVariable("CF_INSTANCE_GUID", "ac923014-93a5-4aee-b934-a043b241868b"); var path = TestHelpers.CreateTempFile(appsettings); string directory = Path.GetDirectoryName(path); string fileName = Path.GetFileName(path); ConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); configurationBuilder.SetBasePath(directory); configurationBuilder.AddJsonFile(fileName); configurationBuilder.AddCloudFoundry(); var config = configurationBuilder.Build(); var sis = config.GetServiceInfos <EurekaServiceInfo>(); Assert.Single(sis); EurekaServiceInfo si = sis[0]; var clientOpts = new EurekaClientOptions(); var clientSection = config.GetSection(EurekaClientOptions.EUREKA_CLIENT_CONFIGURATION_PREFIX); clientSection.Bind(clientOpts); EurekaPostConfigurer.UpdateConfiguration(config, si, clientOpts); var co = clientOpts; Assert.NotNull(co); Assert.Equal("proxyHost", co.ProxyHost); Assert.Equal(100, co.ProxyPort); Assert.Equal("proxyPassword", co.ProxyPassword); Assert.Equal("proxyUserName", co.ProxyUserName); Assert.True(co.AllowRedirects); Assert.Equal(100, co.InstanceInfoReplicationIntervalSeconds); Assert.Equal(100, co.EurekaServerConnectTimeoutSeconds); Assert.Equal("https://eureka-6a1b81f5-79e2-4d14-a86b-ddf584635a60.apps.testcloud.com/eureka/", co.EurekaServerServiceUrls); Assert.Equal(100, co.RegistryFetchIntervalSeconds); Assert.Equal("registryRefreshSingleVipAddress", co.RegistryRefreshSingleVipAddress); Assert.True(co.ShouldDisableDelta); Assert.True(co.ShouldFetchRegistry); Assert.True(co.ShouldFilterOnlyUpInstances); Assert.True(co.ShouldGZipContent); Assert.True(co.ShouldOnDemandUpdateStatusChange); Assert.True(co.ShouldRegisterWithEureka); Assert.Equal("https://p-spring-cloud-services.uaa.system.testcloud.com/oauth/token", co.AccessTokenUri); Assert.Equal("p-service-registry-06e28efd-24be-4ce3-9784-854ed8d2acbe", co.ClientId); Assert.Equal("dCsdoiuklicS", co.ClientSecret); var instOpts = new EurekaInstanceOptions(); var instSection = config.GetSection(EurekaInstanceOptions.EUREKA_INSTANCE_CONFIGURATION_PREFIX); instSection.Bind(instOpts); EurekaPostConfigurer.UpdateConfiguration(config, si, instOpts); var ro = instOpts; Assert.Equal("route", ro.RegistrationMethod); Assert.Equal("foo.apps.testcloud.com:instance_id", ro.InstanceId); Assert.Equal("foo", ro.AppName); Assert.Equal("appGroup", ro.AppGroupName); Assert.True(ro.IsInstanceEnabledOnInit); Assert.Equal(80, ro.NonSecurePort); Assert.Equal("foo.apps.testcloud.com", ro.HostName); Assert.Equal(443, ro.SecurePort); Assert.True(ro.IsNonSecurePortEnabled); Assert.True(ro.SecurePortEnabled); Assert.Equal(100, ro.LeaseExpirationDurationInSeconds); Assert.Equal(100, ro.LeaseRenewalIntervalInSeconds); Assert.Equal("secureVipAddress", ro.SecureVirtualHostName); Assert.Equal("vipAddress", ro.VirtualHostName); Assert.Equal("asgName", ro.ASGName); Assert.Equal("statusPageUrlPath", ro.StatusPageUrlPath); Assert.Equal("statusPageUrl", ro.StatusPageUrl); Assert.Equal("homePageUrlPath", ro.HomePageUrlPath); Assert.Equal("homePageUrl", ro.HomePageUrl); Assert.Equal("healthCheckUrlPath", ro.HealthCheckUrlPath); Assert.Equal("healthCheckUrl", ro.HealthCheckUrl); Assert.Equal("secureHealthCheckUrl", ro.SecureHealthCheckUrl); var map = ro.MetadataMap; Assert.NotNull(map); Assert.Equal(6, map.Count); Assert.Equal("bar", map["foo"]); Assert.Equal("foo", map["bar"]); Assert.Equal("instance_id", map[EurekaPostConfigurer.INSTANCE_ID]); Assert.Equal("ac923014-93a5-4aee-b934-a043b241868b", map[EurekaPostConfigurer.CF_APP_GUID]); Assert.Equal("1", map[EurekaPostConfigurer.CF_INSTANCE_INDEX]); Assert.Equal(EurekaPostConfigurer.UNKNOWN_ZONE, map[EurekaPostConfigurer.ZONE]); }
public static void UpdateConfiguration(IConfiguration config, EurekaServiceInfo si, EurekaClientOptions clientOptions) { if (clientOptions == null || si == null) { return; } var uri = si.Uri; if (!uri.EndsWith(EUREKA_URI_SUFFIX)) { uri = uri + EUREKA_URI_SUFFIX; } clientOptions.EurekaServerServiceUrls = uri; clientOptions.AccessTokenUri = si.TokenUri; clientOptions.ClientId = si.ClientId; clientOptions.ClientSecret = si.ClientSecret; }
public void UpdateConfiguration_NoServiceInfo_ConfiguresEurekaDiscovery_Correctly() { // Arrange var appsettings = @" { ""eureka"": { ""client"": { ""eurekaServer"": { ""proxyHost"": ""proxyHost"", ""proxyPort"": 100, ""proxyUserName"": ""proxyUserName"", ""proxyPassword"": ""proxyPassword"", ""shouldGZipContent"": true, ""connectTimeoutSeconds"": 100 }, ""allowRedirects"": true, ""shouldDisableDelta"": true, ""shouldFilterOnlyUpInstances"": true, ""shouldFetchRegistry"": true, ""registryRefreshSingleVipAddress"":""registryRefreshSingleVipAddress"", ""shouldOnDemandUpdateStatusChange"": true, ""shouldRegisterWithEureka"": true, ""registryFetchIntervalSeconds"": 100, ""instanceInfoReplicationIntervalSeconds"": 100, ""serviceUrl"": ""http://localhost:8761/eureka/"" }, ""instance"": { ""instanceId"": ""instanceId"", ""appName"": ""appName"", ""appGroup"": ""appGroup"", ""instanceEnabledOnInit"": true, ""hostname"": ""hostname"", ""port"": 100, ""securePort"": 100, ""nonSecurePortEnabled"": true, ""securePortEnabled"": true, ""leaseExpirationDurationInSeconds"":100, ""leaseRenewalIntervalInSeconds"": 100, ""secureVipAddress"": ""secureVipAddress"", ""vipAddress"": ""vipAddress"", ""asgName"": ""asgName"", ""metadataMap"": { ""foo"": ""bar"", ""bar"": ""foo"" }, ""statusPageUrlPath"": ""statusPageUrlPath"", ""statusPageUrl"": ""statusPageUrl"", ""homePageUrlPath"":""homePageUrlPath"", ""homePageUrl"": ""homePageUrl"", ""healthCheckUrlPath"": ""healthCheckUrlPath"", ""healthCheckUrl"":""healthCheckUrl"", ""secureHealthCheckUrl"":""secureHealthCheckUrl"" } } }"; var basePath = Path.GetTempPath(); var path = TestHelpers.CreateTempFile(appsettings); var configurationBuilder = new ConfigurationBuilder(); configurationBuilder.SetBasePath(basePath); configurationBuilder.AddJsonFile(Path.GetFileName(path)); var config = configurationBuilder.Build(); var clientOpts = new EurekaClientOptions(); var clientSection = config.GetSection(EurekaClientOptions.EUREKA_CLIENT_CONFIGURATION_PREFIX); clientSection.Bind(clientOpts); EurekaPostConfigurer.UpdateConfiguration(config, null, clientOpts); var co = clientOpts; Assert.NotNull(co); Assert.Equal("proxyHost", co.ProxyHost); Assert.Equal(100, co.ProxyPort); Assert.Equal("proxyPassword", co.ProxyPassword); Assert.Equal("proxyUserName", co.ProxyUserName); Assert.True(co.AllowRedirects); Assert.Equal(100, co.InstanceInfoReplicationIntervalSeconds); Assert.Equal(100, co.EurekaServerConnectTimeoutSeconds); Assert.Equal("http://localhost:8761/eureka/", co.EurekaServerServiceUrls); Assert.Equal(100, co.RegistryFetchIntervalSeconds); Assert.Equal("registryRefreshSingleVipAddress", co.RegistryRefreshSingleVipAddress); Assert.True(co.ShouldDisableDelta); Assert.True(co.ShouldFetchRegistry); Assert.True(co.ShouldFilterOnlyUpInstances); Assert.True(co.ShouldGZipContent); Assert.True(co.ShouldOnDemandUpdateStatusChange); Assert.True(co.ShouldRegisterWithEureka); var instOpts = new EurekaInstanceOptions(); var instSection = config.GetSection(EurekaInstanceOptions.EUREKA_INSTANCE_CONFIGURATION_PREFIX); instSection.Bind(instOpts); EurekaPostConfigurer.UpdateConfiguration(config, null, instOpts); EurekaInstanceOptions ro = instOpts; Assert.Equal("instanceId", ro.InstanceId); Assert.Equal("appName", ro.AppName); Assert.Equal("appGroup", ro.AppGroupName); Assert.True(ro.IsInstanceEnabledOnInit); Assert.Equal(100, ro.NonSecurePort); Assert.Equal("hostname", ro.HostName); Assert.Equal(100, ro.SecurePort); Assert.True(ro.IsNonSecurePortEnabled); Assert.True(ro.SecurePortEnabled); Assert.Equal(100, ro.LeaseExpirationDurationInSeconds); Assert.Equal(100, ro.LeaseRenewalIntervalInSeconds); Assert.Equal("secureVipAddress", ro.SecureVirtualHostName); Assert.Equal("vipAddress", ro.VirtualHostName); Assert.Equal("asgName", ro.ASGName); Assert.Equal("statusPageUrlPath", ro.StatusPageUrlPath); Assert.Equal("statusPageUrl", ro.StatusPageUrl); Assert.Equal("homePageUrlPath", ro.HomePageUrlPath); Assert.Equal("homePageUrl", ro.HomePageUrl); Assert.Equal("healthCheckUrlPath", ro.HealthCheckUrlPath); Assert.Equal("healthCheckUrl", ro.HealthCheckUrl); Assert.Equal("secureHealthCheckUrl", ro.SecureHealthCheckUrl); var map = ro.MetadataMap; Assert.NotNull(map); Assert.Equal(2, map.Count); Assert.Equal("bar", map["foo"]); Assert.Equal("foo", map["bar"]); }