0Pricing
Clojure Functional Programming & JVM Backend Development · บทเรียน

การค้นพบบริการและเกตเวย์ API

เรียนรู้ว่าไมโครเซอร์วิสของ Clojure ค้นหาเซอร์วิสอื่นแบบไดนามิกอย่างไร และเกตเวย์ API มอบจุดเข้าสู่ระบบเดียวที่ปลอดภัยให้ระบบของคุณอย่างไร

การค้นพบบริการและเกตเวย์ API เป็นบทเรียน Clojure Functional Programming & JVM Backend Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 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 ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การออกแบบไมโครเซอร์วิสด้วย Clojure
  2. การบรรจุแอปพลิเคชันด้วย Docker
  3. การนำไปใช้งานบนแพลตฟอร์มคลาวด์
  4. การค้นพบบริการและเกตเวย์ API
← กลับไปที่ Clojure Functional Programming & JVM Backend Development