0Pricing
Clojure Functional Programming & JVM Backend Development · درس

اكتشاف الخدمات وبوابات API

تعلّم كيفية عثور الخدمات المصغّرة في Clojure على بعضها ديناميكيًا، وكيف توفر بوابة API نقطة دخول واحدة وآمنة إلى نظامك.

اكتشاف الخدمات وبوابات API درس مجاني في Clojure Functional Programming & JVM Backend Development على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Clojure Functional Programming & JVM Backend Development، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Clojure Functional Programming & JVM Backend Development 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

The Discovery Problem

In a microservice system, instances start, stop, and move between hosts. Hardcoding IP addresses breaks quickly. Service discovery lets services locate each other dynamically.

Service Registries

A service registry is a database of available services and their network locations. Popular options include Consul, etcd, and Eureka.

Registering a Service

On startup, a service registers itself with the registry, including a health-check endpoint so the registry can drop it if it dies.

(defn register! [consul-url name host port]
  (http/put (str consul-url "/v1/agent/service/register")
    {:body (json/write-str
             {:Name name :Address host :Port port
              :Check {:HTTP (str "http://" host ":" port "/health")
                      :Interval "10s"}})}))

Client-Side Discovery

In client-side discovery, the calling service queries the registry for healthy instances and picks one itself.

(defn lookup [consul-url name]
  (-> (http/get (str consul-url "/v1/health/service/" name "?passing"))
      :body
      json/read-str))

Server-Side Discovery

In server-side discovery, the client calls a load balancer or gateway, which consults the registry and forwards the request. The client stays simple.

What Is an API Gateway?

An API gateway is a single entry point that routes incoming requests to the right backend service. It hides internal topology from clients.

Gateway Responsibilities

Beyond routing, a gateway often handles cross-cutting concerns:

  • Authentication and authorization
  • Rate limiting
  • Request/response transformation
  • Aggregating multiple service calls

A Simple Gateway in Ring

You can build a basic gateway in Clojure by matching the path and proxying to the discovered service.

(defn gateway [request]
  (let [service (route-for (:uri request))
        target  (lookup-instance service)]
    (http/request
      (assoc request :url (str target (:uri request))))))

Authentication at the Edge

Centralizing auth at the gateway means individual services trust the gateway and avoid duplicating login logic.

(defn wrap-gateway-auth [handler]
  (fn [request]
    (if (valid-token? (get-in request [:headers "authorization"]))
      (handler request)
      {:status 401 :body "Unauthorized"})))

Rate Limiting

The gateway is the natural place to throttle abusive clients before requests reach your services.

(defn wrap-rate-limit [handler limit]
  (fn [request]
    (if (under-limit? (client-id request) limit)
      (handler request)
      {:status 429 :body "Too Many Requests"})))

Putting It Together

A production setup: services self-register in a registry with health checks, and a gateway discovers them, enforces auth and rate limits, then routes. This keeps clients decoupled from internal changes.

Quick Check

Test your discovery knowledge.

Recap

You learned how services discover each other and how a gateway unifies access.

  • Registries like Consul track instances via health checks
  • Discovery can be client-side or server-side
  • Gateways centralize routing, auth, and rate limiting

الأسئلة الشائعة

هل درس «اكتشاف الخدمات وبوابات API» مجاني؟

نعم — نص درس «اكتشاف الخدمات وبوابات API» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Clojure Functional Programming & JVM Backend Development، انتقل إلى CoddyKit PRO. تتضمن دورة Clojure Functional Programming & JVM Backend Development 4 دروس في المجموع.

ماذا ستتعلم في «اكتشاف الخدمات وبوابات API»؟

تعلّم كيفية عثور الخدمات المصغّرة في Clojure على بعضها ديناميكيًا، وكيف توفر بوابة API نقطة دخول واحدة وآمنة إلى نظامك. تتمرن على Clojure Functional Programming & JVM Backend Development مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Clojure Functional Programming & JVM Backend Development؟

لا تُشترط خبرة سابقة. Clojure Functional Programming & JVM Backend Development على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «اكتشاف الخدمات وبوابات API»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Clojure Functional Programming & JVM Backend Development هذا؟

نعم. كل درس في Clojure Functional Programming & JVM Backend Development يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. تصميم الخدمات المصغّرة باستخدام Clojure
  2. تحويل التطبيقات إلى حاويات باستخدام Docker
  3. النشر على المنصات السحابية
  4. اكتشاف الخدمات وبوابات API
← العودة إلى Clojure Functional Programming & JVM Backend Development