private static void ConfigureEurekaServices(IServiceCollection services, IConfiguration config, IServiceInfo info, InetOptions netOptions) { var einfo = info as EurekaServiceInfo; var clientSection = config.GetSection(EurekaClientOptions.EUREKA_CLIENT_CONFIGURATION_PREFIX); services.Configure <EurekaClientOptions>(clientSection); services.PostConfigure <EurekaClientOptions>((options) => { EurekaPostConfigurer.UpdateConfiguration(config, einfo, options); }); var instSection = config.GetSection(EurekaInstanceOptions.EUREKA_INSTANCE_CONFIGURATION_PREFIX); services.Configure <EurekaInstanceOptions>(instSection); services.PostConfigure <EurekaInstanceOptions>((options) => { IApplicationInstanceInfo appInfo = null; if (einfo?.ApplicationInfo == null) { appInfo = services.GetApplicationInstanceInfo(); } options.NetUtils = new InetUtils(netOptions); options.ApplyNetUtils(); EurekaPostConfigurer.UpdateConfiguration(config, einfo, options, einfo?.ApplicationInfo ?? appInfo); }); }
public void UpdateConfiguration_UpdatesCorrectly() { var builder = new ConfigurationBuilder(); builder.AddInMemoryCollection(new Dictionary <string, string>() { { "spring:application:name", "bar" }, { "spring:application:instance_id", "instance" }, { "spring:cloud:discovery:registrationMethod", "registrationMethod" }, }); IConfigurationRoot root = builder.Build(); var instOpts = new EurekaInstanceOptions() { AppName = "dontChange", InstanceId = "dontChange", RegistrationMethod = "dontChange" }; EurekaPostConfigurer.UpdateConfiguration(root, instOpts); Assert.Equal("dontChange", instOpts.AppName); Assert.Equal("dontChange", instOpts.InstanceId); Assert.Equal("dontChange", instOpts.RegistrationMethod); Assert.Equal("dontChange", instOpts.VirtualHostName); Assert.Equal("dontChange", instOpts.SecureVirtualHostName); }
private static void ConfigureEurekaServices(IServiceCollection services, IConfiguration config, IServiceInfo info) { var einfo = info as EurekaServiceInfo; var clientSection = config.GetSection(EurekaClientOptions.EUREKA_CLIENT_CONFIGURATION_PREFIX); services.Configure <EurekaClientOptions>(clientSection); services.PostConfigure <EurekaClientOptions>((options) => { EurekaPostConfigurer.UpdateConfiguration(config, einfo, options); }); var instSection = config.GetSection(EurekaInstanceOptions.EUREKA_INSTANCE_CONFIGURATION_PREFIX); services.Configure <EurekaInstanceOptions>(instSection); services.PostConfigure <EurekaInstanceOptions>((options) => { IApplicationInstanceInfo appInfo = null; if (einfo?.ApplicationInfo == null) { appInfo = services.BuildServiceProvider().GetService <IApplicationInstanceInfo>() ?? new ApplicationInstanceInfo(config); } EurekaPostConfigurer.UpdateConfiguration(config, einfo, options, einfo?.ApplicationInfo ?? appInfo); }); }
public static void UpdateConfiguration(IConfiguration config, EurekaServiceInfo si, EurekaInstanceOptions instOptions) { if (instOptions == null) { return; } EurekaPostConfigurer.UpdateConfiguration(config, instOptions); if (si == null) { return; } if (string.IsNullOrEmpty(instOptions.RegistrationMethod) || ROUTE_REGISTRATIONMETHOD.Equals(instOptions.RegistrationMethod, StringComparison.OrdinalIgnoreCase)) { UpdateWithDefaultsForRoute(si, instOptions); return; } if (DIRECT_REGISTRATIONMETHOD.Equals(instOptions.RegistrationMethod, StringComparison.OrdinalIgnoreCase)) { UpdateWithDefaultsForDirect(si, instOptions); return; } if (HOST_REGISTRATIONMETHOD.Equals(instOptions.RegistrationMethod, StringComparison.OrdinalIgnoreCase)) { UpdateWithDefaultsForHost(si, instOptions, instOptions.HostName); return; } }
public void UpdateConfigurationComplainsAboutDefaultWhenWontWork() { Environment.SetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER", "true"); var exception = Assert.Throws <InvalidOperationException>(() => EurekaPostConfigurer.UpdateConfiguration(null, null, new EurekaClientOptions())); Assert.Contains(EurekaClientConfig.Default_ServerServiceUrl, exception.Message); Environment.SetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER", null); }
public void UpdateConfigurationFindsUrls() { var config = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary <string, string>() { { "urls", "https://myapp:1234;http://0.0.0.0:1233;http://::1233;http://*:1233" } }).Build(); var instOpts = new EurekaInstanceOptions(); var appInfo = new ApplicationInstanceInfo(config); EurekaPostConfigurer.UpdateConfiguration(config, instOpts, appInfo); Assert.Equal("myapp", instOpts.HostName); Assert.Equal(1234, instOpts.SecurePort); Assert.Equal(1233, instOpts.Port); }
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(); }
private static void ConfigureEurekaServices(IServiceCollection services, IConfiguration config, IServiceInfo info) { EurekaServiceInfo einfo = info as EurekaServiceInfo; var clientSection = config.GetSection(EurekaClientOptions.EUREKA_CLIENT_CONFIGURATION_PREFIX); services.Configure<EurekaClientOptions>(clientSection); services.PostConfigure<EurekaClientOptions>((options) => { EurekaPostConfigurer.UpdateConfiguration(config, einfo, options); }); var instSection = config.GetSection(EurekaInstanceOptions.EUREKA_INSTANCE_CONFIGURATION_PREFIX); services.Configure<EurekaInstanceOptions>(instSection); services.PostConfigure<EurekaInstanceOptions>((options) => { EurekaPostConfigurer.UpdateConfiguration(config, einfo, options); }); }
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(); }
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)); }
public void UpdateConfiguration_UpdatesCorrectly2() { var builder = new ConfigurationBuilder(); builder.AddInMemoryCollection(new Dictionary <string, string>() { { "spring:application:name", "bar" }, { "spring:cloud:discovery:registrationMethod", "registrationMethod" }, }); IConfigurationRoot root = builder.Build(); var instOpts = new EurekaInstanceOptions(); EurekaPostConfigurer.UpdateConfiguration(root, instOpts); Assert.Equal("bar", instOpts.AppName); Assert.EndsWith("bar:80", instOpts.InstanceId, StringComparison.OrdinalIgnoreCase); Assert.Equal("registrationMethod", instOpts.RegistrationMethod); Assert.Equal("bar", instOpts.VirtualHostName); Assert.Equal("bar", instOpts.SecureVirtualHostName); }
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) { EurekaPostConfigurer.UpdateConfiguration(config, einfo, clientOptions); } var instSection = config.GetSection(EurekaInstanceOptions.EUREKA_INSTANCE_CONFIGURATION_PREFIX); EurekaInstanceOptions instOptions = new EurekaInstanceOptions(); ConfigurationBinder.Bind(instSection, instOptions); if (einfo != null) { EurekaPostConfigurer.UpdateConfiguration(config, einfo, instOptions); } var manager = new EurekaApplicationInfoManager(new OptionsMonitorWrapper <EurekaInstanceOptions>(instOptions), LoggingConfig.LoggerFactory); return(new EurekaDiscoveryClient( new OptionsMonitorWrapper <EurekaClientOptions>(clientOptions), new OptionsMonitorWrapper <EurekaInstanceOptions>(instOptions), manager, null, LoggingConfig.LoggerFactory)); } else { throw new ArgumentException("Unable to create Eureka client"); } }
private static void AddDiscoveryServices(IServiceCollection services, IConfiguration config, IDiscoveryLifecycle lifecycle) { var clientConfigsection = config.GetSection(EUREKA_PREFIX); int childCount = clientConfigsection.GetChildren().Count(); if (childCount > 0) { var clientSection = config.GetSection(EurekaClientOptions.EUREKA_CLIENT_CONFIGURATION_PREFIX); services.Configure <EurekaClientOptions>(clientSection); var instSection = config.GetSection(EurekaInstanceOptions.EUREKA_INSTANCE_CONFIGURATION_PREFIX); services.Configure <EurekaInstanceOptions>(instSection); services.PostConfigure <EurekaInstanceOptions>((options) => { EurekaPostConfigurer.UpdateConfiguration(config, options); }); AddEurekaServices(services, lifecycle); } else { throw new ArgumentException("Discovery client type UNKNOWN, check configuration"); } }
public void UpdateConfiguration_WithInstDefaults_UpdatesCorrectly() { var builder = new ConfigurationBuilder(); builder.AddInMemoryCollection(new Dictionary <string, string>() { { "spring:application:name", "bar" }, { "spring:application:instance_id", "instance" }, { "spring:cloud:discovery:registrationMethod", "registrationMethod" }, }); var root = builder.Build(); var instOpts = new EurekaInstanceOptions(); EurekaPostConfigurer.UpdateConfiguration(root, instOpts, new ApplicationInstanceInfo(root)); Assert.Equal("bar", instOpts.AppName); Assert.Equal("instance", instOpts.InstanceId); Assert.Equal("registrationMethod", instOpts.RegistrationMethod); Assert.Equal("bar", instOpts.VirtualHostName); Assert.Equal("bar", instOpts.SecureVirtualHostName); }
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 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"]); }
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]); }