功能管理概述Feature management overview

一直以来,交付新的应用程序功能都需要完全重新部署该应用程序本身。Traditionally, shipping a new application feature requires a complete redeployment of the application itself. 测试功能通常需要应用程序的多个部署。Testing a feature often requires multiple deployments of the application. 每个部署可能会更改该功能或将该功能公开给不同的客户进行测试。Each deployment may change the feature or expose the feature to different customers for testing.

功能管理是一种新式软件开发实践,它将功能发布与代码部署分离开来,可以按需快速更改功能的可用性。Feature management is a modern software-development practice that decouples feature release from code deployment and enables quick changes to feature availability on demand. 它使用一种称作“功能标志”(也称为“功能切换”、“功能开关”等)的技术来动态管理功能的生命周期。It uses a technique called feature flags (also known as feature toggles, feature switches, and so on) to dynamically administer a feature's lifecycle.

功能管理可以帮助开发人员处理以下问题:Feature management helps developers address the following problems:

  • 代码分支管理:使用功能标志来包装当前正在开发中的新应用程序功能。Code branch management: Use feature flags to wrap new application functionality currently under development. 此类功能默认是“隐藏的”。Such functionality is "hidden" by default. 不过,可以在此功能尚未完成的情况下交付它,它可以在生产环境中保持主导地位。You can safely ship the feature, even though it's unfinished, and it will stay dormant in production. 使用这种称作“暗处部署”的方法,可以在每个开发周期结束时发布所有代码。Using this approach, called dark deployment, you can release all your code at the end of each development cycle. 你不再会因为给定功能需要多个周期才能完成而需要在多个开发周期中维护代码分支。You no longer need to maintain code branches across multiple development cycles because a given feature requires more than one cycle to complete.
  • 在生产环境中测试:在生产环境中,使用功能标志提前授予对新功能的访问权限。Test in production: Use feature flags to grant early access to new functionality in production. 例如,可将访问权限限制为团队成员或内部 Beta 测试人员。For example, you can limit access to team members or to internal beta testers. 这些用户将获得完全保真的生产体验,而无需在测试环境中使用模拟或部分体验。These users will experience the full-fidelity production experience instead of a simulated or partial experience in a test environment.
  • 外部测试:使用功能标志以增量方式将新功能推出给最终用户。Flighting: Use feature flags to incrementally roll out new functionality to end users. 一开始可以面向少量的用户,然后逐渐提高目标用户百分比。You can target a small percentage of your user population first and increase that percentage gradually over time.
  • 即时终止开关:功能标志提供一个固有的安全网络用于发布新功能。Instant kill switch: Feature flags provide an inherent safety net for releasing new functionality. 可以打开和关闭应用程序功能,而无需重新部署任何代码。You can turn application features on and off without redeploying any code. 可根据需要快速禁用某项功能,而无需重新生成再重新部署应用程序。If necessary, you can quickly disable a feature without rebuilding and redeploying your application.
  • 选择性激活:使用功能标志划分用户,并将特定的一组功能交付给每个组用户。Selective activation: Use feature flags to segment your users and deliver a specific set of features to each group. 可能有某项功能只能在特定的 Web 浏览器中运行。You may have a feature that works only on a certain web browser. 可以定义一个功能标志,以便只有运行该浏览器的用户能够查看和使用该功能。You can define a feature flag so that only users of that browser can see and use the feature. 使用此方法可以轻松扩展支持的浏览器列表,而无需进行任何代码更改。With this approach, you can easily expand the supported browser list later without having to make any code changes.

基本概念Basic concepts

下面是与功能管理相关的几个新术语:Here are several new terms related to feature management:

  • 功能标志:功能标志是具有 onoff 二进制状态的变量。Feature flag: A feature flag is a variable with a binary state of on or off. 功能标志还具有关联的代码块。The feature flag also has an associated code block. 无论代码块是否运行,都会触发功能标志的状态。The feature flag's state triggers whether the code block runs.
  • 功能管理器:功能管理器是一个应用程序包,可以处理应用程序中所有功能标志的生命周期。Feature manager: A feature manager is an application package that handles the life cycle of all the feature flags in an application. 功能管理器还提供附加的功能,包括缓存功能标志和更新标志状态。The feature manager also provides additional functionality, including caching feature flags and updating their states.
  • 筛选器:筛选器是用于评估功能标志状态的规则。Filter: A filter is a rule for evaluating the state of a feature flag. 潜在筛选器包括用户组、设备或浏览器类型、地理位置和时间窗口。Potential filters include user groups, device or browser types, geographic locations, and time windows.

功能管理的有效实施方案至少包括两个协同工作的组件:An effective implementation of feature management consists of at least two components working in concert:

  • 利用功能标志的应用程序。An application that makes use of feature flags.
  • 用于存储功能标志及其当前状态的独立存储库。A separate repository that stores the feature flags and their current states.

在代码中使用功能标志Using feature flags in your code

在应用程序中实施功能标志的基本模式很简单。The basic pattern for implementing feature flags in an application is simple. 功能标志是控制代码中条件语句的布尔值状态变量:A feature flag is a Boolean state variable controlling a conditional statement in your code:

if (featureFlag) {
    // Run the following code
}

可以静态设置 featureFlag 的值。You can set the value of featureFlag statically.

bool featureFlag = true;

可以根据特定的规则评估标志的状态:You can evaluate the flag's state based on certain rules:

bool featureFlag = isBetaUser();

可以扩展条件以设置任一状态的应用程序行为:You can extend the conditional to set application behavior for either state:

if (featureFlag) {
    // This following code will run if the featureFlag value is true
} else {
    // This following code will run if the featureFlag value is false
}

功能标志存储库Feature flag repository

若要有效使用功能标志,需要外部化应用程序中使用的所有功能标志。To use feature flags effectively, you need to externalize all the feature flags used in an application. 这样,你就可以更改功能标志状态,而无需修改再重新部署应用程序本身。This allows you to change feature flag states without modifying and redeploying the application itself.

Azure 应用程序配置提供功能标志的集中存储库。Azure App Configuration provides a centralized repository for feature flags. 可以使用它来定义不同类型的功能标志,并快速且有把握地操作其状态。You can use it to define different kinds of feature flags and manipulate their states quickly and confidently. 然后,可以使用适用于各种编程语言框架的应用程序配置库,轻松从应用程序访问这些功能标志。You can then use the App Configuration libraries for various programming language frameworks to easily access these feature flags from your application.

在 ASP.NET Core 应用中使用功能标志介绍了如何结合使用 .NET Core 应用程序配置提供程序和功能管理库,针对 ASP.NET Web 应用程序实施功能标志。Use feature flags in an ASP.NET Core app shows how the .NET Core App Configuration provider and Feature Management libraries are used together to implement feature flags for your ASP.NET web application.

后续步骤Next steps