打包现有可执行文件并将其部署到 Service FabricPackage and deploy an existing executable to Service Fabric

将现有可执行文件打包为来宾可执行文件时,可以选择是使用 Visual Studio 项目模板,还是手动创建应用程序包When packaging an existing executable as a guest executable, you can choose either to use a Visual Studio project template or to create the application package manually. 使用 Visual Studio 时,新的项目模板为用户创建应用程序包结构和清单文件。Using Visual Studio, the application package structure and manifest files are created by the new project template for you.

提示

将现有 Windows 可执行文件打包到服务中的最简单方法就是使用 Visual Studio 以及在 Linux 上使用 YeomanThe easiest way to package an existing Windows executable into a service is to use Visual Studio and on Linux to use Yeoman

使用 Visual Studio 打包和部署现有可执行文件Use Visual Studio to package and deploy an existing executable

Visual Studio 提供 Service Fabric 服务模板将来宾可执行文件部署到 Service Fabric 群集。Visual Studio provides a Service Fabric service template to help you deploy a guest executable to a Service Fabric cluster.

  1. 依次选择“文件” > “新建项目” ,并创建一个 Service Fabric 应用程序。Choose File > New Project, and create a Service Fabric application.
  2. 选择“来宾可执行文件”作为服务模板。Choose Guest Executable as the service template.
  3. 单击“浏览” 选择包含所需可执行文件的文件夹,并填充余下的参数来创建服务。Click Browse to select the folder with your executable and fill in the rest of the parameters to create the service.
    • 代码包行为 。Code Package Behavior. 可以设置为将文件夹中的所有内容复制到 Visual Studio 项目,如果可执行文件不会更改,则这种设置会很有用。Can be set to copy all the content of your folder to the Visual Studio Project, which is useful if the executable does not change. 如果预期可执行文件会更改,并且希望能够动态选择新版本,则可以改为选择文件夹的链接。If you expect the executable to change and want the ability to pick up new builds dynamically, you can choose to link to the folder instead. 在 Visual Studio 中创建应用程序项目时,可以使用链接的文件夹。You can use linked folders when creating the application project in Visual Studio. 这会从项目内部链接到源位置,从而能够在来宾可执行文件的源目标中对它进行更新。This links to the source location from within the project, making it possible for you to update the guest executable in its source destination. 这些更新会在生成时成为应用程序包的一部分。Those updates become part of the application package on build.
    • Program 指定为了启动服务而应该运行的可执行文件。Program specifies the executable that should be run to start the service.
    • Arguments 指定应传递给可执行文件的参数。Arguments specifies the arguments that should be passed to the executable. 它可以是带有实参的形参的列表。It can be a list of parameters with arguments.
    • WorkingFolder 指定要启动的进程的工作目录。WorkingFolder specifies the working directory for the process that is going to be started. 可以指定三个值:You can specify three values:
      • CodeBase 指定将应用程序包中的 Code 目录(如上述文件结构中的 Code 目录所示)设为工作目录。CodeBase specifies that the working directory is going to be set to the code directory in the application package (Code directory shown in the preceding file structure).
      • CodePackage 指定将应用程序包的根目录(如上述文件结构中的 GuestService1Pkg 目录所示)设为工作目录。CodePackage specifies that the working directory is going to be set to the root of the application package (GuestService1Pkg shown in the preceding file structure).
      • Work 指定将文件放置在 Work 子目录中。Work specifies that the files are placed in a subdirectory called work.
  4. 为服务命名,并单击“确定” 。Give your service a name, and click OK.
  5. 如果服务需要使用终结点进行通信,现在可以在 ServiceManifest.xml 文件中添加协议、端口和类型。If your service needs an endpoint for communication, you can now add the protocol, port, and type to the ServiceManifest.xml file. 例如: <Endpoint Name="NodeAppTypeEndpoint" Protocol="http" Port="3000" UriScheme="http" PathSuffix="myapp/" Type="Input" />For example: <Endpoint Name="NodeAppTypeEndpoint" Protocol="http" Port="3000" UriScheme="http" PathSuffix="myapp/" Type="Input" />.
  6. 接下来,可以通过在 Visual Studio 中调试解决方案,针对本地群集使用打包和发布操作。You can now use the package and publish action against your local cluster by debugging the solution in Visual Studio. 准备就绪后,可将应用程序发布到远程群集,或者将解决方案签入源代码管理。When ready, you can publish the application to a remote cluster or check in the solution to source control.
  7. 请阅读检查正在运行的应用程序,了解如何查看在 Service Fabric Explorer 中运行的来宾可执行服务。Read check your running application to see how to view your guest executable service running in Service Fabric Explorer.

