您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

快速入门:将 .NET Reliable Services 应用程序部署到 Service FabricQuickstart: Deploy a .NET reliable services application to Service Fabric

Azure Service Fabric 是一款分布式系统平台,可用于部署和管理可缩放的可靠微服务和容器。Azure Service Fabric is a distributed systems platform for deploying and managing scalable and reliable microservices and containers.

此快速入门展示了如何将首个 .NET 应用程序部署到 Service Fabric。This quickstart shows how to deploy your first .NET application to Service Fabric. 完成后,将生成一个投票应用程序,其中包含 ASP.NET Core Web 前端,用于将投票结果保存到群集的有状态后端服务中。When you're finished, you have a voting application with an ASP.NET Core web front end that saves voting results in a stateful back-end service in the cluster.

应用程序屏幕截图

通过此应用程序,将了解如何:Using this application you learn how to:

  • 使用 .NET 和 Service Fabric 创建应用程序Create an application using .NET and Service Fabric
  • 将 ASP.NET Core 用作 Web 前端Use ASP.NET core as a web front-end
  • 将应用程序数据存储到有状态服务中Store application data in a stateful service
  • 在本地调试应用程序Debug your application locally
  • 跨多个节点横向扩展应用程序Scale-out the application across multiple nodes
  • 执行应用程序滚动升级Perform a rolling application upgrade

先决条件Prerequisites

完成本快速入门教程需要:To complete this quickstart:

  1. 安装 Visual Studio 2017,其中包含 Azure 开发以及 ASP.NET 和 Web 开发工作负载。Install Visual Studio 2017 with the Azure development and ASP.NET and web development workloads.

  2. 安装 GitInstall Git

  3. 安装 Microsoft Azure Service Fabric SDKInstall the Microsoft Azure Service Fabric SDK

  4. 运行以下命令,将 Visual Studio 启用为把应用程序部署到本地 Service Fabric 群集:Run the following command to enable Visual Studio to deploy to the local Service Fabric cluster:

    Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force -Scope CurrentUser
    

生成群集Build a cluster

安装运行时、SDK、Visual Studio 工具、Docker 并运行 Docker 之后,创建一个五节点本地开发群集。After you install the runtime, SDKs, Visual Studio tools, Docker, and have Docker running, create a five-node local development cluster.

备注

在创建群集时运行 Docker 是为了创建启用了容器功能的群集。The reason to have Docker running when you create the cluster is so that the cluster is created with container features enabled. 如果未运行 Docker,则需要重新创建群集以启用容器功能。If Docker is not running, you will have to recreate the cluster to enable container features. 尽管这在此特定快速入门中并非必要,但在创建群集时运行 Docker 是一种最佳做法。Although unnecessary for this particular quickstart, the instruction to have Docker running when you create the cluster is included as a best-practice. 若要测试 Docker 是否正在运行,请打开一个终端窗口,运行 docker ps 并查看是否出错。Test that Docker is running by opening a terminal window and running docker ps to see if an error occurs. 如果响应中未指示错误,则表示 Docker 正在运行,可以生成群集。If the response does not indicate an error, Docker is running and you're ready to build a cluster.

  1. 以管理员身份打开权限提升的新 PowerShell 窗口。Open a new, elevated PowerShell window as an administrator.

  2. 运行以下 PowerShell 命令创建开发群集:Run the following PowerShell command to create a development cluster:

    . "C:\Program Files\Microsoft SDKs\Service Fabric\ClusterSetup\DevClusterSetup.ps1"
    
  3. 运行以下命令来启动本地群集管理器工具:Run the following command to start the local cluster manager tool:

    . "C:\Program Files\Microsoft SDKs\Service Fabric\Tools\ServiceFabricLocalClusterManager\ServiceFabricLocalClusterManager.exe"
    

备注

本快速入门中示例应用程序使用的功能在 Windows 7 中不提供。The sample application in this quickstart uses features that are not available on Windows 7.

下载示例Download the sample

在命令窗口中,运行以下命令,将示例应用程序存储库克隆到本地计算机。In a command window, run the following command to clone the sample app repository to your local machine.

