在 Azure Service Fabric 上托管 Node.js 应用程序Host a Node.js application on Azure Service Fabric

本快速入门教程帮助你将现有的应用程序(本示例中为 Node.js)部署到在 Azure 上运行的 Service Fabric 群集。This quickstart helps you deploy an existing application (Node.js in this example) to a Service Fabric cluster running on Azure.

必备条件Prerequisites

开始之前,请确保已 设置开发环境Before you get started, make sure that you have set up your development environment. 其中包括安装 Service Fabric SDK 和 Visual Studio 2019 或 2015。Which includes installing the Service Fabric SDK and Visual Studio 2019 or 2015.

此外还需一个用于部署的现有 Node.js 应用程序。You also need to have an existing Node.js application for deployment. 本快速入门使用简单的 Node.js 网站,可从此处下载。This quickstart uses a simple Node.js website that can be downloaded here. 在下一步骤中,请在创建项目后将此文件提取到 <path-to-project>\ApplicationPackageRoot\<package-name>\Code\ 文件夹。Extract this file to your <path-to-project>\ApplicationPackageRoot\<package-name>\Code\ folder after you create the project in the next step.

如果没有 Azure 订阅,请创建一个试用帐户If you don't have an Azure subscription, create a trial account.

创建服务Create the service

以管理员身份启动 Visual Studio。 Launch Visual Studio as an administrator.

使用 CTRL+SHIFT+N 创建一个项目Create a project with CTRL+SHIFT+N

在“新建项目”对话框中,选择“云”>“Service Fabric 应用程序”。 In the New Project dialog, choose Cloud > Service Fabric Application.

将应用程序命名为“MyGuestApp”,然后按“确定”。 Name the application MyGuestApp and press OK.

重要

Node.js 可能会轻松突破针对 Windows 路径的 260 字符限制。Node.js can easily break the 260 character limit for paths that windows has. 请对项目本身使用短路径,例如 c:\code\svc1。Use a short path for the project itself such as c:\code\svc1. (可选)可以按照 这些说明 在 Windows 10 中启用长文件路径。Optionally, you can follow these instructions to enable long file paths in Windows 10.

Visual Studio 中的新建项目对话框

可以在下一对话框中创建任何类型的 Service Fabric 服务。You can create any type of Service Fabric service from the next dialog. 对于本快速入门教程,请选择“来宾可执行文件”。 For this quickstart, choose Guest Executable.

将服务命名为“MyGuestService”,然后将右侧的选项设置为以下值: Name the service MyGuestService and set the options on the right to the following values:

设置Setting Value
代码包文件夹Code Package Folder <包含 Node.js 应用的文件夹><the folder with your Node.js app>
代码包行为Code Package Behavior 将文件夹内容复制到项目Copy folder contents to project
节目Program node.exenode.exe
参数Arguments server.jsserver.js
工作文件夹Working Folder CodePackageCodePackage

按“确定” 。Press OK.

Visual Studio 中的新建服务对话框

Visual Studio 会创建应用程序项目和执行组件服务项目,并在解决方案资源管理器中显示它们。Visual Studio creates the application project and the actor service project and displays them in Solution Explorer.

应用程序项目 (MyGuestApp) 不直接包含任何代码。The application project (MyGuestApp) doesn't contain any code directly. 该项目引用一组服务项目。The project references a set of service projects. 此外,它还包含三种其他类型的内容:Also, it contains three other types of content:

  • 发布配置文件 Publish profiles
    针对不同环境的工具首选项。Tooling preferences for different environments.

  • 脚本 Scripts
    用于部署/升级应用程序的 PowerShell 脚本。PowerShell script for deploying/upgrading your application.

  • 应用程序定义 Application definition
    包括 ApplicationPackageRoot 下的应用程序清单。 Includes the application manifest under ApplicationPackageRoot. 关联应用程序参数文件位于 ApplicationParameters 下,它们定义应用程序并使你可以专门为给定环境对其进行配置。Associated application parameter files are under ApplicationParameters, which define the application and allow you to configure it specifically for a given environment.

有关服务项目的内容概述,请参阅 Reliable Services 入门For an overview of the contents of the service project, see Getting started with Reliable Services.

设置网络Set up networking

