サービスディスカバリとAPIゲートウェイ
Clojureのマイクロサービスが動的に互いを見つける仕組みと、APIゲートウェイがシステムへの単一で安全な入口を提供する方法を学びます。
「サービスディスカバリとAPIゲートウェイ」はCoddyKit上の無料Clojure Functional Programming & JVM Backend Developmentレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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ゲートウェイ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応の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を演習し、24時間対応の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フィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Clojureマイクロサービスの設計
- Dockerによるコンテナ化
- クラウドプラットフォームへのデプロイ
- サービスディスカバリとAPIゲートウェイ