moonsd service discovery

Backend · Consul

moonconsul

A complete Consul client, and moonconsul/sd: the contract as an ordinary Consul service with a ttl check. Other Consul clients see a normal registration; the contract sees a lease.

servicegreeter at 10.0.0.11:12000
idmoonsd_id in the service's metadata
checkservice:greeter:10.0.0.11:12000, ttl
renewcheck_pass
watcha blocking query on the health index, wait 30 s
lapsethe check goes critical; the agent reaps it after reap_after
i

What it is

The registration is a Consul registration

Consul's own fields carry the endpoint — address, port, tags, metadata — so an operator looking at the catalogue sees what they expect, and a client written in any language finds the service. The contract's id travels in metadata under moonsd_id, because Consul's service id has to be unique per agent and the contract only requires it to be unique per service.

ii

The lease

Renewing is passing the check

01

register

A service registration with a ttl check named after it. The token in the lease is {service}:{id}, which is enough to find the check again.

02

renew

agent().check_pass(check_id). A check that goes critical takes the instance out of resolve at once; Consul reaps the registration a minute later, which is only how long the corpse stays on the agent.

03

deregister

The service is deregistered with its check, and a registration that has already been reaped is answered as success rather than as an error.

iii

The watcher

Wait on an index, not on a timer

A health query takes wait_index and wait_time: the agent holds the request until the answer changes or the wait runs out, and returns the index that produced it. The watcher feeds that index into the next query, so a change arrives as soon as the agent knows it, and a quiet service costs one idle request every wait — thirty seconds, which is long enough not to be a poll and short enough that closing the watcher is noticed promptly.

wait 30 s reap after 1 m token {service}:{id} verified against Consul 1.20, two agents
iv

The client behind it

More than health checks

The client covers the API a deployment actually uses: agent, catalog, health, kv, sessions, events, ACLs, status, coordinates, prepared queries, config entries, Connect and its CA, plus locks and semaphores built on sessions. Blocking queries are a first-class primitive, with a watch loop that retries on backoff when an agent goes away.