簡介 Service Fabric 應用程式中的 StartupServices.xml
這項功能會在 Service Fabric 應用程式設計中引進 StartupServices.xml 檔案。 此檔案裝載 ApplicationManifest.xml 的 DefaultServices 區段。 透過此實作,DefaultServices 和服務定義相關參數會從現有的 ApplicationManifest.xml 移至名為 StartupServices.xml 的新檔案。 此檔案會用於 Visual Studio 中的每個功能 (組建/重建/F5/Ctrl+F5/發佈)。
StartupServices.xml僅適用於Visual Studio部署。 這種安排是確保使用 Visual Studio 部署的套件(StartupServices.xml)與 ARM 部署的服務沒有衝突。
StartupServices.xml未封裝為應用程式封裝的一部分。 DevOps 管線不支援它,客戶應該透過ARM或透過具有所需設定的 Cmdlet,在應用程式指令清單中部署個別服務。
現有的 Service Fabric 應用程式設計
對於每個 Service Fabric 應用程式,ApplicationManifest.xml 都是應用程式的所有服務相關資訊的來源。 ApplicationManifest.xml 中包含所有的 Parameters、ServiceManifestImport 和 DefaultServices。 設定參數說明於 ApplicationParameters 下的 Cloud.xml/Local1Node.xml/Local5Node.xml 檔案中。
在應用程式中新增新的服務時,會在ApplicationManifest.xml內新增新的服務參數、ServiceManifestImport 和 DefaultServices 區段。 設定參數會新增至 ApplicationParameters 下的 Cloud.xml/Local1Node.xml/Local5Node.xml 檔案中。
當使用者在 Visual Studio 中選取 [建置/重建] 函式時,會在 ApplicationManifest.xml 中修改 ServiceManifestImport、Parameters 和 DefaultServices 區段。 此外也會在 ApplicationParameters 下的 Cloud.xml/Local1Node.xml/Local5Node.xml 檔案中編輯設定參數。
當使用者觸發 F5/Ctrl+F5/發佈時,將會根據 ApplictionManifest.xml 中的資訊來部署或發佈應用程式和服務。 此時會使用 ApplicationParameters 下的任何 Cloud.xml/Local1Node.xml/Local5Node.xml 檔案中的設定參數。
範例 ApplicationManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<ApplicationManifest ApplicationTypeName="SampleAppType"
ApplicationTypeVersion="1.0.0"
xmlns="http://schemas.microsoft.com/2011/01/fabric"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Parameters>
<Parameter Name="Web1_ASPNETCORE_ENVIRONMENT" DefaultValue="" />
<Parameter Name="Web1_MinReplicaSetSize" DefaultValue="3" />
<Parameter Name="Web1_PartitionCount" DefaultValue="1" />
<Parameter Name="Web1_TargetReplicaSetSize" DefaultValue="3" />
</Parameters>
<!-- Import the ServiceManifest from the ServicePackage. The ServiceManifestName and ServiceManifestVersion
should match the Name and Version attributes of the ServiceManifest element defined in the
ServiceManifest.xml file. -->
<ServiceManifestImport>
<ServiceManifestRef ServiceManifestName="Web1Pkg" ServiceManifestVersion="1.0.0" />
<ConfigOverrides />
<EnvironmentOverrides CodePackageRef="code">
<EnvironmentVariable Name="ASPNETCORE_ENVIRONMENT" Value="[Web1_ASPNETCORE_ENVIRONMENT]" />
</EnvironmentOverrides>
</ServiceManifestImport>
<DefaultServices>
<!-- The section below creates instances of service types, when an instance of this
application type is created. You can also create one or more instances of service type using the
ServiceFabric PowerShell module.
The attribute ServiceTypeName below must match the name defined in the imported ServiceManifest.xml file. -->
<Service Name="Web1" ServicePackageActivationMode="ExclusiveProcess">
<StatefulService ServiceTypeName="Web1Type" TargetReplicaSetSize="[Web1_TargetReplicaSetSize]" MinReplicaSetSize="[Web1_MinReplicaSetSize]">
<UniformInt64Partition PartitionCount="[Web1_PartitionCount]" LowKey="-9223372036854775808" HighKey="9223372036854775807" />
</StatefulService>
</Service>
</DefaultServices>
</ApplicationManifest>
使用 StartupServices.xml 的新 Service Fabric 應用程式設計
在此設計中,服務等級資訊(例如服務定義和服務參數)和應用層級資訊(ServiceManifestImport 和 ApplicationParameters)之間有明確的區別。 StartupServices.xml 包含所有服務層級資訊,而 ApplicationManifest.xml 則包含所有應用層級資訊。 導入的另一項變更是在 StartupServiceParameters 下新增了 Cloud.xml/Local1Node.xml/Local5Node.xml,其中僅包含服務參數的設定。 ApplicationParameters 下的現有 Cloud.xml/Local1Node.xml/Local5Node.xml 僅包含應用層級參數設定。
在應用程式中新增服務時,會在 ApplicationManifest.xml 中新增應用層級參數和 ServiceManifestImport。 應用程式參數的設定會新增至 ApplicationParameters 下的 Cloud.xml/Local1Node.xml/Local5Node.xml 檔案中。 服務資訊和服務參數會新增至 StartupServices.xml,而服務參數的設定則會新增至 StartupServiceParameters 下的 Cloud.xml/Local1Node.xml/Local5Node.xml 中。
在建置/重建專案期間,會在 ApplicationManifest.xml 中進行ServiceManifestImport 和應用程式參數的修改。 此外也會在 ApplicationParameters 下的 Cloud.xml/Local1Node.xml/Local5Node.xml 檔案中編輯應用程式參數的設定。 服務相關資訊會在 StartupServices.xml 中編輯,而服務參數則會在 StartupServiceParameters 下的 Cloud.xml/Local1Node.xml/Local5Node.xml 中編輯。
在 Visual Studio 中觸發 F5/Ctrl+F5/發佈時,將會根據 ApplictionManifest.xml 中的資訊,以及 ApplicationParameters 下任何 Cloud.xml/Local1Node.xml/Local5Node.xml 檔案中的應用程式參數,來部署/發佈應用程式。 每項服務都會使用 StartupServices.xml 中的服務資訊,以及 StartupServiceParameters 下任何 Cloud.xml/Local1Node.xml/Local5Node.xml 檔案中的服務參數設定來個別啟動。
您可以在發佈應用程式之前編輯這些服務參數和應用程式參數 (按一下滑鼠右鍵 -> 發佈),如圖所示。
新設計中的範例 ApplicationManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<ApplicationManifest ApplicationTypeName="SampleAppType"
ApplicationTypeVersion="1.0.0"
xmlns="http://schemas.microsoft.com/2011/01/fabric"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Parameters>
<Parameter Name="Web1_ASPNETCORE_ENVIRONMENT" DefaultValue="" />
</Parameters>
<!-- Import the ServiceManifest from the ServicePackage. The ServiceManifestName and ServiceManifestVersion
should match the Name and Version attributes of the ServiceManifest element defined in the
ServiceManifest.xml file. -->
<ServiceManifestImport>
<ServiceManifestRef ServiceManifestName="Web1Pkg" ServiceManifestVersion="1.0.0" />
<ConfigOverrides />
<EnvironmentOverrides CodePackageRef="code">
<EnvironmentVariable Name="ASPNETCORE_ENVIRONMENT" Value="[Web1_ASPNETCORE_ENVIRONMENT]" />
</EnvironmentOverrides>
</ServiceManifestImport>
</ApplicationManifest>
範例 StartupServices.xml 檔案
<?xml version="1.0" encoding="utf-8"?>
<StartupServicesManifest xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.microsoft.com/2011/01/fabric">
<Parameters>
<Parameter Name="Web1_InstanceCount" DefaultValue="-1" />
</Parameters>
<Services>
<!-- The section below creates instances of service types, when an instance of this
application type is created. You can also create one or more instances of service type using the
ServiceFabric PowerShell module.
The attribute ServiceTypeName below must match the name defined in the imported ServiceManifest.xml file. -->
<Service Name="Web1" ServicePackageActivationMode="ExclusiveProcess">
<StatelessService ServiceTypeName="Web1Type" InstanceCount="[Web1_InstanceCount]">
<SingletonPartition />
</StatelessService>
</Service>
</Services>
</StartupServicesManifest>
SF SDK 5.0.516.9590 版和更新版本中的所有新專案都會啟用 startupServices.xml 功能。 使用舊版 SDK 建立的專案與最新的 SDK 完全回溯相容。 不支援將舊專案移轉至新設計。 如果使用者想要在較新版本的 SDK 中建立不含StartupServices.xml的 Service Fabric 應用程式,則使用者應該選取 [協助我選擇專案範本] 連結,如下圖所示。