11# HashiCorp Consul discovery client
22
3- The Steeltoe Consul discovery client lets an application register/ unregister itself with a Consul server
3+ The Steeltoe Consul discovery client allows an application to register and unregister itself with a Consul server
44and provides querying for service instances registered by other applications.
55
6- Once activated, the client registers the currently running app with Consul and starts a background thread that
6+ After it is activated, the client registers the currently running app with Consul and starts a background thread that
77periodically sends a TTL heartbeat to the Consul server (this is optional).
8- The client fetches no service registrations until asked for, and doesn't cache them.
8+ The client doesn't fetch service registrations until they are asked for, and it doesn't cache them.
99
10- ## Usage
10+ ## Using HashiCorp Consul discovery client
1111
12- To use this discovery client, add a NuGet package reference to ` Steeltoe.Discovery.Consul ` and initialize it from your ` Program.cs ` :
12+ To use this discovery client, add a NuGet package reference to ` Steeltoe.Discovery.Consul ` and initialize it from ` Program.cs ` .
1313
1414``` csharp
1515var builder = WebApplication .CreateBuilder (args );
@@ -22,85 +22,88 @@ var app = builder.Build();
2222
2323## Configuration settings
2424
25- To get the Steeltoe discovery client to properly communicate with the Consul server, you need to provide
26- a few configuration settings. There are several sections you may need to configure.
25+ To get the Steeltoe discovery client to properly communicate with the Consul server, there are various settings and sections to configure.
2726What you provide depends on whether you want your application to register the running app and/or
2827whether it also needs to query for other apps.
2928
30- The first section pertains to configuring the information needed to connect to the Consul server.
31- All of these settings should start with ` Consul: ` .
29+ The first section is for configuring the information needed to connect to the Consul server.
30+ All of these settings must start with ` Consul: ` .
31+
32+ | Key | Description | Default |
33+ | ------ | ----------- | ------- |
34+ | ` Host ` | Hostname or IP address of the Consul server | ` localhost ` |
35+ | ` Port ` | Port number the Consul server is listening on | ` 8500 ` |
36+ | ` Scheme ` | Scheme to connect with the Consul server (` http ` or ` https ` ) | ` http ` |
37+ | ` Datacenter ` | The datacenter name passed in each request to the server | |
38+ | ` Token ` | Authentication token passed in each request to the server | |
39+ | ` WaitTime ` | The maximum duration for a blocking request | |
40+ | ` Username ` | Username for HTTP authentication | |
41+ | ` Password ` | Password for HTTP authentication | |
42+ | ` Discovery:Enabled ` | Whether to enable the Consul client | ` true ` |
43+
44+ The second section (optional) pertains to registering the running app.
45+ All of these settings must start with ` Consul:Discovery: ` .
46+
47+ | Key | Description | Default |
48+ | ---------- | ----------- | ------- |
49+ | ` Register ` | Whether to register the running app as a service instance | ` true ` |
50+ | ` FailFast ` | Throw an exception (instead of logging an error) if registration fails | ` true ` |
51+ | ` Retry:Enabled ` | Whether to try again when registering the running app fails | ` false ` |
52+ | ` Retry:MaxAttempts ` | Maximum number of registration attempts (if retries are enabled) | ` 6 ` |
53+ | ` Retry:InitialInterval ` | The time to wait (in milliseconds) after the first registration failure | ` 1000 ` |
54+ | ` Retry:Multiplier ` | The factor to use when incrementing the next interval | ` 1.1 ` |
55+ | ` Retry:MaxInterval ` | Upper bound (in milliseconds) for intervals | ` 2000 ` |
56+ | ` Deregister ` | Whether to de-register the running app on shutdown | ` true ` |
57+ | ` ServiceName ` | Friendly name with which to register the running app | computed |
58+ | ` Scheme ` | Scheme with which to register the running app (` http ` or ` https ` ) | ` http ` |
59+ | ` HostName ` | Hostname with which to register the running app (if ` PreferIPAddress ` is ` false ` ) | computed |
60+ | ` IPAddress ` | IP address with which to register the running app (if ` PreferIPAddress ` is ` true ` ) | computed |
61+ | ` UseNetworkInterfaces ` | Query the operating system for network interfaces to determine ` HostName ` and ` IPAddress ` | ` false ` |
62+ | ` PreferIPAddress ` | Register the running app with IP address instead of hostname | ` false ` |
63+ | ` Port ` | Port number with which to register the running app | |
64+ | ` UseAspNetCoreUrls ` | Register with the port number ASP.NET Core is listening on | ` true ` |
65+ | ` InstanceId ` | The unique ID under which to register the running app | computed |
66+ | ` Tags ` | Array of tags used when registering the running app | |
67+ | ` Meta ` | Metadata key/value pairs used when registering the running app | see [ Configuring metadata] ( #configuring-metadata ) |
68+ | ` InstanceGroup ` | Metadata ` group ` value to use when registering the running app | |
69+ | ` InstanceZone ` | Metadata zone value to use when registering the running app | |
70+ | ` DefaultZoneMetadataName ` | Metadata key name for ` InstanceZone ` | ` zone ` |
71+ | ` RegisterHealthCheck ` | Whether to enable periodic health checking for the running app | ` true ` |
72+ | ` HealthCheckCriticalTimeout ` | Duration after which Consul deregisters the running app when in state ` critical ` [ ^ 1 ] | ` 30m ` |
73+ | ` Heartbeat:Enabled ` | Whether the running app periodically sends TTL (time-to-live) heartbeats [ ^ 1 ] | ` true ` |
74+ | ` Heartbeat:TtlValue ` | How often a TTL heartbeat must be sent to be considered healthy; used with ` Heartbeat:TtlUnit ` | every ` 30 ` seconds |
75+ | ` Heartbeat:TtlUnit ` | Unit for ` TtlValue ` (` ms ` , ` s ` , ` m ` or ` h ` ) | ` s ` (seconds) |
76+ | ` Heartbeat:IntervalRatio ` | Rate at which the running app sends TTL heartbeats, relative to ` TtlValue ` with ` TtlUnit ` | ` 0.66 ` |
77+ | ` HealthCheckPath ` | Relative URL to the health endpoint of the running app [ ^ 2 ] | ` /actuator/health ` |
78+ | ` HealthCheckUrl ` | Absolute URL to the health endpoint of the running app (overrides ` HealthCheckPath ` ) [ ^ 2 ] | |
79+ | ` HealthCheckTlsSkipVerify ` | Whether Consul should skip TLS verification for HTTP health checks [ ^ 2 ] | ` false ` |
80+ | ` HealthCheckInterval ` | How often Consul should perform HTTP health checks [ ^ 2 ] | ` 10s ` |
81+ | ` HealthCheckTimeout ` | The timeout Consul should use for HTTP health checks [ ^ 2 ] | ` 10s ` |
82+
83+ [ ^ 1 ] : This setting only affects operation when ` RegisterHealthCheck ` is ` true `
84+
85+ [ ^ 2 ] : This setting only affects operation when ` RegisterHealthCheck ` is ` true ` and ` Heartbeat:Enabled ` is ` false `
86+
87+ This section pertains to querying for app instances.
88+ All of these settings must start with ` Consul:Discovery: ` .
3289
3390| Key | Description | Default |
34- | --- | --- | --- |
35- | ` Host ` | Hostname or IP address of the Consul server. | ` localhost ` |
36- | ` Port ` | Port number the Consul server is listening on. | ` 8500 ` |
37- | ` Scheme ` | Scheme to connect with the Consul server (` http ` or ` https ` ). | ` http ` |
38- | ` Datacenter ` | The datacenter name passed in each request to the server. | |
39- | ` Token ` | Authentication token passed in each request to the server. | |
40- | ` WaitTime ` | The maximum duration for a blocking request. | |
41- | ` Username ` | Username for HTTP authentication. | |
42- | ` Password ` | Password for HTTP authentication. | |
43- | ` Discovery:Enabled ` | Whether to enable the Consul client. | ` true ` |
44-
45- The second section you may need to specify pertains to registering the running app.
46- All of these settings should start with ` Consul:Discovery: ` .
91+ | --- | ----------- | ------- |
92+ | ` DefaultQueryTag ` | The tag to filter on when querying for service instances | |
93+ | ` QueryPassing ` | Filter on health status passing when querying for service instances | ` true ` |
4794
48- | Key | Description | Default |
49- | --- | --- | --- |
50- | ` Register ` | Whether to register the running app as a service instance. | ` true ` |
51- | ` FailFast ` | Throw an exception (instead of logging an error) if registration fails. | ` true ` |
52- | ` Retry:Enabled ` | Whether to try again when registering the running app fails. | ` false ` |
53- | ` Retry:MaxAttempts ` | Maximum number of registration attempts (if retries are enabled). | ` 6 ` |
54- | ` Retry:InitialInterval ` | The time to wait (in milliseconds) after the first registration failure. | ` 1000 ` |
55- | ` Retry:Multiplier ` | The factor to increment the next interval with. | ` 1.1 ` |
56- | ` Retry:MaxInterval ` | Upper bound (in milliseconds) for intervals. | ` 2000 ` |
57- | ` Deregister ` | Whether to de-register the running app on shutdown. | ` true ` |
58- | ` ServiceName ` | Friendly name to register the running app with. | computed |
59- | ` Scheme ` | Scheme to register the running app with (` http ` or ` https ` ). | ` http ` |
60- | ` HostName ` | Hostname to register the running app with (if ` PreferIPAddress ` is ` false ` ). | computed |
61- | ` IPAddress ` | IP address to register the running app with (if ` PreferIPAddress ` is ` true ` ). | computed |
62- | ` UseNetworkInterfaces ` | Query the operating system for network interfaces to determine ` HostName ` and ` IPAddress ` . | ` false ` |
63- | ` PreferIPAddress ` | Register the running app with IP address instead of hostname. | ` false ` |
64- | ` Port ` | Port number to register the running app with. | |
65- | ` UseAspNetCoreUrls ` | Register with the port number ASP.NET Core is listening on. | ` true ` |
66- | ` InstanceId ` | The unique ID to register the running app under. | computed |
67- | ` Tags ` | Array of tags used when registering the running app. | |
68- | ` Meta ` | Metadata key/value pairs used when registering the running app. | see [ Configuring metadata] ( #configuring-metadata ) |
69- | ` InstanceGroup ` | Metadata ` group ` value to use when registering the running app. | |
70- | ` InstanceZone ` | Metadata zone value to use when registering the running app. | |
71- | ` DefaultZoneMetadataName ` | Metadata key name for ` InstanceZone ` . | ` zone ` |
72- | ` RegisterHealthCheck ` | Whether to enable periodic health checking for the running app. | ` true ` |
73- | ` HealthCheckCriticalTimeout ` | Duration after which Consul deregisters the running app when in state ` critical ` . [ ^ 1 ] | ` 30m ` |
74- | ` Heartbeat:Enabled ` | Whether the running app periodically sends TTL (time-to-live) heartbeats. [ ^ 1 ] | ` true ` |
75- | ` Heartbeat:TtlValue ` | How often a TTL heartbeat must be sent to be considered healthy. | ` 30 ` |
76- | ` Heartbeat:TtlUnit ` | Unit for ` TtlValue ` (` ms ` , ` s ` , ` m ` or ` h ` ) | ` s ` |
77- | ` Heartbeat:IntervalRatio ` | Rate at which the running app sends TTL heartbeats, relative to ` TtlValue ` with ` TtlUnit ` . | ` 0.66 ` |
78- | ` HealthCheckPath ` | Relative URL to the health endpoint of the running app. [ ^ 2 ] | ` /actuator/health ` |
79- | ` HealthCheckUrl ` | Absolute URL to the health endpoint of the running app (overrides ` HealthCheckPath ` ). [ ^ 2 ] | |
80- | ` HealthCheckTlsSkipVerify ` | Whether Consul should skip TLS verification for HTTP health checks. [ ^ 2 ] | ` false ` |
81- | ` HealthCheckInterval ` | How often Consul should perform an HTTP health check. [ ^ 2 ] | ` 10s ` |
82- | ` HealthCheckTimeout ` | The timeout Consul should use for an HTTP health check. [ ^ 2 ] | ` 10s ` |
83-
84- [ ^ 1 ] : This setting only has effect when ` RegisterHealthCheck ` is ` true `
85- [ ^ 2 ] : This setting only has effect when ` RegisterHealthCheck ` is ` true ` and ` Heartbeat:Enabled ` is ` false `
86-
87- The last section pertains to querying for app instances.
88- All of these settings should start with ` Consul:Discovery: ` .
89-
90- | Key | Description | Default |
91- | --- | --- | --- |
92- | ` DefaultQueryTag ` | The tag to filter on when querying for service instances. | |
93- | ` QueryPassing ` | Filter on health status 'passing' when querying for service instances. | ` true ` |
95+ For more information about these settings, see the Consul documentation:
9496
95- For a deeper understanding of these settings, see the Consul documentation on
96- [ registering services] ( https://developer.hashicorp.com/consul/api-docs/agent/service#register-service ) ,
97- [ health setup during registration] ( https://developer.hashicorp.com/consul/api-docs/agent/check#register-check ) and
98- [ filtering service instances] ( https://developer.hashicorp.com/consul/api-docs/health ) .
97+ * [ Registering services] ( https://developer.hashicorp.com/consul/api-docs/agent/service#register-service ) ,
98+ * [ Health setup during registration] ( https://developer.hashicorp.com/consul/api-docs/agent/check#register-check ) and
99+ * [ Filtering service instances] ( https://developer.hashicorp.com/consul/api-docs/health ) .
99100
100101### Configuring metadata
101102
102103Steeltoe sends metadata (string-based key/value pairs) when registering the currently running app.
103- Additional metadata can be added in configuration, for example:
104+ Additional metadata can be added in configuration.
105+
106+ For example:
104107
105108``` json
106109{
@@ -117,13 +120,13 @@ Additional metadata can be added in configuration, for example:
117120
118121By default, the following metadata is added:
119122
120- | Key | Value |
121- | - | - |
122- | ` group ` | Value from ` Consul:Discovery:InstanceGroup ` |
123- | Value from ` Consul:Discovery:DefaultZoneMetadataName ` | Value from ` Consul:Discovery:InstanceZone ` |
123+ | Key | Value |
124+ | --------- | ---- - |
125+ | ` group ` | Value from ` Consul:Discovery:InstanceGroup ` |
126+ | Value from ` Consul:Discovery:DefaultZoneMetadataName ` | Value from ` Consul:Discovery:InstanceZone ` |
124127| ` secure ` | Value at ` Consul:Discovery:Scheme ` equals ` https ` |
125128
126129## Health Contributor
127130
128- The ` Steeltoe.Discovery.Consul ` package includes an ` IHealthContributor ` that verifies the Consul server is reachable.
131+ The ` Steeltoe.Discovery.Consul ` package includes an ` IHealthContributor ` that verifies that the Consul server is reachable.
129132This health contributor is automatically added to the service container.
0 commit comments