اكتشاف الخدمات وفحوصات الصحة
تعلّم كيفية عثور الخدمات المصغرة على بعضها ديناميكيًا، وكيف تحافظ فحوصات الصحة على توجيه حركة المرور إلى المثيلات السليمة فقط.
اكتشاف الخدمات وفحوصات الصحة درس مجاني في FastAPI Backend Development Bootcamp على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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.
الأسئلة الشائعة
هل درس «اكتشاف الخدمات وفحوصات الصحة» مجاني؟
نعم — نص درس «اكتشاف الخدمات وفحوصات الصحة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة FastAPI Backend Development Bootcamp، انتقل إلى CoddyKit PRO. تتضمن دورة FastAPI Backend Development Bootcamp 4 دروس في المجموع.
ماذا ستتعلم في «اكتشاف الخدمات وفحوصات الصحة»؟
تعلّم كيفية عثور الخدمات المصغرة على بعضها ديناميكيًا، وكيف تحافظ فحوصات الصحة على توجيه حركة المرور إلى المثيلات السليمة فقط. تتمرن على FastAPI Backend Development Bootcamp مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ FastAPI Backend Development Bootcamp؟
لا تُشترط خبرة سابقة. FastAPI Backend Development Bootcamp على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «اكتشاف الخدمات وفحوصات الصحة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس FastAPI Backend Development Bootcamp هذا؟
نعم. كل درس في FastAPI Backend Development Bootcamp يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تصميم بنية الخدمات المصغّرة
- الاتصال بين الخدمات
- تنفيذ بوابة API باستخدام FastAPI
- اكتشاف الخدمات وفحوصات الصحة