有关示例演练,请参阅使用 Visual Studio 创建第一个来宾可执行应用程序For an example walkthrough, see Create your first guest executable application using Visual Studio.

使用 Yeoman 在 Linux 上打包和部署现有可执行文件Use Yeoman to package and deploy an existing executable on Linux

用于在 Linux 上创建和部署来宾可执行文件的过程与部署 csharp 或 java 应用程序相同。The procedure for creating and deploying a guest executable on Linux is the same as deploying a csharp or java application.

  1. 在终端中,键入 yo azuresfguestIn a terminal, type yo azuresfguest.
  2. 为应用程序命名。Name your application.
  3. 命名服务,并提供详细信息,包括可执行文件的路径以及调用该服务所必须使用的参数。Name your service, and provide the details including path of the executable and the parameters it must be invoked with.

Yeoman 创建应用程序包,其中包含相应的应用程序和清单文件,以及安装和卸载脚本。Yeoman creates an application package with the appropriate application and manifest files along with install and uninstall scripts.

手动打包和部署现有的可执行文件Manually package and deploy an existing executable

手动打包来宾可执行文件的过程基于以下常规步骤:The process of manually packaging a guest executable is based on the following general steps:

  1. 创建包目录结构。Create the package directory structure.
  2. 添加应用程序的代码和配置文件。Add the application's code and configuration files.
  3. 编辑服务清单文件。Edit the service manifest file.
  4. 编辑应用程序清单文件。Edit the application manifest file.

创建包目录结构Create the package directory structure

可以首先创建目录结构,如打包 Azure Service Fabric 应用中所述。You can start by creating the directory structure, as described in Package an Azure Service Fabric App.

添加应用程序的代码和配置文件Add the application's code and configuration files

创建了目录结构之后,可以在 code 和 config 目录下添加应用程序的代码和配置文件。After you have created the directory structure, you can add the application's code and configuration files under the code and config directories. 还可以在 code 或 config 目录下创建其他目录或子目录。You can also create additional directories or subdirectories under the code or config directories.

Service Fabric 对应用程序根目录下的内容执行了 xcopy,因此除创建 code 和 settings 这两个顶级目录以外没有其他任何预定义结构可以使用。Service Fabric does an xcopy of the content of the application root directory, so there is no predefined structure to use other than creating two top directories, code and settings. (如有需要可以选取其他名称。(You can pick different names if you want. 有关详细信息,请参阅下一节内容。)More details are in the next section.)

备注

确保包含应用程序需要的所有文件和依赖项。Make sure that you include all the files and dependencies that the application needs. Service Fabric 将复制群集中所有节点上的应用程序包的内容,会在群集中部署应用程序的服务。Service Fabric copies the content of the application package on all nodes in the cluster where the application's services are going to be deployed. 包中应该包含应用程序需要运行的所有代码。The package should contain all the code that the application needs to run. 不要假定已安装依赖项。Do not assume that the dependencies are already installed.

编辑服务清单文件Edit the service manifest file

下一步是编辑服务清单文件以包含如下信息:The next step is to edit the service manifest file to include the following information:

  • 服务类型的名称。The name of the service type. 这是 Service Fabric 用于标识服务的 ID。This is an ID that Service Fabric uses to identify a service.
  • 用于启动应用程序的命令 (ExeHost)。The command to use to launch the application (ExeHost).
  • 为设置应用程序而需要运行的任何脚本 (SetupEntrypoint)。Any script that needs to be run to set up the application (SetupEntrypoint).

