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

将 Spring Cloud Gateway for VMWare Tanzu 迁移到 Azure 容器应用中的自承载网关应用程序

注意

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

标准消耗和专用计划于 2024 年 9 月 30 日进入停用期,并将在 2025 年 3 月底之前完全关闭。 有关详细信息,请参阅将 Azure Spring Apps 标准消耗和专用计划迁移到 Azure 容器应用

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

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

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

注意

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

先决条件

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

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

  1. 请导航至 https://start.spring.io
  2. 通过将 Group 字段设置为您的组织名称来更新项目元数据。 将 Artifact (构件 ) 和 Name (名称 ) 字段更改为 gateway
  3. 添加 Reactive GatewaySpring Boot Actuator 依赖项。
  4. 将其他属性保留为默认值。
  5. 选择 Generate (生成 ) 以下载项目。
  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 配置

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

规模

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

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

自定义域和证书

Azure 容器应用支持自定义域和证书。 有关迁移在 Spring Cloud Gateway 上为 VMWare Tanzu 配置的自定义域的更多信息,请参阅 Azure 容器应用中的证书

路由

您可以将 Spring Cloud Gateway for Tanzu 中的路由迁移到 Spring Cloud Gateway,如 application.yml 示例所示。 Spring Cloud Gateway for VMWare Tanzu 的路由与 Spring Cloud Gateway 的路由之间的映射关系如下:

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

例如,请考虑以下路由配置 JSON 文件 test-api.json,该文件在 Spring Cloud Gateway for VMWare Tanzu 中为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

默认情况下,Spring Cloud Gateway for VMWare Tanzu 在每个路由上设置 StripPrefix=1 。 要迁移到 Spring Cloud Gateway,您需要在 filter 配置中显式设置 StripPrefix=1

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

注意

Spring Cloud Gateway 不支持一些 商业谓词 和商业 过滤器

响应缓存

如果在 Spring Cloud Gateway for VMWare Tanzu 中全局启用响应缓存,请在 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 网关配置准备就绪后,使用 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 网关本身不支持以下商业功能:

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

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