git clone https://github.com/Azure-Samples/service-fabric-dotnet-quickstart

在本地运行应用程序Run the application locally

右键单击“开始”菜单中的 Visual Studio 图标,再选择“以管理员身份运行”。Right-click the Visual Studio icon in the Start Menu and choose Run as administrator. 若要将调试程序附加到服务,需要以管理员身份运行 Visual Studio。In order to attach the debugger to your services, you need to run Visual Studio as administrator.

从克隆的存储库中打开 Voting.sln Visual Studio 解决方案。Open the Voting.sln Visual Studio solution from the repository you cloned.

默认情况下,Voting 应用程序在端口 8080 上侦听。By default, the Voting application listens on port 8080. 应用程序端口在 /VotingWeb/PackageRoot/ServiceManifest.xml 文件中进行设置。The application port is set in the /VotingWeb/PackageRoot/ServiceManifest.xml file. 可以通过更新终结点元素的 Port 属性来更改应用程序端口。You can change the application port by updating the Port attribute of the Endpoint element. 若要在本地部署和运行应用程序,应用程序端口必须为打开状态且在你的计算机上可用。To deploy and run the application locally, the application port must be open and available on your computer. 如果更改应用程序端口,在整篇文章中为“8080”替换新的应用程序端口值。If you change the application port, substitute the new application port value for "8080" throughout this article.

若要部署应用程序,请按 F5。To deploy the application, press F5.

备注

在 Visual Studio 输出窗口中,将看到消息“应用程序 URL 未进行设置或不是 HTTP/HTTPS URL,因此浏览器不会对应用程序打开。”In the Visual Studio output window, you will see the message "The application URL is not set or is not an HTTP/HTTPS URL so the browser will not be opened to the application." 此消息不指示错误,但该浏览器将不会自动启动。This message does not indicate an error, but that a browser will not auto-launch.

部署完成后,启动浏览器并打开 http://localhost:8080 来查看应用程序的 Web 前端。When the deployment is complete, launch a browser and open http://localhost:8080 to view the web front end of the application.

应用程序前端

现在可以添加一组投票选项,并开始进行投票。You can now add a set of voting options, and start taking votes. 此应用程序可以运行,并将所有数据存储到 Service Fabric 群集中,而无需单独提供数据库。The application runs and stores all data in your Service Fabric cluster, without the need for a separate database.

大致了解投票示例应用程序Walk through the voting sample application

投票应用程序由以下两个服务组成:The voting application consists of two services:

  • Web 前端服务 (VotingWeb) - ASP.NET Core Web 前端服务,可提供网页服务,并公开用于与后端服务进行通信的 Web API。Web front-end service (VotingWeb)- An ASP.NET Core web front-end service, which serves the web page and exposes web APIs to communicate with the backend service.
  • 后端服务 (VotingData) - ASP.NET Core Web 服务,可公开用于将投票结果存储到磁盘上保留的可靠字典中的 API。Back-end service (VotingData)- An ASP.NET Core web service, which exposes an API to store the vote results in a reliable dictionary persisted on disk.

应用程序关系图

在应用程序中投票时,将会发生以下事件:When you vote in the application the following events occur:

  1. JavaScript 将投票请求作为 HTTP PUT 请求发送给 Web 前端服务中的 Web API。A JavaScript sends the vote request to the web API in the web front-end service as an HTTP PUT request.

  2. Web 前端服务使用代理定位并将 HTTP PUT 请求转发给后端服务。The web front-end service uses a proxy to locate and forward an HTTP PUT request to the back-end service.

  3. 后端服务接收传入请求,并将更新后的结果存储在可靠字典中(结果复制到群集内的多个节点,并保留在磁盘上)。The back-end service takes the incoming request, and stores the updated result in a reliable dictionary, which gets replicated to multiple nodes within the cluster and persisted on disk. 应用程序的所有数据都存储在群集中,因此无需使用数据库。All the application's data is stored in the cluster, so no database is needed.

在 Visual Studio 中进行调试Debug in Visual Studio

