Ejemplo n.º 1
0
        private static async Task <bool> WatchExceptionHandler(ConsulKvEndpoint kv, Exception e, string key,
                                                               ILogger?logger, CancellationToken ct)
        {
            await Task.Delay(10000, ct);

            logger?.LogWarning($"Restart watching {key}");
            return(true);
        }
Ejemplo n.º 2
0
 private static Action <WebHostBuilderContext, IConfigurationBuilder> ConfigureAppConfiguration(
     ConfigurationOptions configurationOptions,
     NethiumConfig nethiumConfig,
     IConsulHandler consulHandler,
     IConsulBuilder consulBuilder
     ) =>
 (context, configurationBuilder) =>
 {
     var consulKvEndpoint = new ConsulKvEndpoint(consulBuilder,
                                                 nethiumConfig.LoggerFactory?.CreateLogger <ConsulKvEndpoint>())
     {
         WatchExceptionHandler = WatchExceptionHandler
     };
     configurationBuilder
     .AddJsonFile("nethium.json", true, true)
     .AddEnvironmentVariables("NETHIUM_")
     .Add(new ConsulConfigurationSource(configurationOptions, consulHandler, consulKvEndpoint));
 };
Ejemplo n.º 3
0
        public static IConfigurationBuilder AddConsul(
            this IConfigurationBuilder builder,
            ConfigurationOptions configurationOptions,
            IConsulHandler consulHandler,
            ILoggerFactory?loggerFactory
            )
        {
            var consulBuilder  = new ConsulBuilder(consulHandler);
            var consulEndpoint = new ConsulKvEndpoint(consulBuilder, loggerFactory.CreateLogger <ConsulKvEndpoint>())
            {
                WatchExceptionHandler = async(kv, e, key, logger, ct) =>
                {
                    await Task.Delay(10000, ct);

                    logger.LogWarning($"Restart watching {key}");
                    return(true);
                }
            };

            return(builder.AddConsul(configurationOptions, consulHandler, consulEndpoint));
        }
Ejemplo n.º 4
0
        public static IWebHostBuilder UseNethium(this IWebHostBuilder builder,
                                                 Action <NethiumConfig> configureNethiumOption)
        {
            var nethiumConfig = new NethiumConfig();

            configureNethiumOption(nethiumConfig);
            nethiumConfig.CancellationToken ??= builder.GetShutdownToken();
            var bootstrapConfig = nethiumConfig.BootstrapConfig;
            var logger          = nethiumConfig.LoggerFactory?.CreateLogger <NethiumConfig>();

            if (bootstrapConfig != null)
            {
                nethiumConfig.Prefix ??= bootstrapConfig["Nethium:Consul:Prefix"] ?? throw new ArgumentNullException("Nethium:Consul:Prefix", "Consul:Prefix not specified in config");
                nethiumConfig.ConfigurationPrefix ??= bootstrapConfig["Nethium:Consul:ConfigurationPrefix"] ??
                nethiumConfig.Prefix;
                nethiumConfig.Separator ??= bootstrapConfig["Nethium:Consul:Separator"];
                nethiumConfig.Watch ??= bootstrapConfig["Nethium:Consul:Watch"];
            }

            var configurationOptions = new ConfigurationOptions
            {
                Prefix = nethiumConfig.Prefix !,
                ConfigurationPrefix = nethiumConfig.ConfigurationPrefix,
                Separator           = nethiumConfig.Separator !,
                Watch      = nethiumConfig.Watch,
                AutoReload = nethiumConfig.AutoReload ?? false
            };

            var consulHandler = new ConsulHandler
            {
                ClientConfigurationOptions = ClientConfiguration(bootstrapConfig, nethiumConfig),
                CancellationToken          = nethiumConfig.CancellationToken
            };
            var consulBuilder             = new ConsulBuilder(consulHandler);
            var consulClientConfiguration = new ConsulClientConfiguration();

            consulHandler.ClientConfigurationOptions.Invoke(consulClientConfiguration);
            foreach (var pair in bootstrapConfig.AsEnumerable())
            {
                logger?.LogInformation(pair.Key + ": " + pair.Value);
            }
            logger?.LogInformation(bootstrapConfig?["Nethium:Consul:Watch"]);
            logger?.LogInformation($"Using consul at address {consulClientConfiguration.Address} " +
                                   $"{(string.IsNullOrEmpty(consulClientConfiguration.Datacenter) ? "" : "datacenter " + consulClientConfiguration.Datacenter)}");
            logger?.LogInformation($"Consul config prefix {configurationOptions.Prefix}" +
                                   $"{(string.IsNullOrEmpty(configurationOptions.ConfigurationPrefix) ? "" : " (" + configurationOptions.ConfigurationPrefix + ") ")}" +
                                   $"{(string.IsNullOrEmpty(configurationOptions.Watch) ? "" : "watching " + configurationOptions.Watch + (configurationOptions.AutoReload ? " auto-reload" : " no auto-reload"))}");
            if (nethiumConfig.HostConfiguration)
            {
                logger?.LogInformation("Injecting Nethium config provider to host configuration");
                var consulKvEndpoint = new ConsulKvEndpoint(consulBuilder,
                                                            nethiumConfig.LoggerFactory?.CreateLogger <ConsulKvEndpoint>())
                {
                    WatchExceptionHandler = WatchExceptionHandler
                };
                var configurationBuilder = new ConfigurationBuilder();
                if (bootstrapConfig != null)
                {
                    configurationBuilder.AddConfiguration(bootstrapConfig);
                }

                var configurationRoot = configurationBuilder
                                        .AddConsul(configurationOptions, consulHandler, consulKvEndpoint)
                                        .Build();

                builder
                .UseConfiguration(configurationRoot);
            }

            builder
            .ConfigureServices(ConfigureServices(nethiumConfig, consulHandler, consulBuilder))
            .ConfigureAppConfiguration(ConfigureAppConfiguration(configurationOptions, nethiumConfig, consulHandler,
                                                                 consulBuilder));
            return(builder);
        }