使用 Spring Cloud 閘道

注意

Azure Spring Apps 是 Azure Spring Cloud 服務的新名稱。 雖然服務有新的名稱,但在我們努力更新資產,例如螢幕快照、影片和圖表時,您會在某些地方看到舊名稱一段時間。

本文適用於:❌ 基本/標準✔️企業

本文說明如何使用 VMware Spring Cloud Gateway 搭配 Azure Spring Apps Enterprise 方案,將要求路由傳送至您的應用程式。

VMware Spring Cloud Gateway 是以開放原始碼 Spring Cloud Gateway 專案為基礎的商業 VMware Tanzu 元件。 Spring Cloud Gateway 會處理 API 開發小組的跨領域考慮,例如單一登錄 (SSO)、訪問控制、速率限制、復原能力、安全性等等。 您可以使用新式雲端原生模式加速 API 傳遞,以及您為 API 開發選擇的任何程式設計語言。

Spring Cloud Gateway 包含下列功能:

  • 動態路由組態,與可以套用和變更而不需重新編譯的個別應用程序無關。
  • 商業 API 路由篩選器,用於將授權的 JSON Web 令牌 (JWT) 宣告傳輸至應用程式服務。
  • 用戶端憑證授權。
  • 速率限制方法。
  • 斷路器組態。
  • 支援透過 HTTP 基本身份驗證認證存取應用程式服務。

為了與 VMware Tanzu 的 API 入口網站整合,VMware Spring Cloud Gateway 會在任何路由設定新增或變更之後自動產生 OpenAPI 第 3 版檔。

必要條件

  • 已啟用 Spring Cloud Gateway 的已布建 Azure Spring Apps 企業方案服務實例。 如需詳細資訊,請參閱 快速入門:使用企業方案建置和部署應用程式至 Azure Spring Apps。
  • Azure CLI 2.0.67 版或更新版本。 使用下列命令來安裝 Azure Spring Apps 擴充功能: az extension add --name spring

設定路由

本節說明如何新增、更新和管理使用 Spring Cloud Gateway 之應用程式的 API 路由。

路由組態定義包含下列部分:

  • OpenAPI URI:此 URI 參考 OpenAPI 規格。 您可以使用公用 URI 端點,例如 https://petstore3.swagger.io/api/v3/openapi.json 或 建構的 URI,例如 http://<app-name>/{relative-path-to-OpenAPI-spec},其中 <app-name> 是 Azure Spring Apps 中包含 API 定義的應用程式名稱。 同時支援 OpenAPI 2.0 和 OpenAPI 3.0 規格。 如果已啟用,規格會顯示在 API 入口網站中。
  • routes:路由規則清單,將流量導向至應用程式並套用篩選。
  • 通訊協定:Spring Cloud Gateway 路由傳送流量的應用程式後端通訊協定。 通訊協定支援的值為 HTTPHTTPS,且預設值為 HTTP。 若要保護從 Spring Cloud Gateway 到已啟用 HTTPS 的應用程式流量,您必須在路由設定中將通訊協定設定為 HTTPS
  • 應用程式層級路由:您可以在應用程式層級設定三個路由屬性,以避免在路由設定中重複所有或大部分路由。 具體路由規則會覆寫相同屬性的應用程式層級路由規則。 您可以在應用程式層級定義下列屬性: predicatesfiltersssoEnabled。 如果您使用 OpenAPI URI 功能來定義路由,唯一支援的應用程式層級路由屬性是 filters

使用下列命令來建立路由設定。此值 --app-name 應該是要求路由傳送至 Azure Spring Apps 中裝載的應用程式名稱。

az spring gateway route-config create \
    --name <route-config-name> \
    --resource-group <resource-group-name> \
    --service <Azure-Spring-Apps-instance-name> \
    --app-name <app-name> \
    --routes-file <routes-file.json>

下列範例示範在 create 命令中傳遞至 --routes-file 參數的 JSON 檔案:

{
   "predicates": [
      "<app-level-predicate-of-route>",
   ],
   "ssoEnabled": false,
   "filters": [
      "<app-level-filter-of-route>",
   ],
   "openApi": {
      "uri": "<OpenAPI-URI>"
   },
   "protocol": "<protocol-of-routed-app>",
   "routes": [
      {
         "title": "<title-of-route>",
         "description": "<description-of-route>",
         "predicates": [
            "<predicate-of-route>",
         ],
         "ssoEnabled": true,
         "filters": [
            "<filter-of-route>",
         ],
         "tags": [
            "<tag-of-route>"
         ],
         "order": 0
      }
   ]
}

下表列出路由定義。 所有屬性都是選擇性的。