下面是 ServiceManifest.xml 文件的示例:The following is an example of a ServiceManifest.xml file:

<?xml version="1.0" encoding="utf-8"?>
<ServiceManifest xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" Name="NodeApp" Version="1.0.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric">
   <ServiceTypes>
      <StatelessServiceType ServiceTypeName="NodeApp" UseImplicitHost="true"/>
   </ServiceTypes>
   <CodePackage Name="code" Version="1.0.0.0">
      <SetupEntryPoint>
         <ExeHost>
             <Program>scripts\launchConfig.cmd</Program>
         </ExeHost>
      </SetupEntryPoint>
      <EntryPoint>
         <ExeHost>
            <Program>node.exe</Program>
            <Arguments>bin/www</Arguments>
            <WorkingFolder>CodePackage</WorkingFolder>
         </ExeHost>
      </EntryPoint>
   </CodePackage>
   <Resources>
      <Endpoints>
         <Endpoint Name="NodeAppTypeEndpoint" Protocol="http" Port="3000" Type="Input" />
      </Endpoints>
   </Resources>
</ServiceManifest>

以下部分介绍了需要更新的文件的不同部分。The following sections go over the different parts of the file that you need to update.

更新 ServiceTypesUpdate ServiceTypes

<ServiceTypes>
  <StatelessServiceType ServiceTypeName="NodeApp" UseImplicitHost="true" />
</ServiceTypes>
  • 可为 ServiceTypeName选择所需的任何名称。You can pick any name that you want for ServiceTypeName. 该值在 ApplicationManifest.xml 文件中用于标识服务。The value is used in the ApplicationManifest.xml file to identify the service.
  • 指定 UseImplicitHost="true"Specify UseImplicitHost="true". 此属性告知 Service Fabric 服务基于自包含的应用,因此 Service Fabric 只需要将其作为进程启动并监视其运行状况。This attribute tells Service Fabric that the service is based on a self-contained app, so all Service Fabric needs to do is to launch it as a process and monitor its health.

更新 CodePackageUpdate CodePackage

CodePackage 元素指定服务代码的位置(和版本)。The CodePackage element specifies the location (and version) of the service's code.

<CodePackage Name="Code" Version="1.0.0.0">

Name 元素用于在包含服务代码的应用程序包中指定目录的名称。The Name element is used to specify the name of the directory in the application package that contains the service's code. CodePackage 也有 version 属性。CodePackage also has the version attribute. 这不仅可用于指定代码的版本,还可用于升级服务的代码,具体方法为在 Service Fabric 中使用应用程序生命周期管理基础结构。This can be used to specify the version of the code, and can also potentially be used to upgrade the service's code by using the application lifecycle management infrastructure in Service Fabric.

可选:更新 SetupEntrypointOptional: Update SetupEntrypoint

<SetupEntryPoint>
   <ExeHost>
       <Program>scripts\launchConfig.cmd</Program>
   </ExeHost>
</SetupEntryPoint>

SetupEntryPoint 元素用于指定在启动服务代码之前应执行的任何可执行文件或批处理文件。The SetupEntryPoint element is used to specify any executable or batch file that should be executed before the service's code is launched. 这是一个可选步骤,因此在不需要初始化时无需包含在内。It is an optional step, so it does not need to be included if there is no initialization required. 每次重新启动服务时,会执行 SetupEntryPoint。The SetupEntryPoint is executed every time the service is restarted.

只有一个 SetupEntryPoint,因此如果应用程序的设置需要多个脚本,则设置脚本需要组合在单个批处理文件中。There is only one SetupEntryPoint, so setup scripts need to be grouped in a single batch file if the application's setup requires multiple scripts. SetupEntryPoint 可以执行任何类型的文件 - 可执行文件、批处理文件和 PowerShell cmdlet。The SetupEntryPoint can execute any type of file: executable files, batch files, and PowerShell cmdlets. 有关详细信息,请参阅配置 SetupEntryPointFor more details, see Configure SetupEntryPoint.

