Class ConsulDiscoveryClient
- Namespace
- Steeltoe.Discovery.Consul
- Assembly
- Steeltoe.Discovery.Consul.dll
A discovery client for HashiCorp Consul .
public sealed class ConsulDiscoveryClient : IDiscoveryClient
- Inheritance
-
ConsulDiscoveryClient
- Implements
- Inherited Members
Constructors
ConsulDiscoveryClient(IConsulClient, IOptionsMonitor<ConsulDiscoveryOptions>, ILoggerFactory)
Initializes a new instance of the ConsulDiscoveryClient class.
[Obsolete("This constructor will be removed in a future release. Use the constructor that takes a TimeProvider instead.")]
public ConsulDiscoveryClient(IConsulClient client, IOptionsMonitor<ConsulDiscoveryOptions> optionsMonitor, ILoggerFactory loggerFactory)
Parameters
clientIConsulClientThe Consul client.
optionsMonitorIOptionsMonitor<ConsulDiscoveryOptions>Provides access to ConsulDiscoveryOptions.
loggerFactoryILoggerFactoryUsed for internal logging. Pass Instance to disable logging.
ConsulDiscoveryClient(IConsulClient, IOptionsMonitor<ConsulDiscoveryOptions>, TimeProvider, ILoggerFactory)
Initializes a new instance of the ConsulDiscoveryClient class.
public ConsulDiscoveryClient(IConsulClient client, IOptionsMonitor<ConsulDiscoveryOptions> optionsMonitor, TimeProvider timeProvider, ILoggerFactory loggerFactory)
Parameters
clientIConsulClientThe Consul client.
optionsMonitorIOptionsMonitor<ConsulDiscoveryOptions>Provides access to ConsulDiscoveryOptions.
timeProviderTimeProviderProvides access to the system time, which schedules the heartbeats.
loggerFactoryILoggerFactoryUsed for internal logging. Pass Instance to disable logging.
Properties
Description
Gets a human-readable description of this discovery client.
public string Description { get; }
Property Value
Methods
GetAllInstancesAsync(QueryOptions, CancellationToken)
Gets all service instances from the Consul catalog.
public Task<IList<IServiceInstance>> GetAllInstancesAsync(QueryOptions queryOptions, CancellationToken cancellationToken)
Parameters
queryOptionsQueryOptionsAny Consul query options to use.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<IList<IServiceInstance>>
The list of remote service instances.
GetInstancesAsync(string, QueryOptions, CancellationToken)
Gets all service instances associated with the specified service ID from the Consul catalog.
public Task<IList<IServiceInstance>> GetInstancesAsync(string serviceId, QueryOptions queryOptions, CancellationToken cancellationToken)
Parameters
serviceIdstringThe ID of the service to lookup.
queryOptionsQueryOptionsAny Consul query options to use.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<IList<IServiceInstance>>
The list of remote service instances.
GetInstancesAsync(string, CancellationToken)
Gets all service instances associated with the specified service ID from the discovery server.
public Task<IList<IServiceInstance>> GetInstancesAsync(string serviceId, CancellationToken cancellationToken)
Parameters
serviceIdstringThe ID of the service to lookup.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<IList<IServiceInstance>>
The list of remote service instances.
GetLocalServiceInstance()
Gets information used to register the local service instance (this app) to the discovery server.
public IServiceInstance GetLocalServiceInstance()
Returns
- IServiceInstance
The service instance that represents this app, or
nullwhen unavailable.
GetServiceIdsAsync(string?, Filter?, QueryOptions, CancellationToken)
Gets all registered service IDs from the Consul catalog.
public Task<ISet<string>> GetServiceIdsAsync(string? dataCenter, Filter? filter, QueryOptions queryOptions, CancellationToken cancellationToken)
Parameters
dataCenterstringSpecifies the datacenter to query.
filterFilterSpecifies the expression used to filter the queries results prior to returning the data.
queryOptionsQueryOptionsOptions to parameterize the Consul query.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
GetServiceIdsAsync(CancellationToken)
Gets all registered service IDs from the discovery server.
public Task<ISet<string>> GetServiceIdsAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
ShutdownAsync(CancellationToken)
Deregisters the local service (this app) from the discovery server.
public Task ShutdownAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
Remarks
This method exists for two reasons, instead of just reusing IAsyncDisposable. The first reason is that this method enables
cancellation. The second reason is more complicated. Deregistration typically requires to send an HTTP request to the discovery server. When using
IHttpClientFactory, it requires the IoC container to obtain an HttpClient. But that fails when the container is being disposed.
Deregistration must be performed earlier. That's why implementations should register DiscoveryClientHostedService in the IoC container, which is a
hosted service that performs deregistration (by calling this method) when the app is terminating. At that time, the IoC container is still
accessible.
Events
InstancesFetched
This event is never raised. The Consul client doesn't implement caching.
public event EventHandler<DiscoveryInstancesFetchedEventArgs>? InstancesFetched