การค้นหาบริการและการตรวจสอบสถานะ
เรียนรู้ว่าไมโครเซอร์วิสค้นหากันแบบไดนามิกอย่างไร และการตรวจสอบสถานะช่วยให้ทราฟฟิกไหลไปยังอินสแตนซ์ที่พร้อมใช้งานเท่านั้นอย่างไร
การค้นหาบริการและการตรวจสอบสถานะ เป็นบทเรียน FastAPI Backend Development Bootcamp ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.
คำถามที่พบบ่อย
บทเรียน “การค้นหาบริการและการตรวจสอบสถานะ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การค้นหาบริการและการตรวจสอบสถานะ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส FastAPI Backend Development Bootcamp ให้อัปเกรดเป็น CoddyKit PRO คอร์ส FastAPI Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การค้นหาบริการและการตรวจสอบสถานะ”
เรียนรู้ว่าไมโครเซอร์วิสค้นหากันแบบไดนามิกอย่างไร และการตรวจสอบสถานะช่วยให้ทราฟฟิกไหลไปยังอินสแตนซ์ที่พร้อมใช้งานเท่านั้นอย่างไร คุณปฏิบัติ FastAPI Backend Development Bootcamp ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน FastAPI Backend Development Bootcamp หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน FastAPI Backend Development Bootcamp บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การค้นหาบริการและการตรวจสอบสถานะ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน FastAPI Backend Development Bootcamp นี้ได้ไหม
ได้ บทเรียน FastAPI Backend Development Bootcamp ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การออกแบบสถาปัตยกรรมไมโครเซอร์วิส
- การสื่อสารระหว่างบริการ
- การใช้งานเกตเวย์ API ด้วย FastAPI
- การค้นหาบริการและการตรวจสอบสถานะ