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

将适用于 VMWare Tanzu 的 Spring Cloud Gateway 迁移到 Azure 容器应用中的自托管网关应用程序

注意

基本计划、标准计划和企业计划于 2025 年 3 月 17 日进入停用期。 有关详细信息,请参阅 Azure Spring Apps 停用公告

本文适用于: ❎ 基本计划/标准计划 ✅ 企业计划

本文介绍如何将 Azure Spring Apps 企业计划上运行的适用于 VMWare Tanzu 的 Spring Cloud Gateway 项目迁移到作为 Azure 容器应用应用程序运行的自托管开源软件 (OSS) Spring Cloud Gateway 项目。

本页提及的 Spring Cloud Gateway 的 OSS 版本作为示例提供,以供参考。 你可以根据需要选择 Spring Cloud Gateway 的其他发行版。

注意

适用于 VMWare Tanzu 的 Spring Cloud Gateway 提供的功能比 OSS 版本中的功能更广泛。 在迁移到生产环境之前,请务必检查差异并确保兼容性。

先决条件

准备自托管 Spring Cloud Gateway 应用程序的代码

要获取 Spring Cloud Gateway 应用程序的代码,请执行以下步骤:

  1. 导航到 https://start.spring.io
  2. 将“组”字段设置为你的组织名称,以更新项目元数据。 将“工件”和“名称”字段更改为“网关”
  3. 添加“反应式网关”和“Spring Boot 执行器”依赖项
  4. 将其他属性保留为默认值。
  5. 选择“生成”以下载项目
  6. 项目下载后,将其解压缩。

配置 Spring Cloud Gateway 应用程序

现在你已下载 Spring Cloud Gateway 应用程序,需要在接下来的部分对其进行配置。

配置应用程序属性文件

