internal static void ConfigureConsulServices(IServiceCollection services)
        {
            services
            .AddOptions <ConsulOptions>()
            .Configure <IConfiguration>((options, config) => config.GetSection(ConsulOptions.CONSUL_CONFIGURATION_PREFIX).Bind(options))
            .PostConfigure(options => ConsulPostConfigurer.ValidateConsulOptions(options));

            services
            .AddOptions <ConsulDiscoveryOptions>()
            .Configure <IConfiguration>((options, config) =>
            {
                config.GetSection(ConsulDiscoveryOptions.CONSUL_DISCOVERY_CONFIGURATION_PREFIX).Bind(options);

                // Consul is enabled by default. If consul:discovery:enabled was not set then check spring:cloud:discovery:enabled
                if (options.Enabled &&
                    config.GetValue <bool?>(ConsulDiscoveryOptions.CONSUL_DISCOVERY_CONFIGURATION_PREFIX + ":enabled") is null &&
                    config.GetValue <bool?>(_springDiscoveryEnabled) == false)
                {
                    options.Enabled = false;
                }
            })
            .PostConfigure <IConfiguration>((options, config) =>
            {
                var netOptions = config.GetSection(InetOptions.PREFIX).Get <InetOptions>();
                ConsulPostConfigurer.UpdateDiscoveryOptions(config, options, netOptions);
            });

            services.TryAddSingleton(serviceProvider =>
            {
                var clientOptions = serviceProvider.GetRequiredService <IOptions <ConsulDiscoveryOptions> >();
                return(new DistributedCacheEntryOptions {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(clientOptions.Value.CacheTTL)
                });
            });
        }
Exemplo n.º 2
0
        private static void ConfigureConsulServices(IServiceCollection services, IConfiguration config, InetOptions netOptions)
        {
            var consulSection = config.GetSection(ConsulOptions.CONSUL_CONFIGURATION_PREFIX);

            services.Configure <ConsulOptions>(consulSection);
            services.PostConfigure <ConsulOptions>(options => ConsulPostConfigurer.ValidateConsulOptions(options));
            var consulDiscoverySection = config.GetSection(ConsulDiscoveryOptions.CONSUL_DISCOVERY_CONFIGURATION_PREFIX);

            services.Configure <ConsulDiscoveryOptions>(consulDiscoverySection);
            services.PostConfigure <ConsulDiscoveryOptions>(options => ConsulPostConfigurer.UpdateDiscoveryOptions(config, options, netOptions));
            services.TryAddSingleton(serviceProvider =>
            {
                var clientOptions = serviceProvider.GetRequiredService <IOptions <ConsulDiscoveryOptions> >();
                return(new DistributedCacheEntryOptions {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(clientOptions.Value.CacheTTL)
                });
            });
        }