You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adds support for configuring .Net Core application using Consul. It is expected that the configuration will be stored as a single object under a given key in Consul. Works great with [git2consul](https://github.com/Cimpress-MCP/git2consul).
8
+
Adds support for configuring .NET Core applications using Consul. Works great with [git2consul](https://github.com/Cimpress-MCP/git2consul).
9
9
10
10
## Installation
11
11
12
-
Add `Winton.Extensions.Configuration.Consul` to the `dependencies` section of your project.json
12
+
Add `Winton.Extensions.Configuration.Consul` to your project's dependencies, either via the NuGet package manager or as a `PackageReference` in the csproj file.
13
13
14
14
## Usage
15
15
16
-
Add the following to your `StartUp` class for the minimal setup:
16
+
### Minimal Setup
17
+
The library provides an extension method called `AddConsul` for `IConfigurationBuilder` in the same way that other configuration providers do. The `IConfigurationBuilder` is usually configured in either the `Program` or `Startup` class for an ASP.NET Core application. See Microsoft's [documentation](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.1) for more information about `IConfigurationBuilder`.
Assuming the application is running in the development environment and the application name is Website, this will load a json configuration object from the key `Website/Development` in Consul.
29
+
Assuming the application is running in the 'Development' environment and the application name is 'Website', then this will load a JSON configuration object from the `Website/Development` key in Consul.
30
+
31
+
The `CancellationToken` is used to cancel any active requests/watches to/on Consul.
32
+
It is recommended that this is cancelled during application shutdown to clean up resources. This will typically be done in one of two places. Either in the `Program` class, for example:
The `CancellationToken` is used to cancel any current requests or watches with Consul.
30
-
It is recommended that this is cancelled during application shut down to clean up resources. This can be done like so in the `Configure` method of your `StartUp` class by injecting the `IApplicationLifetime`:
An options `Action` can be specified as a third argument to set the options outlined below.
68
+
### Options
69
+
`AddConsul` has an overload with an additional third parameter of type `Action<IConsulConfigurationSource>` which allows the options outlined below to be set.
42
70
43
-
## Configuration Options
44
71
***`ConsulConfigurationOptions`**
45
72
46
-
An `Action` that can be used to configure Consul options
47
-
***`ConsulHttpClientOptions`**
48
-
49
-
An `Action` that can be used to configure Consul HTTP options
73
+
An `Action<ConsulClientConfiguration>` that can be used to configure the underlying Consul client.
50
74
***`ConsulHttpClientHandlerOptions`**
51
75
52
-
An `Action` that can be used to configure Consul HTTP handler options
53
-
***`OnLoadException`**
76
+
An `Action<HttpClientHandler>` that can be used to configure the underlying Consul client's HTTP handler options.
77
+
***`ConsulHttpClientOptions`**
54
78
55
-
An `Action` that can be used to configure how exceptions should be handled during load
79
+
An `Action<HttpClient>` that can be used to configure the underlying Consul client's HTTP options.
56
80
***`OnLoadException`**
57
81
58
-
An `Action` that can be used to configure how exceptions should be handled that are thrown when watching for changes
82
+
An `Action<ConsulLoadExceptionContext>` that can be used to configure how exceptions thrown during the first load should be handled.
83
+
***`OnWatchException`**
84
+
85
+
An `Action<ConsulWatchExceptionContext>` that can be used to configure how exceptions thrown when watching for changes should be handled.
59
86
***`Optional`**
60
87
61
-
A `bool` that indicates whether the config is optional. If `false` then will throw during load if the config is missing for the given key.
88
+
A `bool` that indicates whether the config is optional. If `false` then it will throw during the first load if the config is missing for the given key. Defaults to `false`.
62
89
***`Parser`**
63
90
64
-
The parser to use, should match the format of the configuration stored in Consul. Defaults to `JsonConfigurationParser`. Either use those under `Winton.Extensions.Configuration.Consul.Parsers` or create your own by implementing `IConfigurationParser`.
91
+
The parser to use, which should match the format of the configuration stored in Consul. Defaults to `JsonConfigurationParser`. Either use those under `Winton.Extensions.Configuration.Consul.Parsers` or create your own by implementing `IConfigurationParser`.
65
92
***`ReloadOnChange`**
66
93
67
94
A `bool` indicating whether to reload the config when it changes in Consul.
68
-
If `true` it will watch the configured key for changes and then reload the config asynchronously and trigger the `IChangeToken` to raise the event that the config has been reloaded.
95
+
If `true` it will watch the configured key for changes. When a change occurs the config will be asynchronously reloaded and the `IChangeToken` will be triggered to signal that the config has been reloaded. Defaults to `false`.
96
+
97
+
## Storing Config as Expanded Keys In Consul
98
+
99
+
By default this configuration provider will load all key-value pairs from Consul under the specified root key, but by default it assumes that the values of the leaf keys are encoded as JSON.
100
+
101
+
Take the following example of a particular instance of the Consul KV store:
102
+
```
103
+
- myApp/
104
+
- auth/
105
+
{
106
+
"appId": "guid",
107
+
"claims": [
108
+
"email",
109
+
"name"
110
+
]
111
+
}
112
+
- logging/
113
+
{
114
+
"level": "warn"
115
+
}
116
+
```
69
117
70
-
## Backlog
71
-
* Add more parsers for different file formats
72
-
* Add support for expanded configuration where the configuration is a tree of KV pairs under the root key
118
+
In this instance we could add Consul as a configuration source like so:
119
+
120
+
```csharp
121
+
varconfiguration=builder
122
+
.AddConsul("myApp", cancellationToken)
123
+
.Build();
124
+
```
125
+
126
+
The resultant configuration would contain sections for `auth` and `logging`. As a concrete example `configuration.GetValue<string>("logging:level")` would return `"warn"` and `configuration.GetValue<string>("auth:claims:0")` would return `"email"`.
127
+
128
+
Sometimes however, config in Consul is stored as a set of expanded keys. For instance, tools such as `consul-cli` load config in this format.
129
+
130
+
The config in this case can be thought of as a tree under a specific root key in Consul. For instance, continuing with the example above, the config would be stored as:
131
+
132
+
```
133
+
- myApp/
134
+
- auth/
135
+
- appId/
136
+
"guid"
137
+
- claims/
138
+
0/
139
+
"email"
140
+
1/
141
+
"name"
142
+
- logging/
143
+
- level/
144
+
"warn"
145
+
```
146
+
147
+
As outlined above this configuration provider deals with recursive keys by default. The only difference here is that the values are no longer encoded as JSON. Therefore, in order to load this config the parser must be changed. This can be done like so when adding the configuration provider:
148
+
149
+
```csharp
150
+
builder
151
+
.AddConsul(
152
+
"myApp",
153
+
cancellationToken,
154
+
options=>
155
+
{
156
+
options.Parser=newSimpleConfigurationParser();
157
+
});
158
+
```
159
+
160
+
The `SimpleConfigurationParser` expects to encounter a scalar value at each leaf key in the tree.
161
+
162
+
If you need to support both expanded keys and JSON values then this can be achieved by putting them under different root keys and adding multiple configuration sources. For example:
0 commit comments