1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【基本編】Spring Cloud GatewayでAPIゲートウェイを作ってみる

1
Last updated at Posted at 2025-09-05

はじめに

マイクロサービスアーキテクチャが一般的になる中で、APIゲートウェイの重要性が増しています。Springエコシステムを使っているなら、Spring Cloud Gatewayは非常に強力な選択肢です。

この記事では、Spring Cloud Gatewayの基本的な使い方を、初心者向けにわかりやすく解説します。


🌐 Spring Cloud Gatewayとは?

Spring Cloud Gatewayは、Spring公式が提供する非同期・ノンブロッキングなAPIゲートウェイです。以下のような機能を提供します:

  • ルーティング(リクエストの振り分け)
  • フィルター(認証、ロギング、ヘッダー操作など)
  • 負荷分散(Spring Cloud LoadBalancerとの連携)
  • Circuit Breaker(Resilience4jとの統合)

🛠️ 環境構築

必要なもの

  • Java 17以上
  • Spring Boot 3.x
  • Maven または Gradle

pom.xml の依存関係(Mavenの場合)

<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
  </dependency>
</dependencies>

最小構成のルーティング設定

application.yml

spring:
  cloud:
    gateway:
      routes:
        - id: sample_route
          uri: https://httpbin.org/get
          predicates:
            - Path=/get

この設定では、/get にアクセスすると https://httpbin.org/get にリクエストが転送されます。


🧪 動作確認

アプリケーションを起動して、以下のURLにアクセスしてみましょう:

http://localhost:8080/get

httpbin.org からのレスポンスが返ってくれば成功です!


🖼️ Spring Cloud Gatewayの基本構成図

jpeg.jpg

※図はクライアント → Gateway → バックエンドサービスの流れを示しています。


🔍 フィルターの追加例

filters:
  - AddRequestHeader=X-Request-Example, GatewayDemo

このフィルターは、リクエストヘッダーに X-Request-Example: GatewayDemo を追加します。


📌 まとめ

Spring Cloud Gatewayは、シンプルな構成で強力なAPIゲートウェイを構築できるツールです。次回以降は、カスタムフィルターの作成認証との連携など、より実践的な内容を紹介します。


📝 参考リンク

Spring Cloud Gateway公式ドキュメント

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?