Table of Contents

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

client IConsulClient

The Consul client.

optionsMonitor IOptionsMonitor<ConsulDiscoveryOptions>

Provides access to ConsulDiscoveryOptions.

loggerFactory ILoggerFactory

Used 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

client IConsulClient

The Consul client.

optionsMonitor IOptionsMonitor<ConsulDiscoveryOptions>

Provides access to ConsulDiscoveryOptions.

timeProvider TimeProvider

Provides access to the system time, which schedules the heartbeats.

loggerFactory ILoggerFactory

Used 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

string

Methods

GetAllInstancesAsync(QueryOptions, CancellationToken)

Gets all service instances from the Consul catalog.

public Task<IList<IServiceInstance>> GetAllInstancesAsync(QueryOptions queryOptions, CancellationToken cancellationToken)

Parameters

queryOptions QueryOptions

Any Consul query options to use.

cancellationToken CancellationToken

The 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

serviceId string

The ID of the service to lookup.

queryOptions QueryOptions

Any Consul query options to use.

cancellationToken CancellationToken

The 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

serviceId string

The ID of the service to lookup.

cancellationToken CancellationToken

The 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 null when 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

dataCenter string

Specifies the datacenter to query.

filter Filter

Specifies the expression used to filter the queries results prior to returning the data.

queryOptions QueryOptions

Options to parameterize the Consul query.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<ISet<string>>

The list of service IDs.

GetServiceIdsAsync(CancellationToken)

Gets all registered service IDs from the discovery server.

public Task<ISet<string>> GetServiceIdsAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<ISet<string>>

The list of service IDs.

ShutdownAsync(CancellationToken)

Deregisters the local service (this app) from the discovery server.

public Task ShutdownAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task

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

Event Type

EventHandler<DiscoveryInstancesFetchedEventArgs>