Service discovery e API gateway
Impari come i microservizi Clojure si individuano dinamicamente e come un API gateway fornisca un unico punto di accesso sicuro al sistema.
Service discovery e API gateway è una lezione Clojure Functional Programming & JVM Backend Development gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Clojure Functional Programming & JVM Backend Development, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Clojure Functional Programming & JVM Backend Development include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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
Domande Frequenti
La lezione «Service discovery e API gateway» è gratuita?
Sì — il testo completo di «Service discovery e API gateway» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Clojure Functional Programming & JVM Backend Development, passa a CoddyKit PRO. Il corso Clojure Functional Programming & JVM Backend Development include 4 lezioni in totale.
Cosa imparerò in «Service discovery e API gateway»?
Impari come i microservizi Clojure si individuano dinamicamente e come un API gateway fornisca un unico punto di accesso sicuro al sistema. Eserciti Clojure Functional Programming & JVM Backend Development con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Clojure Functional Programming & JVM Backend Development?
Non è richiesta alcuna esperienza precedente. Clojure Functional Programming & JVM Backend Development su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Service discovery e API gateway»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Clojure Functional Programming & JVM Backend Development?
Sì. Ogni lezione Clojure Functional Programming & JVM Backend Development include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Progettazione di microservizi Clojure
- Containerizzazione con Docker
- Distribuzione su piattaforme cloud
- Service discovery e API gateway