サービスディスカバリとヘルスチェック
マイクロサービスが動的に互いを発見する仕組みと、ヘルスチェックによって正常なインスタンスだけにトラフィックを流す方法を学びます。
「サービスディスカバリとヘルスチェック」はCoddyKit上の無料FastAPI Backend Development Bootcampレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFastAPI Backend Development Bootcamp学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 FastAPI Backend Development Bootcampコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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.
よくある質問
「サービスディスカバリとヘルスチェック」レッスンは無料ですか?
はい。「サービスディスカバリとヘルスチェック」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、FastAPI Backend Development Bootcampコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 FastAPI Backend Development Bootcampコースには全4レッスンが含まれています。
「サービスディスカバリとヘルスチェック」で何を学びますか?
マイクロサービスが動的に互いを発見する仕組みと、ヘルスチェックによって正常なインスタンスだけにトラフィックを流す方法を学びます。 ブラウザで直接実行するハンズオンコードでFastAPI Backend Development Bootcampを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
FastAPI Backend Development Bootcampを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFastAPI Backend Development Bootcampは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「サービスディスカバリとヘルスチェック」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFastAPI Backend Development Bootcampレッスンでコードを書いて実行できますか?
はい。すべてのFastAPI Backend Development Bootcampレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- マイクロサービスアーキテクチャの設計
- サービス間通信
- FastAPIによるAPI Gatewayの実装
- サービスディスカバリとヘルスチェック