在 Azure Spring Cloud 中设置过渡环境Set up a staging environment in Azure Spring Cloud

本文介绍如何使用 Azure Spring Cloud 中的蓝绿部署模式来设置过渡部署。This article explains how to set up a staging deployment by using the blue-green deployment pattern in Azure Spring Cloud. 蓝绿部署是一种 Azure DevOps 持续交付模式,它依赖于在部署新(绿色)版本时保持现有(蓝色)版本的活动性。Blue-green deployment is an Azure DevOps continuous delivery pattern that relies on keeping an existing (blue) version live while a new (green) one is deployed. 本文还介绍了如何在不更改生产部署的情况下,将此过渡部署放入生产环境。This article shows you how to put that staging deployment into production without changing the production deployment.

先决条件Prerequisites

本文使用通过 Spring Initializer 生成的应用程序。This article uses an application built from Spring Initializr. 如果你要对本示例使用其他应用程序,则需要在该应用程序面向公众的部分进行一项简单的更改,以将过渡部署和生产区分开来。If you want to use a different application for this example, you'll need to make a simple change in a public-facing portion of the application to differentiate your staging deployment from production.

若要在 Azure Spring Cloud 中设置蓝绿部署,请按照后续部分中的说明进行操作。To set up blue-green deployment in Azure Spring Cloud, follow the instructions in the next sections.

安装 Azure CLI 扩展Install the Azure CLI extension

使用以下命令安装用于 Azure CLI 的 Azure Spring Cloud 扩展:Install the Azure Spring Cloud extension for the Azure CLI by using the following command:

az extension add --name spring-cloud

准备应用和部署Prepare the app and deployments

若要生成应用程序,请执行以下步骤:To build the application, follow these steps:

  1. 使用带有此配置的 Spring Initializer 为示例应用生成代码。Generate the code for the sample app by using Spring Initializr with this configuration.

  2. 下载代码。Download the code.

  3. 将以下 HelloController.java 源文件添加到文件夹 \src\main\java\com\example\hellospring\ 中:Add the following HelloController.java source file to the folder \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 Cloud!"; 
      } 
    
    } 
    
  4. 生成 .jar 文件:Build the .jar file:

    mvn clean packge -DskipTests
    
  5. 在 Azure Spring Cloud 实例中创建应用:Create the app in your Azure Spring Cloud instance:

    az spring-cloud app create -n demo -g <resourceGroup> -s <Azure Spring Cloud instance> --assign-endpoint
    
  6. 将应用部署到 Azure Spring Cloud:Deploy the app to Azure Spring Cloud:

    az spring-cloud app deploy -n demo -g <resourceGroup> -s <Azure Spring Cloud instance> --jar-path target\hellospring-0.0.1-SNAPSHOT.jar
    
  7. 修改过渡部署的代码:Modify the code for your staging deployment:

    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 Cloud! THIS IS THE GREEN DEPLOYMENT"; 
      } 
    
    } 
    
  8. 重新生成 .jar 文件:Rebuild the .jar file:

    mvn clean packge -DskipTests
    
  9. 创建绿色部署:Create the green deployment:

    az spring-cloud app deployment create -n green --app demo -g <resourceGroup> -s <Azure Spring Cloud instance> --jar-path target\hellospring-0.0.1-SNAPSHOT.jar 
    

查看应用和部署View apps and deployments