此应用程序应该正常运行,不过,你可以使用调试程序来了解应用程序关键部分的运行情况。The application should be running OK, but you can use the debugger to see how key parts of the application work. 在 Visual Studio 中调试应用程序时,使用的是本地 Service Fabric 开发群集。When debugging the application in Visual Studio, you are using a local Service Fabric development cluster. 可以根据需要针对自己的方案调整调试体验。You have the option to adjust your debugging experience to your scenario. 在此应用程序中,数据将使用可靠的字典存储到后端服务中。In this application, data is stored in back-end service using a reliable dictionary. 停止调试程序时,Visual Studio 会默认删除应用程序。Visual Studio removes the application per default when you stop the debugger. 删除应用程序后,后端服务中的数据也会随之一起删除。Removing the application causes the data in the back-end service to also be removed. 若要跨调试会话保留数据,可以将“应用程序调试模式”作为 Visual Studio 中“投票”项目的属性进行更改。To persist the data between debugging sessions, you can change the Application Debug Mode as a property on the Voting project in Visual Studio.

若要查看代码,请完成以下步骤:To look at what happens in the code, complete the following steps:

  1. 打开 /VotingWeb/Controllers/VotesController.cs 文件,并在 Web API 的 Put 方法(第 69 行)中设置一个断点。可以在 Visual Studio 的解决方案资源管理器中搜索此文件。Open the /VotingWeb/Controllers/VotesController.cs file and set a breakpoint in the web API's Put method (line 69) - You can search for the file in the Solution Explorer in Visual Studio.

  2. 打开 /VotingData/Controllers/VoteDataController.cs 文件,并在此 Web API 的 Put 方法(第 54 行)中设置一个断点。Open the /VotingData/Controllers/VoteDataController.cs file and set a breakpoint in this web API's Put method (line 54).

  3. 返回到浏览器,再单击投票选项或添加新的投票选项。Go back to the browser and click a voting option or add a new voting option. 点击 Web 前端 API 控制器中的第一个断点。You hit the first breakpoint in the web front end's api controller.

    • 此时,浏览器中的 JavaScript 将请求发送到前端服务中的 Web API 控制器。This is where the JavaScript in the browser sends a request to the web API controller in the front-end service.

      添加投票前端服务

    • 首先,为后端服务构建 ReverseProxy 的 URL (1)。First, construct the URL to the ReverseProxy for our back-end service (1).

    • 然后,向 ReverseProxy 发送 HTTP PUT 请求 (2)。Then, send the HTTP PUT Request to the ReverseProxy (2).

    • 最后,将后端服务的响应返回到客户端 (3)。Finally, return the response from the back-end service to the client (3).

  4. 按 F5 以继续操作Press F5 to continue

    • 在浏览器提示时,授予 ServiceFabricAllowedUsers 组读取和执行权限(适用于调试模式)。If prompted by the browser, grant ServiceFabricAllowedUsers group read and execute permissions for Debug Mode.

    • 此时,到达后端服务中的断点。You are now at the break point in the back-end service.

      添加投票后端服务

    • 在方法 (1) 的第一行,StateManager 获取或添加一个可靠字典 countsIn the first line in the method (1) the StateManager gets or adds a reliable dictionary called counts.

    • 与可靠字典中的值进行的所有交互都需要使用事务,这个 using 语句(图中标识为2)负责创建此事务。All interactions with values in a reliable dictionary require a transaction, this using statement (2) creates that transaction.

    • 在事务中更新投票选项的相关键值,并提交操作 (3)。In the transaction, update the value of the relevant key for the voting option and commit the operation (3). 提交方法返回后,便会更新字典中的数据,并将数据复制到群集中的其他节点。Once the commit method returns, the data is updated in the dictionary and replicated to other nodes in the cluster. 数据现在安全地存储在群集中,并且后端服务可以故障转移到其他节点,同时数据仍可用。The data is now safely stored in the cluster, and the back-end service can fail over to other nodes, still having the data available.

  5. 按 F5 以继续操作Press F5 to continue

若要停止调试会话,请按 Shift+F5。To stop the debugging session, press Shift+F5.

执行应用程序滚动升级Perform a rolling application upgrade

