すべてのプロダクト
Search
ドキュメントセンター

Enterprise Distributed Application Service:サービスゲートウェイの構築

最終更新日:Mar 11, 2026

サービスゲートウェイは、URL パス、ヘッダー、クエリパラメーターなどの設定可能なルールに基づいて、クライアントからのリクエストをバックエンドのマイクロサービスにルーティングします。このトピックでは、サービスレジストリとして Nacos を使用し、Spring Cloud Gateway または Spring Cloud Netflix Zuul のいずれかでゲートウェイを構築する方法について説明します。

手順に従ったセットアップを省略する場合は、以下の完全なデモプロジェクトをダウンロードしてください。

Spring Cloud Gateway によるリクエストのルーティング

Spring Cloud Gateway は、次の 3 つのコアコンストラクトを通じてリクエストを処理します。

コンストラクト説明
Route基本的な構成要素です。各ルートは、ID、送信先 URI、1 つ以上の述語(Predicate)、およびオプションのフィルターによって定義され、受信リクエストをバックエンドサービスにマッピングします。
Predicate受信リクエスト(パス、ヘッダー、クエリパラメーターなど)に対して評価されるマッチング条件です。すべての述語が true を返すと、そのルートがマッチします。
Filter転送前後でリクエストまたは応答を変更する処理ステップです。たとえば、StripPrefix=1 は、リクエストを下流に送信する前に最初のパスセグメントを削除します。

リクエストフロー: クライアント → Gateway Handler Mapping(述語を評価してルートをマッチ) → Gateway Web Handler(フィルターチェーンを実行) → バックエンドサービス。フィルターはプロキシされたリクエストの前後で実行され、ゲートウェイレベルでヘッダーの変更、パスの書き換え、認証の追加などが可能です。

前提条件

作業を開始する前に、以下の準備が整っていることを確認してください。

  • Maven がインストール済みで、環境変数が設定されていること

  • 最新の Nacos Server がダウンロード済みであること

  • オプション:

Nacos Server の起動

  1. ダウンロードした Nacos Server パッケージを展開します。

  2. nacos/bin ディレクトリに移動し、スタンドアロンモードで Nacos Server を起動します。

    • Linux、UNIX、または macOS:

      sh startup.sh -m standalone
    • Windows: startup.cmd をダブルクリックします。

Spring Cloud Gateway によるサービスゲートウェイの構築

Spring Cloud Gateway は Project Reactor および Spring WebFlux 上に構築されており、ノンブロッキングでリアクティブなゲートウェイを提供します。

ステップ 1:ゲートウェイプロジェクトの作成

  1. spring-cloud-gateway-nacos という名前の Maven プロジェクトを作成します。

  2. 以下の依存関係を pom.xml に追加します。この例では、Spring Boot 2.1.4.RELEASE および Spring Cloud Greenwich.SR1 を使用しています。

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.4.RELEASE</version>
            <relativePath/>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-gateway</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                <version>2.1.1.RELEASE</version>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Greenwich.SR1</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
  3. 起動クラス GatewayApplication を作成します。

        @SpringBootApplication
        @EnableDiscoveryClient
        public class GatewayApplication {
            public static void main(String[] args) {
                SpringApplication.run(GatewayApplication.class, args);
            }
        }

ステップ 2:ルーティングルールの構成

以下の構成を application.yaml に追加します。Nacos Server が別のマシンで動作している場合は、127.0.0.1:8848 を実際のアドレスに置き換えてください。

server:
  port: 18012

spring:
  application:
    name: spring-cloud-gateway-nacos
  cloud:
    gateway:
      routes:
      - id: service-provider
        uri: lb://service-provider
        predicates:
        - Path=/provider1/**
        filters:
        - StripPrefix=1
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

このルート構成は次のように動作します。

コンポーネント説明
PredicatePath=/provider1/**/provider1/ で始まるすべてのリクエストにマッチします。
URIlb://service-providerクライアント側負荷分散を使用して、マッチしたリクエストを service-provider サービスに転送します。
FilterStripPrefix=1転送前に最初のパスセグメントを削除します。たとえば、/provider1/echo/hello/echo/hello になります。

