Descubrimiento de Servicios

Registro de servicios

Escrito por Darjus Kazakevic · revisado el

Addresses are data you look up at call time, not settings you deploy.

With autoscaling and frequent deploys, servers get new addresses many times a day, so a fixed host list is soon wrong. Service discovery turns the address into a lookup. Each instance registers with a registry when it starts, sends heartbeats while it runs, and is removed when they stop.

Callers ask the registry for “payments” and get the live set. Consul, etcd, ZooKeeper, Eureka and Kubernetes Services all do this. The cost is a new critical system: if the registry is down, or a caller caches it too long, calls go to dead addresses.

En esencia — Let instances register themselves and send heartbeats, so callers look up a live address when they call instead of reading it from config.

Cuándo recurrir a él

Recurre a él cuando

  • Instances come and go with autoscaling, deploys and failures
  • Callers need the live set of addresses, not a list baked into config
  • Many services in several languages must find each other

Reconsidéralo cuando

  • Your platform already gives each service a stable DNS name
  • A few long-lived hosts whose addresses rarely change
  • A single service called only through one public load balancer

Conceptos clave

Registry
A small, highly available database that maps a service name to its live instances and their health. It usually runs as a cluster of 3 or 5 nodes using consensus, so it survives one or two failures.
Registration and heartbeats (Leases)
An instance adds itself on start and renews a lease every few seconds. If renewals stop for longer than the TTL, the registry removes it. In Kubernetes, the platform registers pods for you.
Client-side discovery
The caller asks the registry for the list and picks an instance itself, with its own load balancing. It saves a hop, but every client, in every language, needs this logic.
Server-side discovery
The caller sends to one stable name or load balancer, and that component reads the registry and picks an instance. Clients stay simple. Kubernetes Services and cloud target groups work this way.
DNS-based discovery
The registry answers DNS queries, such as payments.service.consul, so any client can use it without a library. Keep TTLs short, around 5–30 seconds. Some clients cache DNS far longer than the TTL.

Reglas de diseño

Look up at call time, cache for seconds
A cached address outlives the instance behind it. Keep lookups for seconds, not minutes. On a connection error, drop the cache and look up again.
Prefer the platform's server-side discovery
If Kubernetes or your cloud gives each service a stable name or balancer, use it. Client-side discovery puts lookup and balancing code in every caller and every language.
Deregister before you stop
On shutdown, remove the instance from the registry first, wait a few seconds for callers to notice, then stop. Otherwise callers hit it until the TTL runs out.
Keep working when the registry is down
Run the registry on 3 or 5 nodes across zones. If it is still unreachable, callers should keep using their last known list instead of failing every call.

Errores habituales

  • An HTTP client or JVM that caches DNS for minutes, or forever. The registry is right, but the client keeps calling old addresses.
  • No deregistration on shutdown. During every deploy, callers hit stopped instances until the TTL expires, and error rates jump.
  • A heartbeat sent by a background thread while the app itself is stuck. The instance looks alive but serves nothing. Tie the check to real work.
  • Callers that fail every call when the registry is unreachable, instead of using the last list they had.