Consul

The discovery based on Consul.

This driver implement a client side service discovery.

exception blacksmith.sd._async.adapters.consul.ConsulApiError(exc: HTTPError)

Raised when consul API is not responding what is expected.

class blacksmith.sd._async.adapters.consul.ServiceRequest(*, name: str)

Request parameter of the Consul API to retrieve a host for a service.

name: str

Name of the service to search for an endpoint.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class blacksmith.sd._async.adapters.consul.Service(*, Address: str, ServiceAddress: str | None = None, ServicePort: int)

Consul Service response.

node_address: str

IP address of the Consul node on which the service is registered.

service_address: str | None

IP address of the service host. if empty, node address is used.

port: int

TCP Port of an instance that host the service.

property address: str
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

blacksmith.sd._async.adapters.consul.blacksmith_cli(endpoint: str, consul_token: str) AsyncClientFactory[HTTPError]
class blacksmith.sd._async.adapters.consul.AsyncConsulDiscovery(addr: str = 'http://consul:8500/v1', service_name_fmt: str = '{service}-{version}', service_url_fmt: str = 'http://{address}:{port}/{version}', unversioned_service_name_fmt: str = '{service}', unversioned_service_url_fmt: str = 'http://{address}:{port}', consul_token: str = '', _client_factory: ~typing.Callable[[str, str], ~blacksmith.service._async.client.AsyncClientFactory[~typing.Any]] = <function blacksmith_cli>)

A discovery instance based on a Consul server.

Parameters:
  • addr – endpoint of the consul v1 http api.

  • service_name_fmt – pattern for name of versionned service.

  • service_url_fmt – pattern for url of versionned service.

  • unversioned_service_name_fmt – pattern for name of unversioned service.

  • unversioned_service_url_fmt – pattern for url of unversioned service.

  • consul_token – If set, the consul token is sent on http api call.

addr: str
consul_token: str
service_name_fmt: str
service_url_fmt: str
unversioned_service_name_fmt: str
unversioned_service_url_fmt: str
format_service_name(service: str, version: str | None) str

Build the service name to send to consul.

format_endoint(version: str | None, address: str, port: int) str

Build the rest api endpoint from consul response.

async resolve(service: str, version: str | None) Service

Get the Service from the consul registry.

If many instances host the service, the host is choosen randomly.

async get_endpoint(service: str, version: str | None) str

Get the endpoint from the consul registry

If many instances host the service, the host is choosen randomly.