在上面的示例中,SetupEntryPoint 会运行位于 code 目录的 scripts 子目录中的 LaunchConfig.cmd 批处理文件(假定 WorkingFolder 元素设置为 CodeBase)。In the preceding example, the SetupEntryPoint runs a batch file called LaunchConfig.cmd that is located in the scripts subdirectory of the code directory (assuming the WorkingFolder element is set to CodeBase).

更新 EntryPointUpdate EntryPoint

<EntryPoint>
  <ExeHost>
    <Program>node.exe</Program>
    <Arguments>bin/www</Arguments>
    <WorkingFolder>CodeBase</WorkingFolder>
  </ExeHost>
</EntryPoint>

服务清单文件中的 EntryPoint 元素用于指定如何启动该服务。The EntryPoint element in the service manifest file is used to specify how to launch the service.

ExeHost 元素指定应用于启动该服务的可执行文件(和参数)。The ExeHost element specifies the executable (and arguments) that should be used to launch the service. 可以选择将 IsExternalExecutable="true" 属性添加到 ExeHost,以指示该程序是代码包外部的外部可执行文件。You can optionally add the IsExternalExecutable="true" attribute to ExeHost to indicate that the program is an external executable outside of the code package. 例如,<ExeHost IsExternalExecutable="true">For example, <ExeHost IsExternalExecutable="true">.

  • Program 指定应启动服务的可执行文件的名称。Program specifies the name of the executable that should start the service.
  • Arguments 指定应传递给可执行文件的参数。Arguments specifies the arguments that should be passed to the executable. 它可以是带有实参的形参的列表。It can be a list of parameters with arguments.
  • WorkingFolder 指定要启动的进程的工作目录。WorkingFolder specifies the working directory for the process that is going to be started. 可以指定三个值:You can specify three values:
    • CodeBase 指定工作目录将设置为应用程序包中的 code 目录(前述文件结构中的 Code 目录)。CodeBase specifies that the working directory is going to be set to the code directory in the application package (Code directory in the preceding file structure).
    • CodePackage 指定将应用程序包的根目录(如上述文件结构中的 GuestService1Pkg 所示)设为工作目录。CodePackage specifies that the working directory is going to be set to the root of the application package (GuestService1Pkg in the preceding file structure).
      • Work 指定将文件放置在 Work 子目录中。Work specifies that the files are placed in a subdirectory called work.

WorkingFolder 用于设置正确的工作目录,以便应用程序或初始化脚本可以使用相对路径。The WorkingFolder is useful to set the correct working directory so that relative paths can be used by either the application or initialization scripts.

更新终结点并在命名服务中注册以进行通信Update Endpoints and register with Naming Service for communication

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

在前面的示例中, Endpoint 元素指定应用程序可以侦听的终结点。In the preceding example, the Endpoint element specifies the endpoints that the application can listen on. 在此示例中,Node.js 应用程序侦听端口 3000 上的 http 流量。In this example, the Node.js application listens on http on port 3000.

此外,可以要求 Service Fabric 将此终结点发布到命名服务,使其他服务可以发现此服务的终结点地址。Furthermore you can ask Service Fabric to publish this endpoint to the Naming Service so other services can discover the endpoint address to this service. 然后,便可以在服务(来宾可执行文件)之间进行通信。This enables you to be able to communicate between services that are guest executables. 已发布的终结点地址格式为 UriScheme://IPAddressOrFQDN:Port/PathSuffixThe published endpoint address is of the form UriScheme://IPAddressOrFQDN:Port/PathSuffix. UriSchemePathSuffix 是可选属性。UriScheme and PathSuffix are optional attributes. IPAddressOrFQDN 是此可执行文件所在节点的 IP 地址或完全限定的域名,系统会为你计算此参数。IPAddressOrFQDN is the IP address or fully qualified domain name of the node this executable gets placed on, and it is calculated for you.

