技術共有

[Spring Cloud Elite Guide] 徹底した探索と実践的な戦闘: 高度なアプリケーションとゲートウェイのベスト プラクティス

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1 はじめに

Spring Cloud Gateway は、Spring 5、Spring Boot 2、Project Reactor などの Spring エコシステム上に構築された API ゲートウェイを提供します。 Spring Cloud Gateway は、シンプルかつ効率的なルーティング方法を提供することを目的としており、セキュリティ、監視/メトリクス、復元力などのゲートウェイの基本機能を提供します。

以下に 2 つの例を示します。

2. リリースノート

Spring Boot バージョン: 2.2.5.RELEASE

Spring Cloud バージョン: Hoxton.SR3

特に指定がない限り、すべての Spring Cloud ルーチンは上記のバージョンを使用します。

3. ゲートウェイの利用

推奨事項: このコースを開始する前に、Eureka 作成の詳細な手順を理解していない場合は、[Spring Cloud の登録センター Eureka の使用方法を例とともに学びます]、まだ見ていない場合でも問題ありません。以下の手順に従って開始してください。

3.1 新しい親プロジェクトを作成する

親プロジェクトがない場合 (登録センターのインスタンスを完了している場合は、親プロジェクトが必要です)、単純な Maven を使用して親プロジェクトを作成してください。

作成後、pom.xml ファイルを開き、次のコードを追加します。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>org.cherry</groupId>
  7. <artifactId>springcloudproject</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <properties>
  10. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  11. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  12. <java.version>14</java.version>
  13. <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
  14. <springboot.version>2.2.5.RELEASE</springboot.version>
  15. </properties>
  16. <dependencyManagement>
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.springframework.cloud</groupId>
  20. <artifactId>spring-cloud-dependencies</artifactId>
  21. <version>${spring-cloud.version}</version>
  22. <type>pom</type>
  23. <scope>import</scope>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-dependencies</artifactId>
  28. <version>${springboot.version}</version>
  29. <type>pom</type>
  30. <scope>import</scope>
  31. </dependency>
  32. </dependencies>
  33. </dependencyManagement>
  34. </project>

srcフォルダーを削除する

3.2 ゲートウェイサブプロジェクトの作成

親プロジェクトの下で、Spring Initializr を使用して新しいゲートウェイ サブプロジェクト、ゲートウェイを作成し、ゲートウェイに依存することを選択します。簡単なマップは次のとおりです。

ビルドしたら、pom.xml ファイルを変更します。変更されたファイルは次のとおりです。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <artifactId>springcloudproject</artifactId>
  7. <groupId>com.cherry</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. <!-- <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-parent</artifactId>
  11. <version>2.2.6.RELEASE</version>
  12. <relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
  13. </parent>
  14. <groupId>com.cherry</groupId>
  15. <artifactId>gateway</artifactId>
  16. <version>0.0.1-SNAPSHOT</version>
  17. <name>gateway</name>
  18. <description>Demo project for Spring Boot</description>
  19. <!--<properties>
  20. <java.version>1.8</java.version>
  21. <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
  22. </properties>-->
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-gateway</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-test</artifactId>
  31. <scope>test</scope>
  32. <exclusions>
  33. <exclusion>
  34. <groupId>org.junit.vintage</groupId>
  35. <artifactId>junit-vintage-engine</artifactId>
  36. </exclusion>
  37. </exclusions>
  38. </dependency>
  39. </dependencies>
  40. <dependencyManagement>
  41. <dependencies>
  42. <dependency>
  43. <groupId>org.springframework.cloud</groupId>
  44. <artifactId>spring-cloud-dependencies</artifactId>
  45. <version>${spring-cloud.version}</version>
  46. <type>pom</type>
  47. <scope>import</scope>
  48. </dependency>
  49. </dependencies>
  50. </dependencyManagement>
  51. <build>
  52. <plugins>
  53. <plugin>
  54. <groupId>org.springframework.boot</groupId>
  55. <artifactId>spring-boot-maven-plugin</artifactId>
  56. </plugin>
  57. </plugins>
  58. </build>
  59. </project>

構成ファイル application.properties をサフィックス yml で変更し (つまり、ファイル名を application.yml に変更します)、ゲートウェイを構成します。

ここではサービスプロバイダーとして csdn ブログを使用します

  1. server:
  2. port: 9001
  3. spring:
  4. application:
  5. name: gateway
  6. cloud:
  7. gateway:
  8. routes:
  9. - id: gateway-service
  10. uri: https://blog.csdn.net
  11. predicates:
  12. - Path=/huanzi833

実行を実行、スプリングブート組み込み Tomcat が開始、ポート 9001

