使用 Visual Studio 进行 Service Fabric 应用程序升级的教程Service Fabric application upgrade tutorial using Visual Studio


Azure Service Fabric 确保只升级已更改的服务,并在整个升级过程中监视应用程序的运行状况,从而可以简化云应用程序的升级过程。Azure Service Fabric simplifies the process of upgrading cloud applications by ensuring that only changed services are upgraded, and that application health is monitored throughout the upgrade process. 它还能在应用程序发生任何问题时自动回滚到旧版本。It also automatically rolls back the application to the previous version upon encountering issues. Service Fabric 应用程序升级造成的 停机时间为零,因为可以在不停机的情况下升级应用程序。Service Fabric application upgrades are Zero Downtime, since the application can be upgraded with no downtime. 本教程介绍如何从 Visual Studio 完成滚动升级。This tutorial covers how to complete a rolling upgrade from Visual Studio.

步骤 1:构建和发布视觉对象示例Step 1: Build and publish the Visual Objects sample

首先,从 GitHub 下载可视对象应用程序。First, download the Visual Objects application from GitHub. 然后,右键单击应用程序项目 VisualObjects,并从 Service Fabric 菜单项中选择“发布”命令生成并发布应用程序。Then, build and publish the application by right-clicking on the application project, VisualObjects, and selecting the Publish command in the Service Fabric menu item.

Service Fabric 应用程序的上下文菜单

选择“发布”会显示一个弹出窗口,可以将“目标配置文件”设置为 PublishProfiles\Local.xmlSelecting Publish brings up a popup, and you can set the Target profile to PublishProfiles\Local.xml. 在单击“发布”之前,该窗口应如下所示。The window should look like the following before you click Publish.

发布 Service Fabric 应用程序

现在,可在对话框中单击“发布”。Now you can click Publish in the dialog box. 可以使用 Service Fabric Explorer 查看群集和应用程序You can use Service Fabric Explorer to view the cluster and the application. “视觉对象”应用程序有一个 Web 服务,在浏览器的地址栏中输入 http://localhost:8081/visualobjects/ 即可转到该服务。The Visual Objects application has a web service that you can go to by typing http://localhost:8081/visualobjects/ in the address bar of your browser. 应会在屏幕上看到 10 个四处移动的浮动可视对象。You should see 10 floating visual objects moving around on the screen.

注意: 如果部署到 Cloud.xml 配置文件 (Azure Service Fabric),则可以在 http://{ServiceFabricName}.{Region}.cloudapp.chinacloudapi.cn:8081/visualobjects/ 访问应用程序。NOTE: If deploying to Cloud.xml profile (Azure Service Fabric), the application should then be available at http://{ServiceFabricName}.{Region}.cloudapp.chinacloudapi.cn:8081/visualobjects/. 请确保在负载均衡器中配置了 8081/TCP(在 Service Fabric 实例所在的资源组中找到负载均衡器)。Make sure you do have 8081/TCP configured in the Load Balancer (find the Load Balancer in the same resource group as the Service Fabric instance).

步骤 2:更新视觉对象示例Step 2: Update the Visual Objects sample

你可能会注意到,使用步骤 1 中部署的版本,视觉对象不会旋转。You might notice that with the version that was deployed in step 1, the visual objects do not rotate. 让我们将此应用程序升级到视觉对象也会旋转的版本。Let's upgrade this application to one where the visual objects also rotate.

选择 VisualObjects 解决方案中的 VisualObjects.ActorService 项目,并打开 VisualObjectActor.cs 文件。Select the VisualObjects.ActorService project within the VisualObjects solution, and open the VisualObjectActor.cs file. 在该文件中,转到 MoveObject 方法,注释掉 visualObject.Move(false),并取消注释 visualObject.Move(true)Within that file, go to the method MoveObject, comment out visualObject.Move(false), and uncomment visualObject.Move(true). 此代码更改可在升级服务后旋转对象。This code change rotates the objects after the service is upgraded. 现在可以生成(不是重新生成)解决方案,这会生成修改后的项目。Now you can build (not rebuild) the solution, which builds the modified projects. 如果选择“全部重新生成”,则必须更新所有项目的版本。If you select Rebuild all, you have to update the versions for all the projects.

