Service Fabric 应用程序资源模型Service Fabric application resource model

有多个选项可用于在 Service Fabric 群集上部署 Azure Service Fabric 应用程序。You have multiple options for deploying Azure Service Fabric applications on your Service Fabric cluster. 建议使用 Azure 资源管理器。We recommend using Azure Resource Manager. 如果使用资源管理器,可以采用 JSON 描述应用程序和服务,然后将其部署到群集所在的同一资源管理器模板中。If you use Resource Manager, you can describe applications and services in JSON, and then deploy them in the same Resource Manager template as your cluster. 不同于使用 PowerShell 或 Azure CLI 来部署和管理应用程序,如果使用资源管理器,则不必等待群集准备就绪;应用程序注册、预配和部署都可以在一个步骤中进行。Unlike using PowerShell or Azure CLI to deploy and manage applications, if you use Resource Manager, you don't have to wait for the cluster to be ready; application registration, provisioning, and deployment can all happen in one step. 使用资源管理器是在群集中管理应用程序生命周期的最佳方式。Using Resource Manager is the best way to manage the application life cycle in your cluster. 有关详细信息,请参阅最佳做法:基础结构即代码For more information, see Best practices: Infrastructure as code.

在资源管理器中将应用程序作为资源进行管理有助于在以下方面获得改进:Managing your applications as resources in Resource Manager can help you gain improvements in these areas:

  • 审核线索:资源管理器审核每个操作并保留详细的活动日志。Audit trail: Resource Manager audits every operation and keeps a detailed activity log. 活动日志可帮助你跟踪对应用程序和群集所做的任何更改。An activity log can help you trace any changes made to the applications and to your cluster.
  • 基于角色的访问控制:可以使用相同资源管理器模板来管理对群集的访问以及对群集上部署的应用程序的访问。Role-based access control: You can manage access to clusters and to applications deployed on the cluster by using the same Resource Manager template.
  • 管理效率:使用资源管理器可以在单个位置(Azure 门户)管理群集和关键应用程序部署。Management efficiency: Using Resource Manager gives you a single location (the Azure portal) for managing your cluster and critical application deployments.

本文档将介绍以下操作:In this document, you will learn how to:

  • 使用资源管理器部署应用程序资源。Deploy application resources by using Resource Manager.
  • 使用资源管理器升级应用程序资源。Upgrade application resources by using Resource Manager.
  • 删除应用程序资源。Delete application resources.

部署应用程序资源Deploy application resources

可以使用资源管理器应用程序资源模型来部署应用程序及其服务,所需采用的概要步骤如下:The high-level steps you take to deploy an application and its services by using the Resource Manager application resource model are:

  1. 将应用程序代码打包。Package the application code.
  2. 上传该包。Upload the package.
  3. 在资源管理器模板中引用该包的位置作为应用程序资源。Reference the location of the package in a Resource Manager template as an application resource.

有关详细信息,请查看将应用程序打包For more information, view Package an application.

然后,创建资源管理器模板,使用应用程序详细信息更新参数文件,并将模板部署到 Service Fabric 群集上。Then, you create a Resource Manager template, update the parameters file with application details, and deploy the template on the Service Fabric cluster. 浏览示例Explore samples.

创建存储帐户Create a storage account

若要从资源管理器模板部署应用程序,必须有一个存储帐户。To deploy an application from a Resource Manager template, you must have a storage account. 存储帐户用于暂存应用程序映像。The storage account is used to stage the application image.

可以重复使用现有的存储帐户或创建新的存储帐户来暂存应用程序。You can reuse an existing storage account or you can create a new storage account for staging your applications. 如果使用现有的存储帐户,则可以跳过此步骤。If you use an existing storage account, you can skip this step.


配置存储帐户Configure your storage account

创建存储帐户后,需要创建一个可在其中暂存应用程序的 Blob 容器。After the storage account is created, you create a blob container where the applications can be staged. 在 Azure 门户中,转到要存储应用程序的 Azure 存储帐户。In the Azure portal, go to the Azure Storage account where you want to store your applications. 选择“Blob” > “添加容器”。Select Blobs > Add Container.

可以通过将公共访问级别设置为“专用”来保护群集中的资源。Resources in your cluster can be secured by setting the public access level to private. 可以通过多种方式授予访问权限:You can grant access in multiple ways:

以下屏幕截图中的示例使用对 Blob 的匿名读取访问。The example in the following screenshot uses anonymous read access for blobs.

创建 Blob

在存储帐户中暂存应用程序Stage the application in your storage account

在部署应用程序之前,必须在 Blob 存储中暂存该应用程序。Before you can deploy an application, you must stage the application in blob storage. 在本教程中,我们将手动创建应用程序包。In this tutorial, we create the application package manually. 请记住,此步骤可以自动执行。Keep in mind that this step can be automated. 有关详细信息,请参阅将应用程序打包For more information, see Package an application.

在本教程中,我们将使用投票示例应用程序In this tutorial, we use the Voting sample application.

  1. 在 Visual Studio 中右键单击 Voting 项目并选择“打包”。 In Visual Studio, right-click the Voting project, and then select Package.


  2. 转到 .\service-fabric-dotnet-quickstart\Voting\pkg\Debug 目录。Go to the .\service-fabric-dotnet-quickstart\Voting\pkg\Debug directory. 将内容压缩到名为 的文件中。Zip the contents into a file called Applicationmanifest.xml 文件应位于 zip 文件中的根位置。The ApplicationManifest.xml file should be at the root in the zip file.

    Zip 应用程序

  3. 重命名该文件,以将扩展名从 .zip 更改为 .sfpkg。Rename the file to change the extension from .zip to .sfpkg.

  4. 在 Azure 门户上你的存储帐户的 apps 容器中,选择“上传”,然后上传 Voting.sfpkg。In the Azure portal, in the apps container for your storage account, select Upload, and then upload Voting.sfpkg.


