部署多个来宾可执行文件Deploy multiple guest executables

本文介绍如何打包多个来宾可执行文件并部署到 Azure Service Fabric。This article shows how to package and deploy multiple guest executables to Azure Service Fabric. 要生成并部署单个 Service Fabric 包,请参阅如何将来宾可执行文件部署到 Service FabricFor building and deploying a single Service Fabric package read how to deploy a guest executable to Service Fabric.

虽然本演练演示如何部署将 MongoDB 用作数据存储并具有 Node.js 前端的应用程序,但是可以将这些步骤套用于任何与另一个应用程序具有依赖关系的应用程序。While this walkthrough shows how to deploy an application with a Node.js front end that uses MongoDB as the data store, you can apply the steps to any application that has dependencies on another application.

可使用 Visual Studio 生成包含多个来宾可执行文件的应用程序包。You can use Visual Studio to produce the application package that contains multiple guest executables. 请参阅使用 Visual Studio 打包现有应用程序See Using Visual Studio to package an existing application. 添加第一个来宾可执行文件后,右键单击应用程序项目,并依次选择 “添加”->“新建 Service Fabric 服务” ,将第二个来宾可执行文件项目添加到解决方案中。After you have added the first guest executable, right click on the application project and select the Add->New Service Fabric service to add the second guest executable project to the solution. 注意:如果选择在 Visual Studio 项目中链接源,则生成 Visual Studio 解决方案可确保应用程序包能够与源中的更改保持同步。Note: If you choose to link the source in the Visual Studio project, building the Visual Studio solution, will make sure that your application package is up to date with changes in the source.

示例Samples

手动打包多个来宾可执行文件应用程序Manually package the multiple guest executable application

或者可以手动打包来宾可执行文件。Alternatively you can manually package the guest executable. 有关详细信息,请参阅手动打包和部署现有的可执行文件For details, see Manually package and deploy an existing executable.

打包 Node.js 应用程序Packaging the Node.js application

本文假设 Service Fabric 群集中的节点上未安装 Node.js。This article assumes that Node.js is not installed on the nodes in the Service Fabric cluster. 因此,你需要在打包之前,先将 Node.exe 添加到节点应用程序的根目录中。As a consequence, you need to add Node.exe to the root directory of your node application before packaging. Node.js 应用程序(使用 Express Web 框架和 Jade 模板引擎)的目录结构看起来应该与以下类似:The directory structure of the Node.js application (using Express web framework and Jade template engine) should look similar to the one below:

|-- NodeApplication
    |-- bin
        |-- www
    |-- node_modules
        |-- .bin
        |-- express
        |-- jade
        |-- etc.
    |-- public
        |-- images
        |-- etc.
    |-- routes
        |-- index.js
        |-- users.js
    |-- views
        |-- index.jade
        |-- etc.
    |-- app.js
    |-- package.json
    |-- node.exe

下一个步骤为 Node.js 应用程序创建应用程序包。As a next step, you create an application package for the Node.js application. 以下代码会创建包含 Node.js 应用程序的 Service Fabric 应用程序包。The code below creates a Service Fabric application package that contains the Node.js application.

.\ServiceFabricAppPackageUtil.exe /source:'[yourdirectory]\MyNodeApplication' /target:'[yourtargetdirectory] /appname:NodeService /exe:'node.exe' /ma:'bin/www' /AppType:NodeAppType

下面描述了所使用的参数:Below is a description of the parameters that are being used:

  • /source 指向应打包的应用程序的目录。/source points to the directory of the application that should be packaged.
  • /target 定义应在其中创建包的目录。/target defines the directory in which the package should be created. 此目录必须与源目录不同。This directory has to be different from the source directory.
  • /appname 定义现有应用程序的应用程序名称。/appname defines the application name of the existing application. 请务必了解,这会转换成清单中的服务名称,而不是转换成 Service Fabric 应用程序名称。It's important to understand that this translates to the service name in the manifest, and not to the Service Fabric application name.
  • /exe 定义 Service Fabric 应启动的可执行文件(在此示例中,为 node.exe)。/exe defines the executable that Service Fabric is supposed to launch, in this case node.exe.
  • /ma 定义要用于启动可执行文件的参数。/ma defines the argument that is being used to launch the executable. 由于未安装 Node.js,因此 Service Fabric 需要通过执行 node.exe bin/www 来启动 Node.js Web 服务器。As Node.js is not installed, Service Fabric needs to launch the Node.js web server by executing node.exe bin/www. /ma:'bin/www' 指示打包工具使用 bin/www 作为 node.exe 的参数。/ma:'bin/www' tells the packaging tool to use bin/www as the argument for node.exe.
  • /AppType 定义 Service Fabric 应用程序类型名称。/AppType defines the Service Fabric application type name.

