在 Azure Spring Apps 中设置过渡环境

注意

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

本文介绍如何使用 Azure Spring Apps 中的蓝绿部署模式来设置过渡部署。 蓝绿部署是一种 Azure DevOps 持续交付模式,它依赖于在部署新(绿色)版本时保持现有(蓝色)版本的活动性。 本文还介绍了如何在不更改生产部署的情况下,将此过渡部署放入生产环境。

先决条件

  • 标准计划中的现有 Azure Spring Apps 实例。
  • Azure CLI

本文使用通过 Spring Initializer 生成的应用程序。 如果要对本示例使用其他应用程序,请在应用程序面向公众的部分进行更改,以将过渡部署与生产部署区分开来。

若要在 Azure Spring Apps 中设置蓝绿部署,请按照后续部分中的说明进行操作。

安装 Azure CLI 扩展

使用以下命令安装用于 Azure CLI 的 Azure Spring Apps 扩展

az extension add --name spring

准备应用和部署

若要生成应用程序,请执行以下步骤:

  1. 使用带有此配置的 Spring Initializer 为示例应用生成代码。

  2. 下载代码。

  3. 将以下 HelloController.java 源文件添加到文件夹 *\src\main\java\com\example\hellospring* 中:

    package com.example.hellospring;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @RestController
    
    public class HelloController {
    
    @RequestMapping("/")
    
      public String index() {
          return "Greetings from Azure Spring Apps!";
      }
    
    }
    
  4. 生成 .jar 文件:

    mvn clean package -DskipTests
    
  5. 在 Azure Spring Apps 实例中创建应用:

    az spring app create \
        --resource-group <resource-group-name> \
        --service <Azure-Spring-Apps-instance-name> \
        --name demo \
        --runtime-version Java_17 \
        --assign-endpoint
    
  6. 将应用部署到 Azure Spring Apps:

    az spring app deploy \
        --resource-group <resource-group-name> \
        --service <Azure-Spring-Apps-instance-name> \
        --name demo \
        --artifact-path target\hellospring-0.0.1-SNAPSHOT.jar
    
  7. 修改过渡部署的代码:

    package com.example.hellospring;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @RestController
    
    public class HelloController {
    
    @RequestMapping("/")
    
      public String index() {
          return "Greetings from Azure Spring Apps! THIS IS THE GREEN DEPLOYMENT";
      }
    
    }
    
  8. 重新生成 .jar 文件:

    mvn clean package -DskipTests
    
  9. 创建绿色部署:

    az spring app deployment create \
        --resource-group <resource-group-name> \
        --service <Azure-Spring-Apps-instance-name> \
        --app demo \
        --name green \
        --runtime-version Java_17 \
        --artifact-path target\hellospring-0.0.1-SNAPSHOT.jar
    

查看应用和部署

使用以下步骤查看已部署的应用。

  1. 在 Azure 门户中转到 Azure Spring Apps 实例。

  2. 在导航窗格中,打开“应用”窗格来查看服务实例的应用。

    Screenshot of the Apps pane showing apps for your service instance.

  3. 选择应用以查看详细信息。

    Screenshot of details for an app.

  4. 打开“部署”以查看应用的所有部署。 该网格显示了生产和过渡部署。

    Screenshot that shows listed app deployments.

  5. 选择 URL 以打开当前部署的应用程序。

    Screenshot that shows the URL of the deployed application.

  6. 在“状态”列中选择“生产”查看默认应用 。

    Screenshot that shows the URL of the default app.

  7. 在“状态”列中选择“过渡”查看过渡应用 。

    Screenshot that shows the URL of the staging app.

提示

确认测试终结点以斜线 (/) 结尾,从而确保正确加载 CSS 文件。 如果浏览器要求在查看该页之前输入登录凭据,请使用 URL decode 来解码测试终结点。 URL 解码以 https://\<username>:\<password>@\<cluster-name>.test.microservices.azure.cn/demo/green 格式返回 URL。 请使用此格式访问终结点。

注意

配置服务器设置将应用于过渡环境和生产环境。 例如,如果在配置服务器中将应用演示的上下文路径 (server.servlet.context-path) 设置为 somepath,则绿色部署的路径将更改为 https://\<username>:\<password>@\<cluster-name>.test.microservices.azure.cn/demo/green/somepath/...

如果此时访问面向公众的应用演示,应会看到没有新更改的旧页面。

将绿色部署设置为生产环境

  1. 在过渡环境中验证更改后,可将其推送到生产环境。 在“应用”>“部署”页上,选择当前位于“生产”环境中的应用程序。

  2. 选择绿色部署的“注册状态”后面的省略号图标,然后选择“设为生产”。

    Screenshot that shows selections for setting the staging build to production.

  3. 确认应用的 URL 是否显示了所做的更改。

    Screenshot that shows the URL of the app now in production.

注意

将绿色部署设置为生产环境后,以前的部署将变成过渡部署。

修改过渡部署

如果你对更改不满意,可修改应用程序代码,生成新的 .jar 包,然后使用 Azure CLI 将其上传到绿色部署:

az spring app deploy \
    --resource-group <resource-group-name> \
    --service <service-instance-name> \
    --name demo \
    --deployment green \
    --artifact-path demo.jar

删除过渡部署

若要在 Azure 门户中删除过渡部署,请转到过渡部署的页面,然后选择“删除”按钮。

或者,在 Azure CLI 中运行以下命令删除过渡部署:

az spring app deployment delete \
    --resource-group <resource-group-name> \
    --service <service-instance-name> \
    --name <staging-deployment-name> \
    --app demo

后续步骤