要部署的示例 Node.js 应用使用端口 80 ,我们需告知 Service Fabric:我们需公开该端口。The example Node.js app we're deploying uses port 80 and we need to tell Service Fabric that we need that port exposed.

打开项目中的 ServiceManifest.xml 文件。Open the ServiceManifest.xml file in the project. 在清单的底部,有一个已定义了条目的 <Resources> \ <Endpoints>At the bottom of the manifest, there's a <Resources> \ <Endpoints> with an entry already defined. 修改该条目,添加 PortProtocolTypeModify that entry to add Port, Protocol, and Type.

  <Resources>
    <Endpoints>
      <!-- This endpoint is used by the communication listener to obtain the port on which to 
           listen. Please note that if your service is partitioned, this port is shared with 
           replicas of different partitions that are placed in your code. -->

      <Endpoint Name="MyGuestAppServiceTypeEndpoint" Port="80" Protocol="http" Type="Input" />
    </Endpoints>
  </Resources>

“部署到 Azure”Deploy to Azure

如果通过按 F5 来运行项目,则会将其部署到本地群集。If you press F5 and run the project, it's deployed to the local cluster. 但请将其部署到 Azure。However, let's deploy to Azure instead.

右键单击项目,选择“发布...”,此时会打开一个将内容发布到 Azure 所需的对话框。 Right-click on the project and choose Publish... which opens a dialog to publish to Azure.

用于 Service Fabric 服务的“发布到 Azure”对话框

选择 PublishProfiles\Cloud.xml 目标配置文件。Select the PublishProfiles\Cloud.xml target profile.

选择要将内容部署到其中的 Azure 帐户(如果尚未这样做)。If you haven't previously, choose an Azure account to deploy to. 如果还没有该帐户,请注册一个If you don't have one yet, sign-up for one.

在“连接终结点”下,选择要将内容部署到其中的 Service Fabric 群集。 Under Connection Endpoint, select the Service Fabric cluster to deploy to. 如果没有该群集,请选择“<新建群集...>”,此时会打开通往 Azure 门户的 Web 浏览器窗口。If you don't have one, select <Create New Cluster...> which opens up web browser window to the Azure portal. 有关详细信息,请参阅在门户中创建群集For more information, see create a cluster in the portal.

创建 Service Fabric 群集时,请确保将“自定义终结点”设置设为“80”。 When you create the Service Fabric cluster, make sure to set the Custom endpoints setting to 80.

终结点为自定义的 Service Fabric 节点类型配置

创建新的 Service Fabric 群集需要一段时间来完成。Creating a new Service Fabric cluster takes some time to complete. 创建完以后,请回到发布对话框,然后选择 <刷新>Once it has been created, go back to the publish dialog and select <Refresh>. 新群集列在下拉列表框中;请将其选中。The new cluster is listed in the drop-down box; select it.

按“发布” ,然后等待部署完成。Press Publish and wait for the deployment to finish.

这可能需要几分钟的时间。This may take a few minutes. 完成后,可能还需要等待几分钟,该应用程序才会完全可用。After it completes, it may take a few more minutes for the application to be fully available.

测试网站Test the website

发布服务以后,请在 Web 浏览器中对其进行测试。After your service has been published, test it in a web browser.

首先,打开 Azure 门户并找到 Service Fabric 服务。First, open the Azure portal and find your Service Fabric service.

检查服务地址的概览边栏选项卡。Check the overview blade of the service address. 使用“客户端连接终结点” 属性中的域名。Use the domain name from the Client connection endpoint property. 例如,http://mysvcfab1.chinanorth2.cloudapp.chinacloudapi.cnFor example, http://mysvcfab1.chinanorth2.cloudapp.chinacloudapi.cn.

Azure 门户中的 Service Fabric 概览边栏选项卡

导航到该地址,可以在其中看到 HELLO WORLD 响应。Navigate to this address where you'll see the HELLO WORLD response.

删除群集Delete the cluster

请勿忘记删除为本快速入门创建的所有资源,因为系统会对这些资源收费。Don't forget to delete all of the resources you've created for this quickstart, as you're charged for those resources.

后续步骤Next steps

阅读更多有关来宾可执行文件的内容。Read more about guest executables.