在以下示例中,部署服务后,Service Fabric Explorer 中会显示针对服务实例发布的终结点(类似于 http://10.1.4.92:3000/myapp/ )。In the following example, once the service is deployed, in Service Fabric Explorer you see an endpoint similar to http://10.1.4.92:3000/myapp/ published for the service instance. 如果这是本地计算机,则显示 http://localhost:3000/myapp/Or if this is a local machine, you see http://localhost:3000/myapp/.

<Endpoints>
   <Endpoint Name="NodeAppTypeEndpoint" Protocol="http" Port="3000"  UriScheme="http" PathSuffix="myapp/" Type="Input" />
</Endpoints>

可以将这些地址与反向代理结合使用,在服务之间进行通信。You can use these addresses with reverse proxy to communicate between services.

编辑应用程序清单文件Edit the application manifest file

配置 Servicemanifest.xml 文件之后,需要对 ApplicationManifest.xml 文件进行一些更改,确保使用正确的服务类型和名称。Once you have configured the Servicemanifest.xml file, you need to make some changes to the ApplicationManifest.xml file to ensure that the correct service type and name are used.

<?xml version="1.0" encoding="utf-8"?>
<ApplicationManifest xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" ApplicationTypeName="NodeAppType" ApplicationTypeVersion="1.0" xmlns="http://schemas.microsoft.com/2011/01/fabric">
   <ServiceManifestImport>
      <ServiceManifestRef ServiceManifestName="NodeApp" ServiceManifestVersion="1.0.0.0" />
   </ServiceManifestImport>
</ApplicationManifest>

ServiceManifestImportServiceManifestImport

ServiceManifestImport 元素中,可以指定要包含在应用中的一个或多个服务。In the ServiceManifestImport element, you can specify one or more services that you want to include in the app. ServiceManifestName 指定 ServiceManifest.xml 文件所在的目录名称,用来参考服务。Services are referenced with ServiceManifestName, which specifies the name of the directory where the ServiceManifest.xml file is located.

<ServiceManifestImport>
  <ServiceManifestRef ServiceManifestName="NodeApp" ServiceManifestVersion="1.0.0.0" />
</ServiceManifestImport>

设置日志记录Set up logging

对于来宾可执行文件,最好能够查看控制台日志,以查明应用程序和配置脚本是否显示了任何错误。For guest executables, it is useful to be able to see console logs to find out if the application and configuration scripts show any errors. 可以使用 ConsoleRedirection 元素在 ServiceManifest.xml 文件中配置控制台重定向。Console redirection can be configured in the ServiceManifest.xml file using the ConsoleRedirection element.

警告

永远不要在生产中部署的应用程序中使用控制台重定向策略,因为这可能会影响应用程序故障转移。Never use the console redirection policy in an application that is deployed in production because this can affect the application failover. 将其用于本地开发和调试目的。Only use this for local development and debugging purposes.

<EntryPoint>
  <ExeHost>
    <Program>node.exe</Program>
    <Arguments>bin/www</Arguments>
    <WorkingFolder>CodeBase</WorkingFolder>
    <ConsoleRedirection FileRetentionCount="5" FileMaxSizeInKb="2048"/>
  </ExeHost>
</EntryPoint>

ConsoleRedirection 可用于将控制台输出(stdout 和 stderr)重定向到工作目录。ConsoleRedirection can be used to redirect console output (both stdout and stderr) to a working directory. 这可验证在 Service Fabric 群集中设置或执行应用程序时没有出现错误。This provides the ability to verify that there are no errors during the setup or execution of the application in the Service Fabric cluster.

FileRetentionCount 确定保存在工作目录中的文件的数量。FileRetentionCount determines how many files are saved in the working directory. 例如,值 5 表示前 5 个执行的日志文件存储在工作目录中。A value of 5, for example, means that the log files for the previous five executions are stored in the working directory.

FileMaxSizeInKb 指定日志文件的最大大小。FileMaxSizeInKb specifies the maximum size of the log files.

日志文件保存在服务的一个工作目录中。Log files are saved in one of the service's working directories. 若要确定文件所在的位置,请使用 Service Fabric Explorer 来确定运行服务的节点以及所使用的工作目录。To determine where the files are located, use Service Fabric Explorer to determine which node the service is running on, and which working directory is being used. 本文中后面部分介绍了此过程。This process is covered later in this article.

部署Deployment

最后一步是部署应用程序The last step is to deploy your application. 下面的 PowerShell 脚本展示了如何将应用程序部署到本地开发群集,并启动新的 Service Fabric 服务。The following PowerShell script shows how to deploy your application to the local development cluster, and start a new Service Fabric service.


Connect-ServiceFabricCluster localhost:19000

Write-Host 'Copying application package...'
Copy-ServiceFabricApplicationPackage -ApplicationPackagePath 'C:\Dev\MultipleApplications' -ImageStoreConnectionString 'file:C:\SfDevCluster\Data\ImageStoreShare' -ApplicationPackagePathInImageStore 'nodeapp'

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

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

New-ServiceFabricService -ApplicationName 'fabric:/nodeapp' -ServiceName 'fabric:/nodeapp/nodeappservice' -ServiceTypeName 'NodeApp' -Stateless -PartitionSchemeSingleton -InstanceCount 1

提示

如果包较大或包含多个文件,请先压缩包,并将其复制到映像存储区。Compress the package before copying to the image store if the package is large or has many files. 此处了解详细信息。Read more here.

Service Fabric 服务可以采用各种“配置”进行部署。A Service Fabric service can be deployed in various "configurations." 例如,可将其作为单个或多个实例部署,或者可将其以这样一种方式部署:在 Service Fabric 群集的每个节点上都有一个服务实例。For example, it can be deployed as single or multiple instances, or it can be deployed in such a way that there is one instance of the service on each node of the Service Fabric cluster.

New-ServiceFabricService cmdlet 的 InstanceCount 参数用于指定应在 Service Fabric 群集中启动的服务实例的数量。The InstanceCount parameter of the New-ServiceFabricService cmdlet is used to specify how many instances of the service should be launched in the Service Fabric cluster. 可以根据要部署的应用程序类型设置 InstanceCount 值。You can set the InstanceCount value, depending on the type of application that you are deploying. 最常见的两种方案是:The two most common scenarios are:

  • InstanceCount = "1"InstanceCount = "1". 在此情况下,只会在群集中部署一个服务实例。In this case, only one instance of the service is deployed in the cluster. Service Fabric 的计划程序确定在哪一个节点上部署服务。Service Fabric's scheduler determines which node the service is going to be deployed on.
  • InstanceCount ="-1"InstanceCount ="-1". 在此情况下,会在 Service Fabric 群集中的每个节点上部署一个服务实例。In this case, one instance of the service is deployed on every node in the Service Fabric cluster. 结果是群集中的每个节点上都有一个(且只有一个)服务实例。The result is having one (and only one) instance of the service for each node in the cluster.

这是前端应用程序(不包括 REST 终结点)的有用配置,因为客户端应用程序需要“连接到”群集中的任何节点才能使用该终结点。This is a useful configuration for front-end applications (for example, a REST endpoint), because client applications need to "connect" to any of the nodes in the cluster to use the endpoint. 例如,当 Service Fabric 群集的所有节点都连接到负载均衡器时,也可以使用此配置。This configuration can also be used when, for example, all nodes of the Service Fabric cluster are connected to a load balancer. 然后,客户端流量可以分布于在集群中所有节点上运行的服务。Client traffic can then be distributed across the service that is running on all nodes in the cluster.

检查正在运行的应用程序Check your running application

在 Service Fabric Explorer 中,确定服务在其中运行的节点。In Service Fabric Explorer, identify the node where the service is running. 在此示例中,它在节点 1 上运行:In this example, it runs on Node1:

运行服务的节点

导航到该节点并浏览到应用程序后,会看到基本节点信息(包括在它磁盘上的位置)。If you navigate to the node and browse to the application, you see the essential node information, including its location on disk.

磁盘上的位置

如果使用服务器资源管理器浏览目录,可以找到工作目录和服务的日志文件夹,如以下屏幕截图所示:If you browse to the directory by using Server Explorer, you can find the working directory and the service's log folder, as shown in the following screenshot:

日志的位置

后续步骤Next steps

在本文中,我们了解了如何打包来宾可执行文件并将其部署到 Service Fabric。In this article, you have learned how to package a guest executable and deploy it to Service Fabric. 有关相关信息和任务,请参阅以下文章。See the following articles for related information and tasks.