ステップ 3:ゲートウェイの起動と登録の確認

  1. GatewayApplication の main 関数を実行して、ゲートウェイを起動します。

  2. http://127.0.0.1:8848/nacos(デフォルトのユーザー名およびパスワード:nacos)で Nacos Server コンソールを開きます。

  3. ナビゲーションウィンドウで、サービス管理サービス を選択します。spring-cloud-gateway-nacos がサービスリストに表示されることを確認します。

Spring Cloud Netflix Zuul によるサービスゲートウェイの構築

Zuul は、ブロッキング I/O モデルに基づく代替ゲートウェイオプションです。Netflix OSS スタックを既に使用しているアプリケーションに適しています。

ステップ 1:ゲートウェイプロジェクトの作成

  1. spring-cloud-zuul-nacos という名前の Maven プロジェクトを作成します。

  2. 以下の依存関係を pom.xml に追加します。この例では、Spring Boot 2.1.4.RELEASE、Spring Cloud Greenwich.SR1、および Spring Cloud Alibaba 0.9.0 を使用しています。

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.4.RELEASE</version>
            <relativePath/>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-webflux</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                <version>2.1.1.RELEASE</version>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Greenwich.SR1</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
  3. 起動クラス ZuulApplication を作成します。

        @SpringBootApplication
        @EnableZuulProxy
        @EnableDiscoveryClient
        public class ZuulApplication {
            public static void main(String[] args) {
                SpringApplication.run(ZuulApplication.class, args);
            }
        }

ステップ 2:ルーティングルールの構成

以下の構成を application.properties に追加します。Nacos Server が別のマシンで動作している場合は、127.0.0.1:8848 を実際のアドレスに置き換えてください。

spring.application.name=spring-cloud-zuul-nacos
server.port=18022

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

zuul.routes.opensource-provider1.path=/provider1/**
zuul.routes.opensource-provider1.serviceId=service-provider

これにより、/provider1/ で始まるすべてのリクエストが service-provider バックエンドサービスにルーティングされます。

ステップ 3:ゲートウェイの起動と登録の確認

  1. ZuulApplication の main 関数を実行して、ゲートウェイを起動します。

  2. http://127.0.0.1:8848/nacos(デフォルトのユーザー名およびパスワード:nacos)で Nacos Server コンソールを開きます。

  3. ナビゲーションウィンドウで、サービス管理サービス を選択します。spring-cloud-zuul-nacos がサービスリストに表示されることを確認します。

サービスプロバイダーの作成

Spring Cloud Gateway および Zuul の両方とも、リクエストを転送するための下流サービスが必要です。詳細については、「サービス登録とディスカバリーの実装」をご参照ください。

以下の例では、単一の /echo/{string} エンドポイントを持つ最小限のサービスプロバイダーを作成します。

@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }

    @RestController
    public class EchoController {
        @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
        public String echo(@PathVariable String string) {
            return string;
        }
    }
}

リクエスト転送の検証

ローカルでの検証

ゲートウェイとサービスプロバイダーの両方を起動し、ゲートウェイ経由でリクエストを送信します。

Spring Cloud Gateway(ポート 18012)の場合:

curl http://127.0.0.1:18012/provider1/echo/hello

Zuul(ポート 18022)の場合:

curl http://127.0.0.1:18022/provider1/echo/hello

成功すると、応答として hello が返され、ゲートウェイがリクエストを service-provider に転送し、/provider1 プレフィックスを削除したことが確認できます。

EDAS Spring Cloud application development: build a Zuul gateway

EDAS へのデプロイ

Enterprise Distributed Application Service (EDAS) にアプリケーションをデプロイします。デプロイ手順については、「サービス登録とディスカバリーの実装」をご参照ください。

EDAS サービスレジストリは、Nacos の商用版です。EDAS にデプロイする場合、プラットフォームが自動的に Nacos 接続情報を(IP アドレス、サービスポート、名前空間、AccessKey ID、AccessKey Secret、コンテキストパス)設定します。追加の構成は不要です。オンプレミスの Nacos 設定は、必要に応じて保持または削除してください。

バージョン互換性

Spring Cloud バージョンSpring Cloud Alibaba バージョン
Greenwich2.1.1.RELEASE
Finchley2.0.1.RELEASE
Edgware1.5.1.RELEASE
説明

Spring Cloud Edgware は保守終了となっています。新規アプリケーションには使用しないでください。

次のステップ