屬性 描述
title 要套用至所產生 OpenAPI 檔中方法的標題。
description 要套用至所產生 OpenAPI 檔中方法的描述。
uri 完整 URI,其會覆寫要求路由至的應用程式名稱。
ssoEnabled 值,指出是否啟用 SSO 驗證。 請參閱 設定單一登錄
tokenRelay 將目前已驗證使用者的身分識別令牌傳遞至應用程式。
謂詞 述詞的清單。 請參閱 可用的述詞
篩選 篩選清單。 請參閱 可用的篩選
訂單 路由處理順序。 較低順序的處理優先順序較高,如 Spring Cloud Gateway所示。
tags 套用至所產生 OpenAPI 檔中方法的分類標籤。

注意

由於安全性或相容性原因,Azure Spring Apps 不支援所有篩選/述詞。 不支援下列專案:

  • BasicAuth
  • JWTKey

使用 Spring Cloud 閘道的路由

使用下列步驟來建立使用 Spring Cloud Gateway 的範例應用程式。

  1. 使用下列命令在 Azure Spring Apps 中建立名為 test-app 的測試應用程式:

    az spring app create \
        name test-app \
        resource-group <resource-group-name> \
        service <Azure-Spring-Apps-instance-name>
    
  2. 將公用端點指派給閘道以存取它。

    若要檢視提供給 Spring Cloud Gateway 的執行中狀態和資源,請在 Azure 入口網站 中開啟您的 Azure Spring Apps 實例,選取 [Spring Cloud Gateway] 區段,然後選取 [概觀]。

    若要指派公用端點,請選取 [指派端點] 旁的 []。 URL 會在幾分鐘內出現。 儲存 URL 以供稍後使用。

    Screenshot of Azure portal Azure Spring Apps overview page with 'Assign endpoint' highlighted.

    您也可以使用 Azure CLI 來指派端點。 使用下列命令來指派端點。

    az spring gateway update \
        --resource-group <resource-group-name> \
        --service <Azure-Spring-Apps-instance-name> \
        --assign-endpoint true
    
  3. 建立規則,以透過 Spring Cloud Gateway 存取測試應用程式的健全狀況檢查端點。

    將下列內容儲存至 test-api.json 檔案。 此設定包含RateLimit篩選條件,每10秒允許20個要求,以及一個RewritePath篩選條件,可讓要求端點達到標準 Spring Boot 健康情況檢查端點。

    {
      "protocol": "HTTP",
      "routes": [
        {
          "title": "Test API",
          "description": "Retrieve a health check from our application",
          "predicates": [
            "Path=/test/api/healthcheck",
            "Method=GET"
          ],
          "filters": [
            "RateLimit=20,10s",
            "RewritePath=/api/healthcheck,/actuator/health"
          ],
          "tags": [
            "test"
          ]
        }
      ]
    }
    

    然後,使用下列命令將規則套用至應用程式 test-app

    az spring gateway route-config create \
        --name test-api-routes \
        --resource-group <resource-group-name> \
        --service <Azure-Spring-Apps-instance-name> \
        --app-name test-app \
        --routes-file test-api.json
    

    您也可以在入口網站中檢視路由,如下列螢幕快照所示:

    Screenshot of Azure portal Azure Spring Apps Spring Cloud Gateway page showing 'Routing rules' pane.

  4. 使用下列命令透過閘道端點存取 test health check API:

    curl https://<endpoint-url>/test/api/healthcheck
    
  5. 使用下列命令來查詢路由規則:

    az spring gateway route-config show \
        --name test-api-routes \
        --query '{appResourceId:properties.appResourceId, routes:properties.routes}'
    
    az spring gateway route-config list \
        --query '[].{name:name, appResourceId:properties.appResourceId, routes:properties.routes}'
    

使用篩選器

開放原始碼 Spring Cloud Gateway 專案包含許多用於網關路由的內建篩選器。 Spring Cloud Gateway 除了 OSS 專案中所包含的篩選之外,還提供許多自定義篩選。

下列範例示範如何將篩選套用 AddRequestHeadersIfNotPresent 至路由:

[
  {
    "predicates": [
      "Path=/api/**",
      "Method=GET"
    ],
    "filters": [
      "AddRequestHeadersIfNotPresent=Content-Type:application/json,Connection:keep-alive"
    ]
  }
]

然後,使用下列 Azure CLI 命令來套用路由定義:

az spring gateway route-config create \
    --name <route-config-name> \
    --resource-group <resource-group-name> \
    --service <Azure-Spring-Apps-instance-name> \
    --app <app-name>
    --routes-file <json-file-with-routes>

如需可用路由篩選器的詳細資訊,請參閱 如何使用 VMware Spring Cloud Gateway Route Filters 搭配 Azure Spring Apps Enterprise 方案

下一步