ブラウザ入力アドレスhttp://localhost:9001/huanzi833

3.3 ゲートウェイを閉じる

ゲートウェイを使用できないようにするには、次の設定を application.yml に追加します。

  1. server:
  2. port: 9001
  3. spring:
  4. application:
  5. name: gateway
  6. cloud:
  7. gateway:
  8. routes:
  9. - id: gateway-service
  10. uri: https://blog.csdn.net
  11. predicates:
  12. - Path=/huanzi833
  13. enabled: false

ブラウザからアクセスした場合の結果は以下の通りです。

4. 時間によるルーティング ルールの一致

注: プロパティ ファイルを変更した後は、毎回手順を繰り返すわけではありませんので、自分でアプリケーションを再起動してください。

4.1 ルートマッチング後の時間

ルーティングは [After] で設定した時間後に有効になります。たとえば、2020 年 1 月 1 日以降のリクエストはブログに転送され、この時間より前のリクエストは転送できません。

  1. server:
  2. port: 9001
  3. spring:
  4. application:
  5. name: gateway
  6. cloud:
  7. gateway:
  8. routes:
  9. - id: gateway-service
  10. uri: https://blog.csdn.net
  11. predicates:
  12. - Path=/huanzi833
  13. - After=2020-01-01T00:00:00+08:00[Asia/Shanghai]
  14. enabled: true

4.2 ルーティングマッチングまでの時間

Before で設定した時間より前のルートは有効になります。たとえば、2021 年 1 月 1 日より前のリクエストはブログに転送され、それ以降のリクエストは転送できません。

  1. server:
  2. port: 9001
  3. spring:
  4. application:
  5. name: gateway
  6. cloud:
  7. gateway:
  8. routes:
  9. - id: gateway-service
  10. uri: https://blog.csdn.net
  11. predicates:
  12. - Path=/huanzi833
  13. - After=2020-01-01T00:00:00+08:00[Asia/Shanghai]
  14. - Before=Before=2021-01-01T00:00:00+08:00[Asia/Shanghai]
  15. enabled: true

4.3 ルートマッチング間の時間

Between で設定した時間間のルーティングが有効になります。たとえば、2020 年 1 月 1 日から 2020 年 10 月 1 日までのリクエストはブログに転送されます。通常、Between と After および Before を併用することはできません。設定の繰り返しを避けるため。

  1. server:
  2. port: 9001
  3. spring:
  4. application:
  5. name: gateway
  6. cloud:
  7. gateway:
  8. routes:
  9. - id: gateway-service
  10. uri: https://blog.csdn.net
  11. predicates:
  12. - Path=/huanzi833
  13. # - After=2019-01-01T00:00:00+08:00[Asia/Shanghai]
  14. # - Before=2021-01-01T00:00:00+08:00[Asia/Shanghai]
  15. - Between=2020-01-01T00:00:00+08:00[Asia/Shanghai], 2020-10-01T00:00:00+08:00[Asia/Shanghai]
  16. enabled: true

4.4 Cookie ルーティングのマッチング

Cookie の場合、述部は 2 つのパラメータを受け取ることができます。1 つは Cookie 名、もう 1 つは正規表現です。ルーティング ルールは、対応する Cookie 名の値と一致し、一致しない場合はルーティングが実行されます。ルーティングは実行されます。実行されません。

  1. server:
  2. port: 9001
  3. spring:
  4. application:
  5. name: gateway
  6. cloud:
  7. gateway:
  8. routes:
  9. - id: gateway-service
  10. uri: https://blog.csdn.net
  11. predicates:
  12. - Path=/huanzi833
  13. # - After=2019-01-01T00:00:00+08:00[Asia/Shanghai]
  14. # - Before=2021-01-01T00:00:00+08:00[Asia/Shanghai]
  15. - Between=2020-01-01T00:00:00+08:00[Asia/Shanghai], 2020-10-01T00:00:00+08:00[Asia/Shanghai]
  16. - Cookie=uid, cherry #通过cookie进行路由规则的匹配
  17. enabled: true

Cookie ルーティング マッチングを使用すると、cmd を入力してテストし、cmd に次のステートメントを入力できます。

4.5 ヘッダールートのマッチング