如果浏览到 /target 参数中指定的目录,则可以看到工具已创建完全正常运行的 Service Fabric 包,如下所示:If you browse to the directory that was specified in the /target parameter, you can see that the tool has created a fully functioning Service Fabric package as shown below:

|--[yourtargetdirectory]
    |-- NodeApplication
        |-- C
              |-- bin
              |-- data
              |-- node_modules
              |-- public
              |-- routes
              |-- views
              |-- app.js
              |-- package.json
              |-- node.exe
        |-- config
              |--Settings.xml
        |-- ServiceManifest.xml
    |-- ApplicationManifest.xml

所生成的 ServiceManifest.xml 现在有一个描述应该如何启动 Node.js Web 服务器的部分,如以下代码片段所示:The generated ServiceManifest.xml now has a section that describes how the Node.js web server should be launched, as shown in the code snippet below:

<CodePackage Name="C" Version="1.0">
    <EntryPoint>
        <ExeHost>
            <Program>node.exe</Program>
            <Arguments>'bin/www'</Arguments>
            <WorkingFolder>CodePackage</WorkingFolder>
        </ExeHost>
    </EntryPoint>
</CodePackage>

在此示例中,Node.js Web 服务器会侦听端口 3000,所以需要更新 ServiceManifest.xml 文件中的终结点信息,如下所示。In this sample, the Node.js web server listens to port 3000, so you need to update the endpoint information in the ServiceManifest.xml file as shown below.

<Resources>
      <Endpoints>
         <Endpoint Name="NodeServiceEndpoint" Protocol="http" Port="3000" Type="Input" />
      </Endpoints>
</Resources>

打包 MongoDB 应用程序Packaging the MongoDB application

既然已打包 Node.js 应用程序,可以继续打包 MongoDB。Now that you have packaged the Node.js application, you can go ahead and package MongoDB. 如前文所述,现在进行的步骤并非特定于 Node.js 和 MongoDB 的步骤。As mentioned before, the steps that you go through now are not specific to Node.js and MongoDB. 事实上,它们适用于所有要打包在一起以作为一个 Service Fabric 应用程序的应用程序。In fact, they apply to all applications that are meant to be packaged together as one Service Fabric application.

若要打包 MongoDB,需要确保打包 Mongod.exe 和 Mongo.exe。To package MongoDB, you want to make sure you package Mongod.exe and Mongo.exe. 这两个二进制文件都位于 MongoDB 安装目录的 bin 目录中。Both binaries are located in the bin directory of your MongoDB installation directory. 目录结构类似于下面的结构。The directory structure looks similar to the one below.

|-- MongoDB
    |-- bin
        |-- mongod.exe
        |-- mongo.exe
        |-- anybinary.exe

Service Fabric 需要使用类似于下面的命令来启动 MongoDB,因此打包 MongoDB 时,需要使用 /ma 参数。Service Fabric needs to start MongoDB with a command similar to the one below, so you need to use the /ma parameter when packaging MongoDB.

mongod.exe --dbpath [path to data]

备注

如果你将 MongoDB 数据目录放在节点的本地目录中,当节点发生故障时,不会保留数据。The data is not being preserved in the case of a node failure if you put the MongoDB data directory on the local directory of the node. 应该使用持久存储或实现 MongoDB 副本集以防止数据丢失。You should either use durable storage or implement a MongoDB replica set in order to prevent data loss.

在 PowerShell 或命令外壳中,我们会使用下列参数运行打包工具:In PowerShell or the command shell, we run the packaging tool with the following parameters:

.\ServiceFabricAppPackageUtil.exe /source: [yourdirectory]\MongoDB' /target:'[yourtargetdirectory]' /appname:MongoDB /exe:'bin\mongod.exe' /ma:'--dbpath [path to data]' /AppType:NodeAppType

为了将 MongoDB 添加到 Service Fabric 应用程序包,需要确保 /target 参数指向已经包含应用程序清单及 Node.js 应用程序的同一个目录。In order to add MongoDB to your Service Fabric application package, you need to make sure that the /target parameter points to the same directory that already contains the application manifest along with the Node.js application. 此外,还需要确保使用的是相同的 ApplicationType 名称。You also need to make sure that you are using the same ApplicationType name.