我们还需要设置应用程序的版本。We also need to version our application. 若要在右键单击“VisualObjects”项目之后进行版本更改,可以使用 Visual Studio 的“编辑清单版本”选项。To make the version changes after you right-click on the VisualObjects project, you can use the Visual Studio Edit Manifest Versions option. 选择此选项会显示用于编辑版本的对话框,如下所示:Selecting this option brings up the dialog box for edition versions as follows:

版本控制对话框

将已修改的项目及其代码包连同应用程序的版本一起更新为 2.0.0。Update the versions for the modified projects and their code packages, along with the application to version 2.0.0. 完成更改后,清单应该如下所示(粗体部分即为所做的更改):After the changes are made, the manifest should look like the following (bold portions show the changes):

更新版本

如果选择“自动更新应用程序和服务版本”,Visual Studio 工具可以自动滚动更新版本。The Visual Studio tools can do automatic rollups of versions upon selecting Automatically update application and service versions. 如果使用 SemVer,则在选择该选项后,需要单独更新代码和/或配置包版本。If you use SemVer, you need to update the code and/or configuration package version alone if that option is selected.

保存更改,并选中“升级应用程序”框。Save the changes, and now check the Upgrade the Application box.

步骤 3:升级应用程序Step 3: Upgrade your application

请熟悉应用程序升级参数升级过程,充分了解可应用的各种升级参数、超时和运行状况标准。Familiarize yourself with the application upgrade parameters and the upgrade process to get a good understanding of the various upgrade parameters, time-outs, and health criterion that can be applied. 在本演练中,服务运行状况评估条件设置为默认值(不受监视模式)。For this walkthrough, the service health evaluation criterion is set to the default (unmonitored mode). 如果要配置这些设置,可以选择“配置升级设置”,并视需要修改参数。You can configure these settings by selecting Configure Upgrade Settings and then modifying the parameters as desired.

现在,我们已经准备好选择“发布”来开始升级应用程序。Now we are all set to start the application upgrade by selecting Publish. 此选项会将应用程序升级到对象会旋转的版本 2.0.0。This option upgrades your application to version 2.0.0, in which the objects rotate. Service Fabric 每次会升级一个更新域(先更新一些对象,再更新另一些对象),在升级期间,仍可访问服务。Service Fabric upgrades one update domain at a time (some objects are updated first, followed by others), and the service remains accessible during the upgrade. 可以通过客户端(浏览器)检查是否可以访问服务。Access to the service can be checked through your client (browser).

现在,随着应用程序不断升级,可以使用 Service Fabric Explorer 来监视应用程序(使用应用程序下的“正在进行升级”选项卡)。Now, as the application upgrade proceeds, you can monitor it with Service Fabric Explorer, by using the Upgrades in Progress tab under the applications.

几分钟后,所有更新域应已升级(已完成),Visual Studio 输出窗口应该也会指出升级已完成。In a few minutes, all update domains should be upgraded (completed), and the Visual Studio output window should also state that the upgrade is completed. 此外,可以看到,浏览器窗口中的所有 可视对象都在旋转!And you should find that all the visual objects in your browser window are now rotating!

可以尝试通过更改版本来练习本文所述的操作:从版本 2.0.0 升级到版本 3.0.0,或者从版本 2.0.0 降级到版本 1.0.0。You may want to try changing the versions, and moving from version 2.0.0 to version 3.0.0 as an exercise, or even from version 2.0.0 back to version 1.0.0. 尝试练习使用超时和运行状况策略,以便加深对它的熟悉。Play with time-outs and health policies to make yourself familiar with them. 与部署到本地群集不同,在部署到 Azure 群集时,可能需要使用不同的参数。When deploying to an Azure cluster as opposed to a local cluster, the parameters used may have to differ. 们建议保守设置超时值。We recommend that you set the time-outs conservatively.

后续步骤Next steps

使用 PowerShell 升级应用程序逐步讲解了如何使用 PowerShell 进行应用程序升级。Upgrading your application using PowerShell walks you through an application upgrade using PowerShell.

使用升级参数来控制应用程序的升级方式。Control how your application is upgraded by using upgrade parameters.

了解如何使用数据序列化,使应用程序在升级后保持兼容。Make your application upgrades compatible by learning how to use data serialization.

参考高级主题,了解如何在升级应用程序时使用高级功能。Learn how to use advanced functionality while upgrading your application by referring to Advanced topics.

参考对应用程序升级进行故障排除中的步骤来解决应用程序升级时的常见问题。Fix common problems in application upgrades by referring to the steps in Troubleshooting application upgrades.