应用程序现已暂存,你可以创建资源管理器模板来部署应用程序了。Now, the application is now staged and you can create the Resource Manager template to deploy the application.

创建 Resource Manager 模板Create the Resource Manager template

示例应用程序包含可用于部署该应用程序的 Azure 资源管理器模板The sample application contains Azure Resource Manager templates you can use to deploy the application. 模板文件名为 UserApp.json 和 UserApp.Parameters.json。The template file names are UserApp.json and UserApp.Parameters.json.


必须使用群集名称更新 UserApp.Parameters.json 文件。The UserApp.Parameters.json file must be updated with the name of your cluster.

参数Parameter 说明Description 示例Example 注释Comments
clusterNameclusterName 要部署到的群集的名称The name of the cluster you're deploying to sf-cluster123sf-cluster123
applicationapplication 应用程序的名称The name of the application VotingVoting
applicationTypeNameapplicationTypeName 应用程序的类型名称The type name of the application VotingTypeVotingType 必须匹配 ApplicationManifest.xmlMust match ApplicationManifest.xml
applicationTypeVersionapplicationTypeVersion 应用程序类型的版本The version of the application type 必须匹配 ApplicationManifest.xmlMust match ApplicationManifest.xml
serviceNameserviceName 服务的名称The name of the service Voting~VotingWebVoting~VotingWeb 必须采用 ApplicationName~ServiceType 格式Must be in the format ApplicationName~ServiceType
serviceTypeNameserviceTypeName 服务的类型名称The type name of the service VotingWebVotingWeb 必须匹配 ServiceManifest.xmlMust match ServiceManifest.xml
appPackageUrlappPackageUrl 应用程序的 Blob 存储 URLThe blob storage URL of the application Blob 存储中应用程序包的 URL(设置该 URL 的过程在本文中的后面部分介绍)The URL of the application package in blob storage (the procedure to set the URL is described later in the article)
    "apiVersion": "2019-03-01",
    "type": "Microsoft.ServiceFabric/clusters/applications",
    "name": "[concat(parameters('clusterName'), '/', parameters('applicationName'))]",
    "location": "[variables('clusterLocation')]",
    "apiVersion": "2019-03-01",
    "type": "Microsoft.ServiceFabric/clusters/applicationTypes",
    "name": "[concat(parameters('clusterName'), '/', parameters('applicationTypeName'))]",
    "location": "[variables('clusterLocation')]",
    "apiVersion": "2019-03-01",
    "type": "Microsoft.ServiceFabric/clusters/applicationTypes/versions",
    "name": "[concat(parameters('clusterName'), '/', parameters('applicationTypeName'), '/', parameters('applicationTypeVersion'))]",
    "location": "[variables('clusterLocation')]",
    "apiVersion": "2019-03-01",
    "type": "Microsoft.ServiceFabric/clusters/applications/services",
    "name": "[concat(parameters('clusterName'), '/', parameters('applicationName'), '/', parameters('serviceName'))]",
    "location": "[variables('clusterLocation')]"

部署应用程序Deploy the application

运行 New-AzResourceGroupDeployment cmdlet,以将应用程序部署到包含群集的资源组:Run the New-AzResourceGroupDeployment cmdlet to deploy the application to the resource group that contains your cluster:

New-AzResourceGroupDeployment -ResourceGroupName "sf-cluster-rg" -TemplateParameterFile ".\UserApp.Parameters.json" -TemplateFile ".\UserApp.json" -Verbose

使用资源管理器升级 Service Fabric 应用程序Upgrade the Service Fabric application by using Resource Manager

你可能会出于以下某一原因升级已部署到 Service Fabric 群集的应用程序:You might upgrade an application that's already deployed to a Service Fabric cluster for one of these reasons:

  • 一个新服务将添加到应用程序。A new service is added to the application. 当服务添加到应用程序时,一个服务定义必须添加到 service-manifest 和 application-manifest 文件中。A service definition must be added to service-manifest.xml and application-manifest.xml files when a service is added to the application. 若要反映应用程序的新版本,还必须在 UserApp.parameters.json 中将应用程序类型版本从 1.0.0 更改为 1.0.1:To reflect a new version of an application, you also must change the application type version from 1.0.0 to 1.0.1 in UserApp.Parameters.json:

    "applicationTypeVersion": {
        "value": "1.0.1"
    "serviceName2": {
        "value": "Voting~VotingData"
    "serviceTypeName2": {
        "value": "VotingDataType"
  • 现有服务的新版本将添加到应用程序中。A new version of an existing service is added to the application. 例如,对应用程序代码的更改,以及对应用类型版本和名称的更新。Examples include application code changes and updates to app type version and name. 对于此升级,请更新 UserApp.Parameters.json,如下所示:For this upgrade, update UserApp.Parameters.json like this:

     "applicationTypeVersion": {
        "value": "1.0.1"

删除应用程序资源Delete application resources

若要删除使用资源管理器中的应用程序资源模型部署的应用程序,请执行以下操作:To delete an application that was deployed by using the application resource model in Resource Manager:

  1. 使用 Get-AzResource cmdlet 获取应用程序的资源 ID:Use the Get-AzResource cmdlet to get the resource ID for the application:

    Get-AzResource  -Name <String> | f1
  2. 使用 Remove-AzResource cmdlet 删除应用程序资源:Use the Remove-AzResource cmdlet to delete the application resources:

    Remove-AzResource  -ResourceId <String> [-Force] [-ApiVersion <String>]

后续步骤Next steps

获取有关应用程序资源模型的信息:Get information about the application resource model: