管理多个环境的应用程序Manage applications for multiple environments

借助 Azure Service Fabric 群集,可以在任何位置,使用任意数量的计算机(从一台到数千台)来创建群集。Azure Service Fabric clusters enable you to create clusters using anywhere from one to many thousands machines. 大多数情况下,必须跨多个群集配置(本地开发群集、共享开发群集和生产群集)来部署应用程序。In most cases, you find yourself having to deploy your application across multiple cluster configurations: your local development cluster, a shared development cluster and your production cluster. 所有这些群集被视为运行代码的不同环境。All of these clusters are considered different environments your code has to run in. 尽管无需修改,应用程序二进制文件也可在这些不同环境中运行,但用户通常希望以不同方式配置应用程序。Application binaries can run without modification across this wide spectrum, but you often want to configure the application differently.

请参见以下两个简单的示例:Consider two simple examples:

  • 你的服务在已定义的端口上侦听,但是你需要该端口在各个环境中有所不同your service listens on a defined port, but you need that port to be different across the environments
  • 你需要在各个环境中为数据库提供不同的绑定凭据you need to provide different binding credentials for a database across the environments

指定配置Specifying configuration

提供的配置可分为两个类别:The configuration you provide can be divided in two categories:

  • 应用于服务运行方式的配置Configuration that applies to how your services are run

    • 例如,终结点的端口号或服务的实例数量For example, the port number for an endpoint or the number of instances of a service
    • 此配置是在应用程序或服务清单文件中指定的This configuration is specified in the application or service manifest file
  • 应用于应用程序代码的配置Configuration that applies to your application code

    • 例如,绑定数据库的信息For example, binding information for a database
    • 此配置可通过配置文件或环境变量来提供This configuration can be provided either through configuration files or environment variables

    Note

    并非应用程序和服务清单文件中的所有属性都支持参数。Not all attributes in the application and service manifest file support parameters. 在这些情况下,用户必须依赖于将字符串替换为部署工作流的一部分。In those cases, you have to rely on substituting strings as part of your deployment workflow. 在 Azure DevOps 中,可以使用类似于替换令牌: https://marketplace.visualstudio.com/items?itemName=qetza.replacetokens 的扩展,或者在 Jenkins 中通过运行脚本任务来替换值。In Azure DevOps you can use an extension like Replace Tokens: https://marketplace.visualstudio.com/items?itemName=qetza.replacetokens or in Jenkins you could run a script task to replace the values.

在应用程序创建期间指定参数Specifying parameters during application creation

在 Service Fabric 中创建命名的应用程序实例时,可以选择传入参数。When creating a named application instances in Service Fabric, you have the option to pass in parameters. 执行此操作的方式取决于创建应用程序实例的方式。The way you do it depends on how you create the application instance.

  • 在 PowerShell 中,New-ServiceFabricApplication cmdlet 将应用程序参数作为哈希表。In PowerShell, the New-ServiceFabricApplication cmdlet takes the application parameters as a hashtable.
  • 借助 sfctl,sfctl application create 命令将参数作为 JSON 字符串。Using sfctl, The sfctl application create command takes parameters as a JSON string. Install.sh 脚本使用 sfctl。The install.sh script uses sfctl.
  • Visual Studio 在应用程序项目的参数文件夹中提供一组参数文件。Visual Studio provides you with a set of parameter files in the Parameters folder in the application project. 使用 Azure DevOps Services 或 Azure DevOps Server 从 Visual Studio 发布时会使用这些参数文件。These parameter files are used when publishing from Visual Studio, using Azure DevOps Services or Azure DevOps Server. 在 Visual Studio 中,参数文件会传递给 Deploy-FabricApplication.ps1 脚本。In Visual Studio, the parameter files are being passed on to the Deploy-FabricApplication.ps1 script.

后续步骤Next steps

以下文章演示如何使用此处所述的某些概念:The following articles show you how to use some of the concepts described here: