0Pricing
Clojure Functional Programming & JVM Backend Development · Lekcja

Wykrywanie usług i bramy API

Dowiedz się, jak mikrousługi Clojure dynamicznie odnajdują się nawzajem oraz jak brama API zapewnia pojedynczy, bezpieczny punkt wejścia do systemu.

Wykrywanie usług i bramy API to bezpłatna lekcja Clojure Functional Programming & JVM Backend Development na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Clojure Functional Programming & JVM Backend Development, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Clojure Functional Programming & JVM Backend Development zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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

Często zadawane pytania

Czy lekcja „Wykrywanie usług i bramy API” jest bezpłatna?

Tak — pełny tekst „Wykrywanie usług i bramy API” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Clojure Functional Programming & JVM Backend Development, przejdź na CoddyKit PRO. Kurs Clojure Functional Programming & JVM Backend Development zawiera 4 lekcji w sumie.

Co nauczysz się w „Wykrywanie usług i bramy API”?

Dowiedz się, jak mikrousługi Clojure dynamicznie odnajdują się nawzajem oraz jak brama API zapewnia pojedynczy, bezpieczny punkt wejścia do systemu. Ćwiczysz Clojure Functional Programming & JVM Backend Development z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Clojure Functional Programming & JVM Backend Development?

Nie wymagamy żadnego doświadczenia. Clojure Functional Programming & JVM Backend Development w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Wykrywanie usług i bramy API”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Clojure Functional Programming & JVM Backend Development?

Tak. Każda lekcja Clojure Functional Programming & JVM Backend Development zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Projektowanie mikrousług w Clojure
  2. Konteneryzacja za pomocą Dockera
  3. Wdrażanie na platformach chmurowych
  4. Wykrywanie usług i bramy API
← Powrót do Clojure Functional Programming & JVM Backend Development