Service Discovery und Health Checks
Lernen Sie, wie Microservices einander dynamisch finden und wie Health Checks den Datenverkehr nur zu gesunden Instanzen leiten.
Service Discovery und Health Checks ist eine kostenlose FastAPI Backend Development Bootcamp-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des FastAPI Backend Development Bootcamp-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der FastAPI Backend Development Bootcamp-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
The Discovery Problem
In a microservices system, instances start, stop, and move across hosts constantly. Hardcoding IP addresses breaks immediately.
Service discovery lets a service find healthy instances of another service by name at runtime.
Client-Side vs Server-Side
Two patterns exist:
- Client-side: the caller queries a registry and picks an instance
- Server-side: a load balancer or gateway resolves the name and forwards the request
Service Registries
A registry stores live instances. Popular options include Consul, etcd, and Kubernetes DNS.
Services register on startup and deregister on shutdown.
Registering a Service
On boot, a service announces its address to the registry.
import httpx
async def register():
await httpx.AsyncClient().put(
"http://consul:8500/v1/agent/service/register",
json={"Name": "orders", "Address": "10.0.0.5", "Port": 8000})Why Health Checks Matter
A registered instance might still be broken. Health checks ensure the registry only routes to instances that are actually able to serve requests.
A FastAPI Health Endpoint
Expose a lightweight endpoint that returns quickly when the service is healthy.
from fastapi import FastAPI
app = FastAPI()
@app.get("/health")
def health():
return {"status": "ok"}Liveness vs Readiness
Two distinct checks:
- Liveness: is the process alive? If not, restart it.
- Readiness: can it serve traffic now with DB connected and caches warm? If not, hold traffic.
A Readiness Check
Readiness verifies downstream dependencies before accepting traffic.
from fastapi import Response, status
@app.get("/ready")
async def ready(response: Response):
if not await db_is_up():
response.status_code = status.HTTP_503_SERVICE_UNAVAILABLE
return {"ready": False}
return {"ready": True}Kubernetes Probes
Kubernetes uses these endpoints as probes to manage pods automatically.
livenessProbe:
httpGet:
path: /health
port: 8000
readinessProbe:
httpGet:
path: /ready
port: 8000Graceful Deregistration
On shutdown, deregister so callers stop routing to a dying instance, and finish in-flight requests first.
@app.on_event("shutdown")
async def shutdown():
await deregister_from_registry()TTL and Stale Entries
Registry entries usually carry a TTL. If a service stops sending heartbeats, its entry expires automatically so callers stop routing to a dead instance.
Quick Check
Test your discovery knowledge.
Recap
You learned how services find and trust each other:
- Service discovery resolves services by name via a registry
- Health checks ensure only healthy instances get traffic
- Liveness restarts, readiness gates traffic
Together they keep a dynamic microservices system resilient.
Häufig gestellte Fragen
Ist die Lektion „Service Discovery und Health Checks“ kostenlos?
Ja — der vollständige Text von „Service Discovery und Health Checks“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des FastAPI Backend Development Bootcamp-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der FastAPI Backend Development Bootcamp-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Service Discovery und Health Checks“?
Lernen Sie, wie Microservices einander dynamisch finden und wie Health Checks den Datenverkehr nur zu gesunden Instanzen leiten. Du übst FastAPI Backend Development Bootcamp mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um FastAPI Backend Development Bootcamp zu starten?
Keine Vorkenntnisse erforderlich. FastAPI Backend Development Bootcamp auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Service Discovery und Health Checks“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser FastAPI Backend Development Bootcamp-Lektion Code schreiben und ausführen?
Ja. Jede FastAPI Backend Development Bootcamp-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Microservices-Architektur entwerfen
- Kommunikation zwischen Diensten
- Ein API-Gateway mit FastAPI implementieren
- Service Discovery und Health Checks