要配置应用程序属性文件,请执行以下步骤:

  1. 导航到项目的 gateway/src/main/resources 目录

  2. 将 application.properties 文件重命名为 application.yml

  3. 编辑 application.yml 文件。 以下是一个典型的 application.yml 文件

    spring:
      application:
        name: gateway
      cloud:
        gateway:
          globalcors:
            corsConfigurations:
              '[/**]':
                allowedOriginPatterns: "*"
                allowedMethods:
                - GET
                - POST
                - PUT
                - DELETE
                allowedHeaders:
                - "*"
                allowCredentials: true
                maxAge: 3600
          routes:
          - id: front
            uri: http://front-app
            predicates:
            - Path=/**
            - Method=GET
            order: 1000
            filters:
            - StripPrefix=0
            tags:
            - front
    

CORS 配置

要迁移适用于 VMWare Tanzu 的 Spring Cloud Gateway 项目的跨域资源共享 (CORS) 配置,请参阅 CORS 配置,了解全局 CORS 配置和路由 CORS 配置。

规模

迁移到 Azure 容器应用中的 Spring Cloud Gateway 应用程序时,缩放行为应与 Azure 容器应用模型保持一致。 适用于 VMWare Tanzu 的 Spring Cloud Gateway 中的实例计数会映射到 Azure 容器应用中的 min-replicamax-replica。 你可以通过定义缩放规则为网关应用程序配置自动缩放。 有关详细信息,请参阅在 Azure 容器应用中设置缩放规则

Azure Spring Apps 中可用的 CPU 和内存组合与 Azure 容器应用中可用的组合不同。 映射资源分配时,请确保 Azure 容器应用中所选的 CPU 和内存配置符合性能需求和支持的选项。

自定义域和证书

Azure 容器应用支持自定义域和证书。 如需详细了解如何迁移在适用于 VMware Tanzu 的 Spring Cloud Gateway 上配置的自定义域,请参阅 Azure 容器应用中的证书

路由

你可以将适用于 VMware Tanzu 的 Spring Cloud Gateway 中的路由迁移到 Spring Cloud Gateway,如 application.yml 示例所示。 以下列表说明了适用于 VMware Tanzu 的 Spring Cloud Gateway 的路由与 Spring Cloud Gateway 的路由之间的映射关系:

  • 路由的 name 属性映射到 id
  • appNameprotocol 属性映射到路由的 URI,该 URI 应是 Azure 容器应用实例的可访问 URI。 请确保 Azure 容器应用应用程序启用入口。
  • 适用于 VMWare Tanzu 的 Spring Cloud Gateway 的谓词和筛选器映射到 Spring Cloud Gateway 的谓词和筛选器。 不支持商业谓词和筛选器。 有关详细信息,请参阅适用于 K8s 的 Spring Cloud Gateway 中的商业路由筛选器

例如,请考虑以下路由配置 JSON 文件 test-api.json,该文件在适用于 VMWare Tanzu 的 Spring Cloud Gateway 中为 test 应用定义了 test-api 路由

{
  "protocol": "HTTP",
  "routes": [
    {
      "title": "Test API",
      "predicates": [
        "Path=/test/api/healthcheck",
        "Method=GET"
      ],
      "filters": [
        "AddRequestHeader=X-Request-red, blue"
      ]
    }
  ]
}

接下来,以下 YAML 文件展示了 Spring Cloud Gateway 应用程序对应的路由配置:

spring:
  cloud:
    gateway:
      routes:
      - id: test-api
        uri: http://test
        predicates:
        - Path=/test/api/healthcheck
        - Method=GET
        filters:
        - AddRequestHeader=X-Request-red, blue
        - StripPrefix=1

默认情况下,适用于 VMWare Tanzu 的 Spring Cloud Gateway 会在每个路由上设置 StripPrefix=1。 要迁移到 Spring Cloud Gateway,需要在筛选器配置中显式设置 StripPrefix=1

要使 Spring Cloud Gateway 应用程序能够通过应用程序名称访问其他应用程序,需要为 Azure 容器应用应用程序启用入口。 还可按照以下格式 https://<app-name>.<container-app-env-name>.<region>.azurecontainerapps.io 将 Azure 容器应用应用程序的可访问完全限定的域名 (FQDN) 设置为路由的 URI。

注意

Spring Cloud Gateway 不支持某些商业谓词商业筛选器

响应缓存

如果在适用于 VMWare Tanzu 的 Spring Cloud Gateway 中全局启用响应缓存,请在 Spring Cloud Gateway 中使用以下配置。 有关详细信息,请参阅本地响应缓存筛选器

spring:
  cloud:
    gateway:
      filter:
        local-response-cache:
          enabled: true
          time-to-live: <response-cache-ttl>
          size: <response-cache-size>

如果为路由启用响应缓存,则可以在托管 Gateway for Spring 的传递规则配置中使用 LocalResponseCache 筛选器,如以下示例所示:

spring:
  cloud:
    gateway:
      routes:
      - id: test-api
        uri: http://test
        predicates:
        - Path=/resource
        filters:
        - LocalResponseCache=30m,500MB

与 APM 集成

你可以为 Spring Cloud Gateway 应用程序启用应用程序性能监视 (APM)。 有关详细信息,请参阅将应用程序性能监视集成到容器映像中

部署到 Azure 容器应用

Spring Cloud Gateway 配置准备就绪后,使用 Azure 容器注册表构建映像并将其部署到 Azure 容器应用。

生成并推送 Docker 映像

要生成并推送 Docker 映像,请执行以下步骤:

  1. 在 Spring Cloud Gateway 项目目录中,创建一个包含以下内容的 Dockerfile

    注意

    或者,使用 ACME Fitness Store 示例 Dockerfile

    FROM mcr.microsoft.com/openjdk/jdk:17-mariner as build
    WORKDIR /staging
    # Install gradle
    RUN tdnf install -y wget unzip
    
    RUN wget https://services.gradle.org/distributions/gradle-8.8-bin.zip && \
        unzip -d /opt/gradle gradle-8.8-bin.zip && \
        chmod +x /opt/gradle/gradle-8.8/bin/gradle
    
    ENV GRADLE_HOME=/opt/gradle/gradle-8.8
    ENV PATH=$PATH:$GRADLE_HOME/bin
    
    COPY . .
    
    # Compile with gradle
    RUN gradle build -x test
    
    FROM mcr.microsoft.com/openjdk/jdk:17-mariner as runtime
    
    WORKDIR /app
    
    COPY --from=build /staging/build/libs/gateway-0.0.1-SNAPSHOT.jar .
    
    ENTRYPOINT ["java", "-jar", "gateway-0.0.1-SNAPSHOT.jar"]
    
  2. 使用以下命令生成网关的映像:

    az acr login --name <azure-container-registry-name>
    az acr build \
        --resource-group <resource-group-name> \
        --image gateway:acrbuild-spring-cloud-gateway-0.0.1-SNAPSHOT \
        --registry <azure-container-registry-name> \
        --file Dockerfile .
    
  3. 确保网关映像已创建,并获取映像标签,该标签使用以下格式:<azure-container-registry-name>.azurecr.io/gateway:acrbuild-spring-cloud-gateway-0.0.1-SNAPSHOT

在 Azure 容器应用中部署映像

网关应用程序映像准备就绪后,使用以下命令将其部署为 Azure 容器应用应用程序。 将 <container-image-of-gateway> 占位符替换为在上一步中检索到的映像标记。

az containerapp up \
    --resource-group <resource-group-name> \
    --name gateway \
    --environment <azure-container-app-environment-name> \
    --image <container-image-of-gateway> \
    --target-port 8080 \
    --ingress external

访问 Spring Cloud Gateway 应用程序的 FQDN 以验证它是否正在运行。

疑难解答

如果在运行 Spring Cloud Gateway 应用程序时遇到问题,可以在 Azure 容器应用中查看 gateway 应用程序的实时和历史日志。 有关详细信息,请参阅 Azure 容器应用中的 Application Logging

还可监视网关应用程序的指标。 有关详细信息,请参阅监视 Azure 容器应用指标

已知限制

OSS Spring Cloud Gateway 本身不支持以下商业功能:

  • 用于生成 OpenAPI 文档的元数据。
  • 单一登录 (SSO) 功能。

如果需要这些功能,则可能需要考虑其他商业解决方案,例如适用于 VMWare Tanzu 的 Spring Cloud Gateway。