Azure Service Fabric - 使用 Powershell 配置服务实例参数
Azure Service Fabric - Configure service instance parameters using Powershell
是否可以通过 Settings.xml 文件或其他方式在 运行 时将参数注入来宾可执行文件?我有一个 GuestExecutable,我需要将一些配置传递给它 - 在服务创建时 URL。
我需要两个服务实例 运行 不同的参数,服务实例信息必须根据我需要传递的自定义参数而有所不同。这是否可以使用 Powershell,或者我是否需要对配置进行版本控制并创建新版本?
提前致谢
您是否尝试过以下命令:New-ServiceFabricApplication
?
当您创建应用程序清单文件时,参数将包含您在注册新应用程序时设置的可替换参数。
像这样的 ApplicationManifest.xml 示例:
<?xml version="1.0" encoding="utf-8"?>
<ApplicationManifest ApplicationTypeName="MyAppTypeName" ApplicationTypeVersion="1.0.0" xmlns=...>
<Parameters>
<Parameter Name="Web1_InstanceCount" Value="-1" />
<Parameter Name="ENVIRONMENT_NAME" Value="DEV" />
<Parameter Name="FEPlacementConstraints" Value="NodeTypeName==FrontEnd" />
</Parameters>
<ServiceManifestImport>
<ServiceManifestRef ServiceManifestName="MyServicePkg" ServiceManifestVersion="1.0.0" />
<ConfigOverrides />
<EnvironmentOverrides />
</ServiceManifestImport>
<DefaultServices>
<Service Name="Web1">
<StatelessService ServiceTypeName="MyServiceType" InstanceCount="[Web1_InstanceCount]">
<SingletonPartition />
<PlacementConstraints>[FEPlacementConstraints]</PlacementConstraints>
</StatelessService>
</Service>
</DefaultServices>
</ApplicationManifest>
您可以像这样使用配置覆盖更改设置文件:
<ConfigOverrides>
<ConfigOverride Name="Config">
<Settings>
<Section Name="MyConfigSection">
<Parameter Name="MySetting" Value="[ENVIRONMENT_NAME]"/>
</Section>
</Settings>
</ConfigOverride>
</ConfigOverrides>
或者您可以像这样在您的服务上设置环境变量:
<EnvironmentOverrides CodePackageRef="Code">
<EnvironmentVariable Name="ASPNETCORE_ENVIRONMENT" Value="[ENVIRONMENT_NAME]" />
</EnvironmentOverrides>
In my opinion the environment overrides would work better on your case,
because most guest executable are not flexible on how you can configure them but generally they accept environment variables.
那么你:
- 上传您的申请:
Copy-ServiceFabricApplicationPackage
- 使用
Register-ServiceFabricApplicationType
注册您的应用程序
- 然后您使用所需的设置注册每个新应用:
。
New-ServiceFabricApplication -ApplicationName fabric:/myapp/todolist-dev -ApplicationTypeName "MyAppTypeName" -ApplicationTypeVersion "1.0.0" -ApplicationParameter @{Web1_InstanceCount='-1'; ENVIRONMENT_NAME='DEV'}
New-ServiceFabricApplication -ApplicationName fabric:/myapp/todolist-uat -ApplicationTypeName "MyAppTypeName" -ApplicationTypeVersion "1.0.0" -ApplicationParameter @{Web1_InstanceCount='-1'; ENVIRONMENT_NAME='UAT'}
这种方法的唯一缺点是您最终会得到两个应用程序,但您认为这对您来说不是问题,它们的管理方式与您对单个应用程序的管理方式大致相同.
如果您确实需要 运行 在同一个应用程序中同时使用两者,您可以采取一些解决方法:
使用多个配置包(不认为与来宾可执行文件一起工作很好,但与可靠服务一起工作很好
使用启动脚本,您可以在其中添加将参数传递给启动 exe 的逻辑,如下所示:
在你的ServiceManifest.xml中:
<CodePackage Name="Code" Version="1.0.0">
<EntryPoint>
<ExeHost>
<Program>start.bat</Program>
<WorkingFolder>CodePackage</WorkingFolder>
</ExeHost>
</EntryPoint>
</CodePackage>
在您的 .exe 的同一文件夹(代码)上,您创建了包含以下内容的 start.bat 文件:
myApp.exe %EnvironmentVariableNameSetBySF%
是否可以通过 Settings.xml 文件或其他方式在 运行 时将参数注入来宾可执行文件?我有一个 GuestExecutable,我需要将一些配置传递给它 - 在服务创建时 URL。
我需要两个服务实例 运行 不同的参数,服务实例信息必须根据我需要传递的自定义参数而有所不同。这是否可以使用 Powershell,或者我是否需要对配置进行版本控制并创建新版本?
提前致谢
您是否尝试过以下命令:New-ServiceFabricApplication
?
当您创建应用程序清单文件时,参数将包含您在注册新应用程序时设置的可替换参数。
像这样的 ApplicationManifest.xml 示例:
<?xml version="1.0" encoding="utf-8"?>
<ApplicationManifest ApplicationTypeName="MyAppTypeName" ApplicationTypeVersion="1.0.0" xmlns=...>
<Parameters>
<Parameter Name="Web1_InstanceCount" Value="-1" />
<Parameter Name="ENVIRONMENT_NAME" Value="DEV" />
<Parameter Name="FEPlacementConstraints" Value="NodeTypeName==FrontEnd" />
</Parameters>
<ServiceManifestImport>
<ServiceManifestRef ServiceManifestName="MyServicePkg" ServiceManifestVersion="1.0.0" />
<ConfigOverrides />
<EnvironmentOverrides />
</ServiceManifestImport>
<DefaultServices>
<Service Name="Web1">
<StatelessService ServiceTypeName="MyServiceType" InstanceCount="[Web1_InstanceCount]">
<SingletonPartition />
<PlacementConstraints>[FEPlacementConstraints]</PlacementConstraints>
</StatelessService>
</Service>
</DefaultServices>
</ApplicationManifest>
您可以像这样使用配置覆盖更改设置文件:
<ConfigOverrides>
<ConfigOverride Name="Config">
<Settings>
<Section Name="MyConfigSection">
<Parameter Name="MySetting" Value="[ENVIRONMENT_NAME]"/>
</Section>
</Settings>
</ConfigOverride>
</ConfigOverrides>
或者您可以像这样在您的服务上设置环境变量:
<EnvironmentOverrides CodePackageRef="Code">
<EnvironmentVariable Name="ASPNETCORE_ENVIRONMENT" Value="[ENVIRONMENT_NAME]" />
</EnvironmentOverrides>
In my opinion the environment overrides would work better on your case, because most guest executable are not flexible on how you can configure them but generally they accept environment variables.
那么你:
- 上传您的申请:
Copy-ServiceFabricApplicationPackage
- 使用
Register-ServiceFabricApplicationType
注册您的应用程序
- 然后您使用所需的设置注册每个新应用:
。
New-ServiceFabricApplication -ApplicationName fabric:/myapp/todolist-dev -ApplicationTypeName "MyAppTypeName" -ApplicationTypeVersion "1.0.0" -ApplicationParameter @{Web1_InstanceCount='-1'; ENVIRONMENT_NAME='DEV'}
New-ServiceFabricApplication -ApplicationName fabric:/myapp/todolist-uat -ApplicationTypeName "MyAppTypeName" -ApplicationTypeVersion "1.0.0" -ApplicationParameter @{Web1_InstanceCount='-1'; ENVIRONMENT_NAME='UAT'}
这种方法的唯一缺点是您最终会得到两个应用程序,但您认为这对您来说不是问题,它们的管理方式与您对单个应用程序的管理方式大致相同.
如果您确实需要 运行 在同一个应用程序中同时使用两者,您可以采取一些解决方法:
使用多个配置包(不认为与来宾可执行文件一起工作很好,但与可靠服务一起工作很好
使用启动脚本,您可以在其中添加将参数传递给启动 exe 的逻辑,如下所示:
在你的ServiceManifest.xml中:
<CodePackage Name="Code" Version="1.0.0">
<EntryPoint>
<ExeHost>
<Program>start.bat</Program>
<WorkingFolder>CodePackage</WorkingFolder>
</ExeHost>
</EntryPoint>
</CodePackage>
在您的 .exe 的同一文件夹(代码)上,您创建了包含以下内容的 start.bat 文件:
myApp.exe %EnvironmentVariableNameSetBySF%