サービスゲートウェイは、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 の起動
ダウンロードした Nacos Server パッケージを展開します。
nacos/binディレクトリに移動し、スタンドアロンモードで Nacos Server を起動します。Linux、UNIX、または macOS:
sh startup.sh -m standaloneWindows:
startup.cmdをダブルクリックします。
Spring Cloud Gateway によるサービスゲートウェイの構築
Spring Cloud Gateway は Project Reactor および Spring WebFlux 上に構築されており、ノンブロッキングでリアクティブなゲートウェイを提供します。
ステップ 1:ゲートウェイプロジェクトの作成
spring-cloud-gateway-nacosという名前の Maven プロジェクトを作成します。以下の依存関係を
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>起動クラス
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このルート構成は次のように動作します。
| コンポーネント | 値 | 説明 |
|---|---|---|
| Predicate | Path=/provider1/** | /provider1/ で始まるすべてのリクエストにマッチします。 |
| URI | lb://service-provider | クライアント側負荷分散を使用して、マッチしたリクエストを service-provider サービスに転送します。 |
| Filter | StripPrefix=1 | 転送前に最初のパスセグメントを削除します。たとえば、/provider1/echo/hello は /echo/hello になります。 |
ステップ 3:ゲートウェイの起動と登録の確認
GatewayApplicationの main 関数を実行して、ゲートウェイを起動します。http://127.0.0.1:8848/nacos(デフォルトのユーザー名およびパスワード:nacos)で Nacos Server コンソールを開きます。ナビゲーションウィンドウで、サービス管理 > サービス を選択します。
spring-cloud-gateway-nacosがサービスリストに表示されることを確認します。
Spring Cloud Netflix Zuul によるサービスゲートウェイの構築
Zuul は、ブロッキング I/O モデルに基づく代替ゲートウェイオプションです。Netflix OSS スタックを既に使用しているアプリケーションに適しています。
ステップ 1:ゲートウェイプロジェクトの作成
spring-cloud-zuul-nacosという名前の Maven プロジェクトを作成します。以下の依存関係を
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>起動クラス
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:ゲートウェイの起動と登録の確認
ZuulApplicationの main 関数を実行して、ゲートウェイを起動します。http://127.0.0.1:8848/nacos(デフォルトのユーザー名およびパスワード:nacos)で Nacos Server コンソールを開きます。ナビゲーションウィンドウで、サービス管理 > サービス を選択します。
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/helloZuul(ポート 18022)の場合:
curl http://127.0.0.1:18022/provider1/echo/hello成功すると、応答として hello が返され、ゲートウェイがリクエストを service-provider に転送し、/provider1 プレフィックスを削除したことが確認できます。

EDAS へのデプロイ
Enterprise Distributed Application Service (EDAS) にアプリケーションをデプロイします。デプロイ手順については、「サービス登録とディスカバリーの実装」をご参照ください。
EDAS サービスレジストリは、Nacos の商用版です。EDAS にデプロイする場合、プラットフォームが自動的に Nacos 接続情報を(IP アドレス、サービスポート、名前空間、AccessKey ID、AccessKey Secret、コンテキストパス)設定します。追加の構成は不要です。オンプレミスの Nacos 設定は、必要に応じて保持または削除してください。
バージョン互換性
| Spring Cloud バージョン | Spring Cloud Alibaba バージョン |
|---|---|
| Greenwich | 2.1.1.RELEASE |
| Finchley | 2.0.1.RELEASE |
| Edgware | 1.5.1.RELEASE |
Spring Cloud Edgware は保守終了となっています。新規アプリケーションには使用しないでください。