Descubrimiento de servicios y API gateways
Aprenda cómo los microservicios de Clojure se encuentran dinámicamente y cómo un API gateway proporciona un único punto de entrada seguro a su sistema.
Descubrimiento de servicios y API gateways es una lección gratuita de Clojure Functional Programming & JVM Backend Development en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Clojure Functional Programming & JVM Backend Development, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Clojure Functional Programming & JVM Backend Development incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
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
Preguntas frecuentes
¿La lección «Descubrimiento de servicios y API gateways» es gratis?
Sí — el texto completo de «Descubrimiento de servicios y API gateways» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Clojure Functional Programming & JVM Backend Development, actualiza a CoddyKit PRO. El curso de Clojure Functional Programming & JVM Backend Development incluye 4 lecciones en total.
¿Qué aprenderé en «Descubrimiento de servicios y API gateways»?
Aprenda cómo los microservicios de Clojure se encuentran dinámicamente y cómo un API gateway proporciona un único punto de entrada seguro a su sistema. Practicas Clojure Functional Programming & JVM Backend Development con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Clojure Functional Programming & JVM Backend Development?
No se requiere experiencia previa. Clojure Functional Programming & JVM Backend Development en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.
¿Cuánto tiempo toma la lección «Descubrimiento de servicios y API gateways»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Clojure Functional Programming & JVM Backend Development?
Sí. Cada lección de Clojure Functional Programming & JVM Backend Development incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Diseño de microservicios con Clojure
- Contenerización con Docker
- Despliegue en plataformas cloud
- Descubrimiento de servicios y API gateways