0Pricing
Clojure Functional Programming & JVM Backend Development · 课时

服务发现与 API 网关

学习 Clojure 微服务如何动态发现彼此,以及 API 网关如何为系统提供单一且安全的入口。

服务发现与 API 网关 是 CoddyKit 上的免费 Clojure Functional Programming & JVM Backend Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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 导师)并解锁 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 导师会在你学习这节课的过程中回答你的问题。

学习 Clojure Functional Programming & JVM Backend Development 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Clojure Functional Programming & JVM Backend Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 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