将新更新部署到应用程序时,Service Fabric 会安全地分阶段发布更新。When deploying new updates to your application, Service Fabric rolls out the update in a safe way. 借助滚动升级,可以杜绝升级时的故障时间,并在出错时自动回滚。Rolling upgrades gives you no downtime while upgrading as well as automated rollback should errors occur.

若要升级应用程序,请执行以下操作:To upgrade the application, do the following:

  1. 在 Visual Studio 中打开 /VotingWeb/Views/Home/Index.cshtml 文件。Open the /VotingWeb/Views/Home/Index.cshtml file in Visual Studio.

  2. 通过添加或更新文本,对页面中的标题进行更改。Change the heading on the page by adding or updating the text. 例如,将标题更改为“Service Fabric 投票示例 v2”。For example, change the heading to "Service Fabric Voting Sample v2".

  3. 保存文件。Save the file.

  4. 在解决方案资源管理器中,右键单击“投票”,再选择“发布”。Right-click Voting in the Solution Explorer and choose Publish. 此时,“发布”对话框显示。The Publish dialog appears.

  5. 单击“清单版本”按钮,更改服务和应用程序的版本。Click the Manifest Version button to change the version of the service and application.

  6. 将 VotingWebPkg 下 Code 元素的版本更改为“2.0.0”(举个例子),再单击“保存”。Change the version of the Code element under VotingWebPkg to "2.0.0", for example, and click Save.

    “更改版本”对话框

  7. 在“发布 Service Fabric 应用程序”对话框中,选中“升级应用程序”复选框。In the Publish Service Fabric Application dialog, check the Upgrade the Application checkbox.

  8. 将“目标配置文件”更改为 PublishProfiles\Local.5Node.xml 并确保将“连接终结点”设置为“本地群集”。Change Target profile to PublishProfiles\Local.5Node.xml and ensure that Connection Endpoint is set to Local Cluster.

  9. 选择“升级应用程序”。Select Upgrade the Application.

    “发布”对话框中的升级设置

  10. 单击“发布” 。Click Publish.

    运行升级期间,仍可以使用应用程序。While the upgrade is running, you can still use the application. 由于在群集中运行的服务有两个实例,因此一些请求可能会获取升级版应用程序,另一些请求可能仍获取旧版应用程序。Because you have two instances of the service running in the cluster, some of your requests may get an upgraded version of the application, while others may still get the old version.

  11. 打开浏览器,并在端口 19080 上转到群集地址。Open your browser and browse to the cluster address on port 19080. 例如,http://localhost:19080/For example http://localhost:19080/.

  12. 单击树视图中的“应用程序”节点,再单击右侧窗格中的“进行中的升级”。Click on the Applications node in the tree view, and then Upgrades in Progress in the right-hand pane. 可以了解如何通过群集中的升级域滚动升级,同时确保在继续执行下一步之前每个域都能够正常运行。You see how the upgrade rolls through the upgrade domains in your cluster, making sure each domain is healthy before proceeding to the next. 在验证域运行状况后,进度栏中的升级域将显示为绿色。An upgrade domain in the progress bar appears green when the health of the domain has been verified. Service Fabric Explorer 中的升级视图Upgrade View in Service Fabric Explorer

    Service Fabric 在升级群集中每个节点上的服务后等待两分钟,从而确保升级安全性。Service Fabric makes upgrades safe by waiting two minutes after upgrading the service on each node in the cluster. 预计整个更新大约需要 8 分钟的时间。Expect the entire update to take approximately eight minutes.

后续步骤Next steps

在此快速入门中,读者学习了如何:In this quickstart, you learned how to:

  • 使用 .NET 和 Service Fabric 创建应用程序Create an application using .NET and Service Fabric
  • 将 ASP.NET Core 用作 Web 前端Use ASP.NET core as a web front-end
  • 将应用程序数据存储到有状态服务中Store application data in a stateful service
  • 在本地调试应用程序Debug your application locally
  • 跨多个节点横向扩展应用程序Scale-out the application across multiple nodes
  • 执行应用程序滚动升级Perform a rolling application upgrade

若要详细了解 Service Fabric 和 .NET,请查看以下教程:To learn more about Service Fabric and .NET, take a look at this tutorial: