Skip to content

Commit 85dd065

Browse files
authored
Update README. (#88)
1 parent 4fa073c commit 85dd065

2 files changed

Lines changed: 24 additions & 51 deletions

File tree

README.md

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,20 @@ Add `Winton.Extensions.Configuration.Consul` to your project's dependencies, eit
1414
## Usage
1515

1616
### 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`.
17+
18+
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-3.0) for more information about `IConfigurationBuilder`.
1819

1920
A minimal example is shown below:
2021

2122
```csharp
22-
var cancellationTokenSource = new cancellationTokenSource();
2323
builder
24-
.AddConsul(
25-
$"{env.ApplicationName}/{env.EnvironmentName}",
26-
cancellationTokenSource.Token);
24+
.AddConsul($"{env.ApplicationName}/{env.EnvironmentName}");
2725
```
2826

2927
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.
3028

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:
33-
34-
```csharp
35-
public static void Main(string[] args)
36-
{
37-
var cancellationTokenSource = new CancellationTokenSource();
38-
WebHost
39-
.CreateDefaultBuilder(args)
40-
.ConfigureAppConfiguration(
41-
builder => builder.AddConsul("key", cancellationTokenSource.Token))
42-
// Rest of webhost setup
43-
.Build()
44-
.Run();
45-
cancellationTokenSource.Cancel();
46-
}
47-
```
48-
49-
Or in the `Startup` class, for example:
50-
51-
```csharp
52-
public Startup()
53-
{
54-
_cancellationTokenSource = new cancellationTokenSource();
55-
var builder = new ConfigurationBuilder()
56-
.AddConsul("key", _cancellationTokenSource.Token);
57-
Configuration = builder.Build();
58-
}
59-
60-
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime appLifetime)
61-
{
62-
// Other app configuration
63-
64-
appLifetime.ApplicationStopping.Register(_cancellationTokenSource.Cancel);
65-
}
66-
```
67-
6829
### Options
30+
6931
`AddConsul` has an overload with an additional third parameter of type `Action<IConsulConfigurationSource>` which allows the options outlined below to be set.
7032

7133
* **`ConsulConfigurationOptions`**
@@ -77,20 +39,32 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplica
7739
* **`ConsulHttpClientOptions`**
7840

7941
An `Action<HttpClient>` that can be used to configure the underlying Consul client's HTTP options.
42+
* **`KeyToRemove`**
43+
44+
The portion of the Consul key to remove from the configuration keys.
45+
By default, when the configuration is parsed, the keys are created by removing the root key in Consul where the configuration is located.
46+
This defaults to `Key`.
8047
* **`OnLoadException`**
8148

8249
An `Action<ConsulLoadExceptionContext>` that can be used to configure how exceptions thrown during the first load should be handled.
8350
* **`OnWatchException`**
8451

8552
A `Func<ConsulWatchExceptionContext, TimeSpan>` that can be used to configure how exceptions thrown when watching for changes should be handled.
86-
The `TimeSpan` that is returned is used to set a delay before retrying.
87-
The `ConsulWatchExceptionContext` provides data that can be used to implement a backoff strategy or to cancel watching altogether.
53+
The `TimeSpan` that is returned is used to set a delay before retrying.
54+
The `ConsulWatchExceptionContext` provides data that can be used to implement a back-off strategy or to cancel watching altogether.
8855
* **`Optional`**
8956

9057
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`.
9158
* **`Parser`**
9259

9360
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`.
61+
* **`PollWaitTime`**
62+
63+
The amount of time the client should wait before timing out when polling for changes.
64+
If this is set too low it can lead to excessive requests being issued to Consul.
65+
Note this setting does not affect how quickly updates propagate, because when a value changes the long polling query returns immediately.
66+
It is better to think of this as the frequency with which it issues calls in the long polling loop in the case where there is no change.
67+
Defaults to 5 minutes.
9468
* **`ReloadOnChange`**
9569

9670
A `bool` indicating whether to reload the config when it changes in Consul.
@@ -101,10 +75,11 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplica
10175
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.
10276

10377
Take the following example of a particular instance of the Consul KV store:
78+
10479
```
10580
- myApp/
10681
- auth/
107-
{
82+
{
10883
"appId": "guid",
10984
"claims": [
11085
"email",
@@ -127,7 +102,7 @@ var configuration = builder
127102

128103
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"`.
129104

130-
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.
105+
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.
131106

132107
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:
133108

@@ -152,22 +127,20 @@ As outlined above this configuration provider deals with recursive keys by defau
152127
builder
153128
.AddConsul(
154129
"myApp",
155-
cancellationToken,
156130
options =>
157131
{
158132
options.Parser = new SimpleConfigurationParser();
159133
});
160134
```
161135

162-
The `SimpleConfigurationParser` expects to encounter a scalar value at each leaf key in the tree.
136+
The `SimpleConfigurationParser` expects to encounter a scalar value at each leaf key in the tree.
163137

164138
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:
165139

166140
```csharp
167141
builder
168142
.AddConsul(
169143
"myApp/expandedKeys",
170-
cancellationToken,
171144
options =>
172145
{
173146
options.Parser = new SimpleConfigurationParser();

src/Winton.Extensions.Configuration.Consul/IConsulConfigurationSource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ public interface IConsulConfigurationSource : IConfigurationSource
4343
string Key { get; }
4444

4545
/// <summary>
46-
/// Gets the portion of the Consul key to remove from the configuration keys.
46+
/// Gets or sets the portion of the Consul key to remove from the configuration keys.
4747
/// By default, when the configuration is parsed, the keys are created by removing the root key in Consul
4848
/// where the configuration is located.
4949
/// Defaults to <see cref="Key" />.
5050
/// </summary>
51-
string KeyToRemove { get; }
51+
string KeyToRemove { get; set; }
5252

5353
/// <summary>
5454
/// Gets or sets an <see cref="Action" /> that is invoked when an exception is raised during config load.

0 commit comments

Comments
 (0)