Cookie ルート マッチングと同様に、パラメータ名と正規表現という 2 つのパラメータがあり、一致する場合はルートが実行され、一致しない場合はルートは実行されません。

  1. server:
  2. port: 9001
  3. spring:
  4. application:
  5. name: gateway
  6. cloud:
  7. gateway:
  8. routes:
  9. - id: gateway-service
  10. uri: https://blog.csdn.net
  11. predicates:
  12. - Path=/huanzi833
  13. # - After=2019-01-01T00:00:00+08:00[Asia/Shanghai]
  14. # - Before=2021-01-01T00:00:00+08:00[Asia/Shanghai]
  15. - Between=2020-01-01T00:00:00+08:00[Asia/Shanghai], 2020-10-01T00:00:00+08:00[Asia/Shanghai]
  16. - Cookie=uid, cherry #通过cookie进行路由规则的匹配
  17. - Header=X-Request-Id, d+ #Header路由规则
  18. enabled: true

テストするには cmd と入力し、cmd に次のステートメントを入力します。

4.6 ホストルートマッチング

次の構成は、www.csdn.net、www.baidu.com、blog.csdn.net などのホスト アドレスとその他のアドレスによって照合されます。

  1. server:
  2. port: 9001
  3. spring:
  4. application:
  5. name: gateway
  6. cloud:
  7. gateway:
  8. routes:
  9. - id: gateway-service
  10. uri: https://blog.csdn.net
  11. predicates:
  12. - Path=/huanzi833
  13. # - After=2019-01-01T00:00:00+08:00[Asia/Shanghai]
  14. # - Before=2021-01-01T00:00:00+08:00[Asia/Shanghai]
  15. - Between=2020-01-01T00:00:00+08:00[Asia/Shanghai], 2020-10-01T00:00:00+08:00[Asia/Shanghai]
  16. - Cookie=uid, cherry #通过cookie进行路由规则的匹配
  17. - Header=X-Request-Id, d+ #Header路由规则
  18. - Host=**.csdn.net, **.baidu.com #Host路由规则
  19. enabled: true

テストするには cmd と入力し、cmd に次のステートメントを入力します。

4.7 メソッドマッチングルート

  1. server:
  2. port: 9001
  3. spring:
  4. application:
  5. name: gateway
  6. cloud:
  7. gateway:
  8. routes:
  9. - id: gateway-service
  10. uri: https://blog.csdn.net
  11. predicates:
  12. - Path=/huanzi833
  13. # - After=2019-01-01T00:00:00+08:00[Asia/Shanghai]
  14. # - Before=2021-01-01T00:00:00+08:00[Asia/Shanghai]
  15. - Between=2020-01-01T00:00:00+08:00[Asia/Shanghai], 2020-10-01T00:00:00+08:00[Asia/Shanghai]
  16. - Cookie=uid, cherry #通过cookie进行路由规则的匹配
  17. - Header=X-Request-Id, d+ #Header路由规则
  18. - Host=**.csdn.net, **.baidu.com #Host路由规则
  19. - Method=GET, POST #Method路由规则
  20. enabled: true

cmd と入力して Get テストを実行します。 cmd に次のステートメントを入力します。

POST テストを実行するには、「cmd」と入力します。cmd に次のステートメントを入力します。(注: サービス プロバイダー コントローラーのパスで GetMapping が使用され、呼び出し時に -X POST が使用される場合、404 が表示されます)。

4.8 IPアドレスのマッチング

述語は、192.168.1.1/24 などの特定の IP 間隔番号を使用してリクエストを設定することによるルーティングもサポートします (192.168.1.1 は IP アドレス、24 はサブネット マスクで、ここでの 24 はサブネット マスクが 255.255.255.0 であることを意味します) )。このアドレスをテスト用のローカル IP アドレスに設定できます。

  1. server:
  2. port: 9001
  3. spring:
  4. application:
  5. name: gateway
  6. cloud:
  7. gateway:
  8. routes:
  9. - id: gateway-service
  10. uri: https://blog.csdn.net
  11. predicates:
  12. - Path=/huanzi833
  13. # - After=2019-01-01T00:00:00+08:00[Asia/Shanghai]
  14. # - Before=2021-01-01T00:00:00+08:00[Asia/Shanghai]
  15. - Between=2020-01-01T00:00:00+08:00[Asia/Shanghai], 2020-10-01T00:00:00+08:00[Asia/Shanghai]
  16. - Cookie=uid, cherry #通过cookie进行路由规则的匹配
  17. - Header=X-Request-Id, d+ #Header路由规则
  18. - Host=**.csdn.net, **.baidu.com #Host路由规则
  19. - Method=GET, POST #Method路由规则
  20. - RemoteAddr=192.168.1.1/24
  21. enabled: true

パスルートマッチング/パラメータマッチング/重みマッチングは、次の登録センターとの統合で完了します。

5. ゲートウェイをサービスにし、登録センター、サービスプロバイダー、および消費者と関連付ける

1. まずサービス センター Eureka を作成します。コードは次のとおりです。