私の連絡先情報
郵便メール:
2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Spring Cloud Gateway は、Spring 5、Spring Boot 2、Project Reactor などの Spring エコシステム上に構築された API ゲートウェイを提供します。 Spring Cloud Gateway は、シンプルかつ効率的なルーティング方法を提供することを目的としており、セキュリティ、監視/メトリクス、復元力などのゲートウェイの基本機能を提供します。
以下に 2 つの例を示します。
Spring Boot バージョン: 2.2.5.RELEASE
Spring Cloud バージョン: Hoxton.SR3
特に指定がない限り、すべての Spring Cloud ルーチンは上記のバージョンを使用します。
推奨事項: このコースを開始する前に、Eureka 作成の詳細な手順を理解していない場合は、[Spring Cloud の登録センター Eureka の使用方法を例とともに学びます]、まだ見ていない場合でも問題ありません。以下の手順に従って開始してください。
親プロジェクトがない場合 (登録センターのインスタンスを完了している場合は、親プロジェクトが必要です)、単純な Maven を使用して親プロジェクトを作成してください。
作成後、pom.xml ファイルを開き、次のコードを追加します。
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>org.cherry</groupId>
- <artifactId>springcloudproject</artifactId>
- <version>1.0-SNAPSHOT</version>
-
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>14</java.version>
- <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
- <springboot.version>2.2.5.RELEASE</springboot.version>
- </properties>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring-cloud.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-dependencies</artifactId>
- <version>${springboot.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- </project>
srcフォルダーを削除する
親プロジェクトの下で、Spring Initializr を使用して新しいゲートウェイ サブプロジェクト、ゲートウェイを作成し、ゲートウェイに依存することを選択します。簡単なマップは次のとおりです。
ビルドしたら、pom.xml ファイルを変更します。変更されたファイルは次のとおりです。
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <artifactId>springcloudproject</artifactId>
- <groupId>com.cherry</groupId>
- <version>1.0-SNAPSHOT</version>
- <!-- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.2.6.RELEASE</version>
- <relativePath/> <!– lookup parent from repository –>-->
- </parent>
- <groupId>com.cherry</groupId>
- <artifactId>gateway</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>gateway</name>
- <description>Demo project for Spring Boot</description>
-
- <!--<properties>
- <java.version>1.8</java.version>
- <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
- </properties>-->
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-gateway</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- <exclusions>
- <exclusion>
- <groupId>org.junit.vintage</groupId>
- <artifactId>junit-vintage-engine</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- </dependencies>
-
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring-cloud.version}</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>
-
- </project>
構成ファイル application.properties をサフィックス yml で変更し (つまり、ファイル名を application.yml に変更します)、ゲートウェイを構成します。
ここではサービスプロバイダーとして csdn ブログを使用します
- server:
- port: 9001
-
- spring:
- application:
- name: gateway
- cloud:
- gateway:
- routes:
- - id: gateway-service
- uri: https://blog.csdn.net
- predicates:
- - Path=/huanzi833
実行を実行、スプリングブート組み込み Tomcat が開始、ポート 9001
ブラウザ入力アドレスhttp://localhost:9001/huanzi833
ゲートウェイを使用できないようにするには、次の設定を application.yml に追加します。
- server:
- port: 9001
-
- spring:
- application:
- name: gateway
- cloud:
- gateway:
- routes:
- - id: gateway-service
- uri: https://blog.csdn.net
- predicates:
- - Path=/huanzi833
- enabled: false
-
ブラウザからアクセスした場合の結果は以下の通りです。
注: プロパティ ファイルを変更した後は、毎回手順を繰り返すわけではありませんので、自分でアプリケーションを再起動してください。
ルーティングは [After] で設定した時間後に有効になります。たとえば、2020 年 1 月 1 日以降のリクエストはブログに転送され、この時間より前のリクエストは転送できません。
- server:
- port: 9001
-
- spring:
- application:
- name: gateway
- cloud:
- gateway:
- routes:
- - id: gateway-service
- uri: https://blog.csdn.net
- predicates:
- - Path=/huanzi833
- - After=2020-01-01T00:00:00+08:00[Asia/Shanghai]
- enabled: true
-
Before で設定した時間より前のルートは有効になります。たとえば、2021 年 1 月 1 日より前のリクエストはブログに転送され、それ以降のリクエストは転送できません。
- server:
- port: 9001
-
- spring:
- application:
- name: gateway
- cloud:
- gateway:
- routes:
- - id: gateway-service
- uri: https://blog.csdn.net
- predicates:
- - Path=/huanzi833
- - After=2020-01-01T00:00:00+08:00[Asia/Shanghai]
- - Before=Before=2021-01-01T00:00:00+08:00[Asia/Shanghai]
- enabled: true
-
Between で設定した時間間のルーティングが有効になります。たとえば、2020 年 1 月 1 日から 2020 年 10 月 1 日までのリクエストはブログに転送されます。通常、Between と After および Before を併用することはできません。設定の繰り返しを避けるため。
- server:
- port: 9001
-
- spring:
- application:
- name: gateway
- cloud:
- gateway:
- routes:
- - id: gateway-service
- uri: https://blog.csdn.net
- predicates:
- - Path=/huanzi833
- # - After=2019-01-01T00:00:00+08:00[Asia/Shanghai]
- # - Before=2021-01-01T00:00:00+08:00[Asia/Shanghai]
- - Between=2020-01-01T00:00:00+08:00[Asia/Shanghai], 2020-10-01T00:00:00+08:00[Asia/Shanghai]
- enabled: true
-
Cookie の場合、述部は 2 つのパラメータを受け取ることができます。1 つは Cookie 名、もう 1 つは正規表現です。ルーティング ルールは、対応する Cookie 名の値と一致し、一致しない場合はルーティングが実行されます。ルーティングは実行されます。実行されません。
- server:
- port: 9001
-
- spring:
- application:
- name: gateway
- cloud:
- gateway:
- routes:
- - id: gateway-service
- uri: https://blog.csdn.net
- predicates:
- - Path=/huanzi833
- # - After=2019-01-01T00:00:00+08:00[Asia/Shanghai]
- # - Before=2021-01-01T00:00:00+08:00[Asia/Shanghai]
- - Between=2020-01-01T00:00:00+08:00[Asia/Shanghai], 2020-10-01T00:00:00+08:00[Asia/Shanghai]
- - Cookie=uid, cherry #通过cookie进行路由规则的匹配
- enabled: true
-
Cookie ルーティング マッチングを使用すると、cmd を入力してテストし、cmd に次のステートメントを入力できます。
Cookie ルート マッチングと同様に、パラメータ名と正規表現という 2 つのパラメータがあり、一致する場合はルートが実行され、一致しない場合はルートは実行されません。
- server:
- port: 9001
-
- spring:
- application:
- name: gateway
- cloud:
- gateway:
- routes:
- - id: gateway-service
- uri: https://blog.csdn.net
- predicates:
- - Path=/huanzi833
- # - After=2019-01-01T00:00:00+08:00[Asia/Shanghai]
- # - Before=2021-01-01T00:00:00+08:00[Asia/Shanghai]
- - Between=2020-01-01T00:00:00+08:00[Asia/Shanghai], 2020-10-01T00:00:00+08:00[Asia/Shanghai]
- - Cookie=uid, cherry #通过cookie进行路由规则的匹配
- - Header=X-Request-Id, d+ #Header路由规则
- enabled: true
-
テストするには cmd と入力し、cmd に次のステートメントを入力します。
次の構成は、www.csdn.net、www.baidu.com、blog.csdn.net などのホスト アドレスとその他のアドレスによって照合されます。
- server:
- port: 9001
-
- spring:
- application:
- name: gateway
- cloud:
- gateway:
- routes:
- - id: gateway-service
- uri: https://blog.csdn.net
- predicates:
- - Path=/huanzi833
- # - After=2019-01-01T00:00:00+08:00[Asia/Shanghai]
- # - Before=2021-01-01T00:00:00+08:00[Asia/Shanghai]
- - Between=2020-01-01T00:00:00+08:00[Asia/Shanghai], 2020-10-01T00:00:00+08:00[Asia/Shanghai]
- - Cookie=uid, cherry #通过cookie进行路由规则的匹配
- - Header=X-Request-Id, d+ #Header路由规则
- - Host=**.csdn.net, **.baidu.com #Host路由规则
- enabled: true
-
テストするには cmd と入力し、cmd に次のステートメントを入力します。
- server:
- port: 9001
-
- spring:
- application:
- name: gateway
- cloud:
- gateway:
- routes:
- - id: gateway-service
- uri: https://blog.csdn.net
- predicates:
- - Path=/huanzi833
- # - After=2019-01-01T00:00:00+08:00[Asia/Shanghai]
- # - Before=2021-01-01T00:00:00+08:00[Asia/Shanghai]
- - Between=2020-01-01T00:00:00+08:00[Asia/Shanghai], 2020-10-01T00:00:00+08:00[Asia/Shanghai]
- - Cookie=uid, cherry #通过cookie进行路由规则的匹配
- - Header=X-Request-Id, d+ #Header路由规则
- - Host=**.csdn.net, **.baidu.com #Host路由规则
- - Method=GET, POST #Method路由规则
- enabled: true
-
cmd と入力して Get テストを実行します。 cmd に次のステートメントを入力します。
POST テストを実行するには、「cmd」と入力します。cmd に次のステートメントを入力します。(注: サービス プロバイダー コントローラーのパスで GetMapping が使用され、呼び出し時に -X POST が使用される場合、404 が表示されます)。
述語は、192.168.1.1/24 などの特定の IP 間隔番号を使用してリクエストを設定することによるルーティングもサポートします (192.168.1.1 は IP アドレス、24 はサブネット マスクで、ここでの 24 はサブネット マスクが 255.255.255.0 であることを意味します) )。このアドレスをテスト用のローカル IP アドレスに設定できます。
- server:
- port: 9001
-
- spring:
- application:
- name: gateway
- cloud:
- gateway:
- routes:
- - id: gateway-service
- uri: https://blog.csdn.net
- predicates:
- - Path=/huanzi833
- # - After=2019-01-01T00:00:00+08:00[Asia/Shanghai]
- # - Before=2021-01-01T00:00:00+08:00[Asia/Shanghai]
- - Between=2020-01-01T00:00:00+08:00[Asia/Shanghai], 2020-10-01T00:00:00+08:00[Asia/Shanghai]
- - Cookie=uid, cherry #通过cookie进行路由规则的匹配
- - Header=X-Request-Id, d+ #Header路由规则
- - Host=**.csdn.net, **.baidu.com #Host路由规则
- - Method=GET, POST #Method路由规则
- - RemoteAddr=192.168.1.1/24
- enabled: true
-
パスルートマッチング/パラメータマッチング/重みマッチングは、次の登録センターとの統合で完了します。
1. まずサービス センター Eureka を作成します。コードは次のとおりです。