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

本文介绍如何使用 Azure Spring Cloud 中的蓝绿色部署模式来设置过渡部署。This article discusses 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 directly.

先决条件Prerequisites

本文假设你已根据 Azure Spring Cloud 应用程序的启动教程部署 PiggyMetrics 应用程序。This article assumes that you've already deployed the PiggyMetrics application from our tutorial about launching an Azure Spring Cloud application. PiggyMetrics 包括三个应用程序:“gateway”、“account-service”和“auth-service”。PiggyMetrics comprises three applications: "gateway," "account-service," and "auth-service."

如果要对本示例使用其他应用程序,需要在该应用程序面向公众的部分进行一项简单的更改。If you want to use a different application for this example, you need to make a simple change in a public-facing portion of the application. 此项更改会将过渡部署与生产部署区分开来。This change differentiates your staging deployment from production.

若要在 Azure Spring Cloud 中设置过渡环境,请按照后续部分中的说明进行操作。To set up a staging environment 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

查看所有部署View all deployments

在 Azure 门户中转到你的服务实例,然后选择“部署管理”以查看所有部署。Go to your service instance in the Azure portal, and select Deployment management to view all deployments. 若要查看更多详细信息,可选择每个部署。To view more details, you can select each deployment.

创建分阶段部署Create a staging deployment

  1. 在本地开发环境中,对 PiggyMetrics 网关应用程序进行少许修改。In your local development environment, make a small modification to the PiggyMetrics gateway application. 例如,更改 gateway/src/main/resources/static/css/launch.css 文件中的颜色。For instance, change the color in the gateway/src/main/resources/static/css/launch.css file. 这样便可轻松区分这两个部署。Doing so lets you easily differentiate the two deployments. 若要生成 jar 包,请运行以下命令:To build the jar package, run the following command:

    mvn clean package
    
  2. 在 Azure CLI 中创建新的部署,并指定过渡部署名称“绿色”。In the Azure CLI, create a new deployment, and give it the staging deployment name "green."

    az spring-cloud app deployment create -g <resource-group-name> -s <service-instance-name> --app gateway -n green --jar-path gateway/target/gateway.jar
    
  3. 成功部署后,从应用程序仪表板访问网关页面,然后在左侧的“应用实例”选项卡中查看所有实例 。After the deployment finishes successfully, access the gateway page from the Application Dashboard, and view all your instances in the App Instances tab on the left.

备注

发现状态为“OUT_OF_SERVICE”,因此在验证完成之前,流量不会路由到此部署。The discovery status is OUT_OF_SERVICE so that traffic won't be routed to this deployment before verification is complete.

验证过渡部署Verify the staging deployment

  1. 返回“部署管理”页面并选择你的新部署。Return to the Deployment management page, and select your new deployment. 部署状态应显示为“正在运行”。The deployment status should show Running. “分配/取消分配域”按钮应显示为灰色,因为该环境是一个过渡环境。The Assign/Unassign domain button should appear grayed, because the environment is a staging environment.

  2. 在“概述”页面中,应会看到“测试终结点” 。In the Overview pane, you should see a Test Endpoint. 将其复制粘贴到新的浏览器窗口,此时应会显示新的 PiggyMetrics 页面。Copy and paste it into a new browser window, and the new PiggyMetrics page should be displayed.

提示

  • 确认测试终结点以斜线 (/) 结尾,从而确保正确加载 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 form "https://<username>:<password>@<cluster-name>.test.azureapps.io/gateway/green". 请使用此格式来访问终结点。Use this form to access your endpoint.

备注

配置服务器设置既应用于过渡环境,也应用于生产环境。Config server settings apply to both your staging environment and production. 例如,如果在配置服务器中将应用网关的上下文路径 (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 config 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. 返回“部署管理”,选中“网关”应用程序复选框。 Return to Deployment management, and select the gateway application check box.

  2. 选择“设置部署”。Select Set deployment.

  3. 在“生产部署”列表中选择“绿色”,然后选择“应用” 。In the Production Deployment list, select Green, and then select Apply.

  4. 转到网关应用程序的“概述”页。Go to your gateway application Overview page. 如果已为网关应用程序分配了一个域,URL 将在“概述”窗格中显示。If you've already assigned a domain for your gateway application, the URL will appear in the Overview pane. 若要查看修改后的 PiggyMetrics 页面,请选择该 URL,然后前往该站点。To view the modified PiggyMetrics page, select the URL, and go to the site.

备注

将绿色部署设置为生产环境后,以前的部署将变成过渡部署。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 port, go to your staging deployment page, and then 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