Consul configuration provider implementation for Microsoft.Extensions.Configuration. Allows reading Asp.Net.Core app configuration from Consul Key-Value store
Install package into your project from NuGet and use AddConsul
extension method on IConfigurationBuilder
as shown in examples below:
IConfigurationBuilder builder = new ConfigurationBuilder()
.AddConsul("ExampleConsulKey");
IConfiguration configuration = builder.Build()
IWebHost webHost = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureAppConfiguration((hostingContext, config) =>
{
config
.AddJsonFile("appsettings.json", true, true)
.AddConsul("ExampleConsulKey")
.AddEnvironmentVariables()
.AddCommandLine(args);
})
.UseStartup<Startup>()
.Build();
In both cases Consul configuration provider will make a GET HTTP request to Consul using URL http://localhost:8500/v1/kv/ExampleConsulKey?recurse=true
to list all sub-keys of ExampleConsulKey
and add them to configuration dictionary
For release notes please see CHANGELOG.md
In case your Consul agent doesn't run on http://localhost:8500
it is possible to provide custom Consul address:
-
By specifying additional arguments to "AddConsul" method e.g.
.AddConsul("ExampleConsulKey", "example.com", 9999)
. In this case provider will make a request tohttp://example.com:9999/v1/kv/ExampleConsulKey?recurse=true
instead of localhost -
By setting two ENV variables -
CONSUL_HOST
andCONSUL_PORT
to Consul host and port respectively e.g. settingCONSUL_HOST=example.com
andCONSUL_PORT=9999
and calling.AddConsul("ExampleConsulKey")
will give same result as the first case.
Please note that each of host and port values can be set using different approach. Default value will be used if not set. For example:
- setting just
CONSUL_HOST=example.com
and calling.AddConsul("ExampleConsulKey")
will give base addressexample.com:8500
. - setting
CONSUL_PORT=9999
and calling.AddConsul("ExampleConsulKey", "example.com", null)
will give base addressexample.com:9999
- setting
CONSUL_PORT=9999
and calling.AddConsul("ExampleConsulKey")
will give addresslocalhost:9999
Let's assume that Consul provider is added like this .AddConsul("ExampleConsulKey")
and Consul KV Store contains keys listed in the left table column.
Corresponding configuration keys, that will be created by provider, are listed on the right:
Consul KV Store Key | Configuration Key |
---|---|
ExampleConsulKey/key1 | key1 |
ExampleConsulKey/key2 | key2 |
ExampleConsulKey/key3/subkey1 | key3:subkey1 |
ExampleConsulKey/key4/subkey1/subsubkey1 | key4:subkey1:subsubkey1 |
OtherKey |
OtherKey
will not be included because it is not under ExampleConsulKey
key.
Consul provider allows defining arrays by adding item index in square brackets to the Consul KV Store key
Consul KV StoreKey | Configuration Key |
---|---|
ExampleConsulKey/keys[0] | keys:0 |
ExampleConsulKey/keys[1] | keys:1 |
ExampleConsulKey/keys2[0]/subkey1 | keys2:0:subkey1 |
ExampleConsulKey/keys2[0]/subkey2 | keys2:0:subkey2 |
ExampleConsulKey/keys2[1]/subkey1 | keys2:1:subkey1 |
ExampleConsulKey/keys2[1]/subkey2 | keys2:1:subkey2 |
Consul provider defines settings in a well-known format (colon as delimiter for sub-keys and array indexes) that allows binding to types using Configuration Binder and Options.
For examples of this functionality you may take a look at tests here.
Feel free to ask questions and post bugs/ideas in the issues, as well as send pull requests.