使用以下过程查看已部署的应用:View deployed apps by using the following procedure:

  1. 转到 Azure 门户中的 Azure Spring Cloud 实例。Go to your Azure Spring Cloud instance in the Azure portal.

  2. 在左侧窗格中,打开“应用”窗格查看服务实例的应用。From the left pane, open the Apps pane to view apps for your service instance.

    打开“应用”窗格的屏幕截图。

  3. 可以选择某个应用并查看详细信息。You can select an app and view details.

    应用详细信息的屏幕截图。

  4. 打开“部署”以查看应用的所有部署。Open Deployments to see all deployments of the app. 该网格显示了生产和过渡部署。The grid shows both production and staging deployments.

    显示列出的应用部署的屏幕截图。

  5. 选择 URL 以打开当前部署的应用程序。Select the URL to open the currently deployed application.

    显示已部署应用程序的 URL 的屏幕截图。

  6. 在“状态”列中选择“生产”查看默认应用 。Select Production in the State column to see the default app.

    显示默认应用的 URL 的屏幕截图。

  7. 在“状态”列中选择“过渡”查看过渡应用 。Select Staging in the State column to see the staging app.

    显示过渡应用的 URL 的屏幕截图。

提示

  • 确认测试终结点以斜线 (/) 结尾,从而确保正确加载 CSS 文件。Confirm that your test endpoint ends with a slash (/) to ensure that the CSS file is loaded correctly.
  • 如果浏览器要求在查看该页之前输入登录凭据,请使用 URL decode 来解码测试终结点。If your browser requires you to enter login credentials to view the page, use URL decode to decode your test endpoint. URL 解码会返回 https://<username>:<password>@<cluster-name>.test.azureapps.io/gateway/green 格式的 URL。URL decode returns a URL in the format https://<username>:<password>@<cluster-name>.test.azureapps.io/gateway/green. 请使用此格式访问终结点。Use this format to access your endpoint.

备注

配置服务器设置将应用于过渡环境和生产环境。Configuration server settings apply to both your staging environment and your production environment. 例如,如果将配置服务器中的应用网关的上下文路径 (server.servlet.context-path) 设置为 somepath,则绿色部署的路径将更改为 https://<username>:<password>@<cluster-name>.test.azureapps.io/gateway/green/somepath/...For example, if you set the context path (server.servlet.context-path) for your app gateway in the configuration server as somepath, the path to your green deployment changes to https://<username>:<password>@<cluster-name>.test.azureapps.io/gateway/green/somepath/....

如果此时访问面向公众的应用网关,应会看到没有新更改的旧页面。If you visit your public-facing app gateway at this point, you should see the old page without your new change.

将绿色部署设置为生产环境Set the green deployment as the production environment

  1. 在过渡环境中验证更改后,可将其推送到生产环境。After you've verified your change in your staging environment, you can push it to production. 在“应用” > “部署”页上,选择当前位于“生产”环境中的应用程序。 On the Apps > Deployments page, select the application currently in Production.

  2. 选择绿色部署的“注册状态”后面的省略号图标,然后选择“设为生产”。 Select the ellipsis after Registration status of the green deployment, and then select Set as production.

    显示用于将过渡版本设置为生产版本的选项的屏幕截图。

  3. 确认应用的 URL 是否显示了所做的更改。Confirm that the URL of the app displays your changes.

    显示现已在生产环境中的应用的 URL 的屏幕截图。

备注

将绿色部署设置为生产环境后,以前的部署将变成过渡部署。After you've set the green deployment as the production environment, the previous deployment becomes the staging deployment.

修改过渡部署Modify the staging deployment

如果你对更改不满意,可修改应用程序代码,生成新的 .jar 包,然后使用 Azure CLI 将其上传到绿色部署:If you're not satisfied with your change, you can modify your application code, build a new .jar package, and upload it to your green deployment by using the Azure CLI:

az spring-cloud app deploy  -g <resource-group-name> -s <service-instance-name> -n gateway -d green --jar-path gateway.jar

删除过渡部署Delete the staging deployment

若要在 Azure 门户中删除过渡部署,请转到过渡部署的页面,然后选择“删除”按钮。To delete your staging deployment from the Azure portal, go to the page for your staging deployment and select the Delete button.

或者,在 Azure CLI 中运行以下命令删除过渡部署:Alternatively, delete your staging deployment from the Azure CLI by running the following command:

az spring-cloud app deployment delete -n <staging-deployment-name> -g <resource-group-name> -s <service-instance-name> --app gateway

后续步骤Next steps