你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 Spring Cloud 网关

注意

Azure Spring Apps 是 Azure Spring Cloud 服务的新名称。 虽然该服务有新名称,但一些地方仍会使用旧名称,我们仍在更新屏幕截图、视频和图形等资产。

本文适用于:❌ 基本版/标准版 ✔️ 企业版

本文介绍如何将 VMware Spring Cloud Gateway 与 Azure Spring Apps Enterprise 计划配合使用,将请求路由到应用程序。

VMware Spring Cloud 网关是一个基于开源 Spring Cloud 网关项目的商业 VMware Tanzu 组件。 Spring Cloud 网关处理 API 开发团队的横切关注点,例如单一登录 (SSO)、访问控制、速率限制、复原能力、安全性等。 你可以使用新式云本机模式以及你为 API 开发选择的任何编程语言来加速 API 交付。

Spring Cloud 网关包括以下功能:

  • 动态路由配置,独立于无需重新编译即可应用和更改的单个应用程序。
  • 用于将授权的 JSON Web 令牌 (JWT) 声明传输到应用程序服务的商业 API 路由筛选器。
  • 客户端证书授权。
  • 速率限制方法。
  • 断路器配置。
  • 支持通过 HTTP 基本身份验证凭据访问应用程序服务。

为了与 VMware Tanzu 的 API 门户集成,VMware Spring Cloud Gateway 会在添加或更改任何路由配置后自动生成 OpenAPI 版本 3 文档。

先决条件

  • 已启用 Spring Cloud Gateway 的已预配 Azure Spring Apps Enterprise 计划服务实例。 有关详细信息,请参阅 快速入门:使用企业计划生成应用并将其部署到 Azure Spring Apps。
  • Azure CLI 2.0.67 或更高版本。 使用以下命令安装 Azure Spring Apps 扩展:az extension add --name spring

配置路由

本节介绍如何为使用 Spring Cloud 网关的应用添加、更新和管理 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 网关将流量路由到的应用程序的后端协议。 协议支持的值为 HTTPHTTPS,默认值为 HTTP。 若要保护从 Spring Cloud 网关到启用了 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
      }
   ]
}

下表列出了路由定义。 许多属性是可选的。

properties 说明
title 将应用于生成的 OpenAPI 文档中的方法的标题。
description 将应用于生成的 OpenAPI 文档中的方法的说明。
uri 完整 URI,用于替代请求路由到的应用的名称。
ssoEnabled 一个值,用于指示是否启用 SSO 验证。 请参阅配置单一登录
tokenRelay 将当前经过身份验证的用户标识令牌传递给应用程序。
谓词 谓词列表。 请参阅可用谓词
filters 筛选器列表。 请参阅可用筛选器
订单 路由处理顺序。 顺序越低,处理优先级越高,与 Spring Cloud 网关中一样。
标记 应用于生成的 OpenAPI 文档中方法的分类标记。

注意

由于安全性或兼容性原因,并非所有筛选器/谓词都在 Azure Spring 应用程序中得到支持。 不支持以下各项:

  • BasicAuth
  • JWTKey

使用 Spring Cloud 网关的路由

使用 Spring Cloud 网关,按照以下步骤创建示例应用程序。

  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 网关的运行状态和资源,请在 Azure 门户中打开 Azure Spring Apps 实例,选择“Spring Cloud 网关”部分,然后选择“概述”。

    要分配公共终结点,请选择“分配终结点”旁边的“是”。 几分钟后会显示 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 网关访问测试应用的运行状况检查终结点。

    将以下内容保存到 test-api.json 文件中。 此配置包括一个 RateLimit 筛选器和一个 RewritePath 筛选器,前者允许每 10 秒 20 个请求,后者允许请求终结点达到标准的 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 网关项目包括许多内置筛选器,可在网关路由中使用。 Spring Cloud 网关除了 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 计划配合使用。

后续步骤