让我们浏览到该目录并检查已创建的工具。Let's browse to the directory and examine what the tool has created.

|--[yourtargetdirectory]
    |-- MyNodeApplication
    |-- MongoDB
        |-- C
            |--bin
                |-- mongod.exe
                |-- mongo.exe
                |-- etc.
        |-- config
            |--Settings.xml
        |-- ServiceManifest.xml
    |-- ApplicationManifest.xml

正如所见,工具已将新文件夹“MongoDB”添加到包含 MongoDB 二进制文件的目录中。As you can see, the tool added a new folder, MongoDB, to the directory that contains the MongoDB binaries. 如果打开 ApplicationManifest.xml 文件,可以看到包现在包含 Node.js 应用程序和 MongoDB。If you open the ApplicationManifest.xml file, you can see that the package now contains both the Node.js application and MongoDB. 以下代码会显示应用程序清单的内容。The code below shows the content of the application manifest.

<ApplicationManifest xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" ApplicationTypeName="MyNodeApp" ApplicationTypeVersion="1.0" xmlns="http://schemas.microsoft.com/2011/01/fabric">
   <ServiceManifestImport>
      <ServiceManifestRef ServiceManifestName="MongoDB" ServiceManifestVersion="1.0" />
   </ServiceManifestImport>
   <ServiceManifestImport>
      <ServiceManifestRef ServiceManifestName="NodeService" ServiceManifestVersion="1.0" />
   </ServiceManifestImport>
   <DefaultServices>
      <Service Name="MongoDBService">
         <StatelessService ServiceTypeName="MongoDB">
            <SingletonPartition />
         </StatelessService>
      </Service>
      <Service Name="NodeServiceService">
         <StatelessService ServiceTypeName="NodeService">
            <SingletonPartition />
         </StatelessService>
      </Service>
   </DefaultServices>
</ApplicationManifest>  

发布应用程序Publishing the application

最后一个步骤是使用以下 PowerShell 脚本,将应用程序发布到本地 Service Fabric 群集:The last step is to publish the application to the local Service Fabric cluster by using the PowerShell scripts below:

Connect-ServiceFabricCluster localhost:19000

Write-Host 'Copying application package...'
Copy-ServiceFabricApplicationPackage -ApplicationPackagePath '[yourtargetdirectory]' -ImageStoreConnectionString 'file:C:\SfDevCluster\Data\ImageStoreShare' -ApplicationPackagePathInImageStore 'NodeAppType'

Write-Host 'Registering application type...'
Register-ServiceFabricApplicationType -ApplicationPathInImageStore 'NodeAppType'

New-ServiceFabricApplication -ApplicationName 'fabric:/NodeApp' -ApplicationTypeName 'NodeAppType' -ApplicationTypeVersion 1.0  

将应用程序成功发布到本地群集之后,便可以在我们在 Node.js 应用程序的服务清单中输入的端口(例如 http://localhost:3000)上访问 Node.js 应用程序。Once the application is successfully published to the local cluster, you can access the Node.js application on the port that we have entered in the service manifest of the Node.js application--for example http://localhost:3000.

在本教程中,你学习了如何轻松地将两个现有应用程序打包成一个 Service Fabric 应用程序。In this tutorial, you have seen how to easily package two existing applications as one Service Fabric application. 还已了解如何将其部署到 Service Fabric,以便它能够从一些 Service Fabric 功能(例如高可用性和运行状况系统集成)中获益。You have also learned how to deploy it to Service Fabric so that it can benefit from some of the Service Fabric features, such as high availability and health system integration.

在 Linux 上使用 Yeoman 将更多来宾可执行文件添加到现有应用程序Adding more guest executables to an existing application using Yeoman on Linux

要将另一个服务添加到使用 yo 创建的应用程序,请执行以下步骤:To add another service to an application already created using yo, perform the following steps:

  1. 将目录更改为现有应用程序的根目录。Change directory to the root of the existing application. 例如 cd ~/YeomanSamples/MyApplication(如果 MyApplication 是 Yeoman 创建的应用程序)。For example, cd ~/YeomanSamples/MyApplication, if MyApplication is the application created by Yeoman.
  2. 运行 yo azuresfguest:AddService 并提供必要的详细信息。Run yo azuresfguest:AddService and provide the necessary details.

后续步骤Next steps