使用功能筛选器启用条件功能标志Use feature filters to enable conditional feature flags

使用功能标志可以在应用程序中激活或停用功能。Feature flags allow you to activate or deactivate functionality in your application. 简单的功能标志为“开”或“关”。A simple feature flag is either on or off. 应用程序会始终采用相同的行为方式。The application always behaves the same way. 例如,你可以在一个功能标志后面推出新功能。For example, you could roll out a new feature behind a feature flag. 启用该功能标志后,所有用户都将看到新功能。When the feature flag is enabled, all users see the new feature. 禁用功能标志将隐藏新功能。Disabling the feature flag hides the new feature.

相比之下,使用“条件功能标志”可以动态启用或禁用功能标志。In contrast, a conditional feature flag allows the feature flag to be enabled or disabled dynamically. 应用程序的行为可能会有所不同,具体取决于功能标志条件。The application may behave differently, depending on the feature flag criteria. 假设要首先向小部分用户显示新功能。Suppose you want to show your new feature to a small subset of users at first. 使用条件功能标志可以为某些用户启用功能标志,同时为其他用户禁用该标志。A conditional feature flag allows you to enable the feature flag for some users while disabling it for others. 功能筛选器在每次计算出其结果时确定功能标志的状态。Feature filters determine the state of the feature flag each time it's evaluated.

Microsoft.FeatureManagement 库包括三个功能筛选器:The Microsoft.FeatureManagement library includes three feature filters:

  • PercentageFilter 基于百分比启用功能标志。PercentageFilter enables the feature flag based on a percentage.
  • TimeWindowFilter 在指定时间段内启用功能标志。TimeWindowFilter enables the feature flag during a specified window of time.
  • TargetingFilter 为指定的用户和组启用功能标志。TargetingFilter enables the feature flag for specified users and groups.

还可以创建自己的功能筛选器来实现 Microsoft.FeatureManagement.IFeatureFilter 接口You can also create your own feature filter that implements the Microsoft.FeatureManagement.IFeatureFilter interface.

注册功能筛选器Registering a feature filter

可以通过调用 AddFeatureFilter 方法并指定功能筛选器名称来注册功能筛选器。You register a feature filter by calling the AddFeatureFilter method, specifying the name of the feature filter. 例如,以下代码将注册 PercentageFilterFor example, the following code registers PercentageFilter:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();
    services.AddFeatureManagement().AddFeatureFilter<PercentageFilter>();
}

在 Azure 应用配置中配置功能筛选器Configuring a feature filter in Azure App Configuration

某些功能筛选器具有其他设置。Some feature filters have additional settings. 例如,PercentageFilter 基于百分比激活某个功能。For example, PercentageFilter activates a feature based on a percentage. 它有一个设置用于定义要使用的百分比。It has a setting defining the percentage to use.

可以为 Azure 应用配置中定义的功能标志配置这些设置。You can configure these settings for feature flags defined in Azure App Configuration. 例如,执行以下步骤可使用 PercentageFilter 为针对某个 Web 应用的 50% 请求启用功能标志:For example, follow these steps to use PercentageFilter to enable the feature flag for 50% of requests to a web app:

  1. 遵循快速入门:将功能标志添加到 ASP.NET Core 应用中的说明,以创建具有功能标志的 Web 应用。Follow the instructions in Quickstart: Add feature flags to an ASP.NET Core app to create a web app with a feature flag.

  2. 在 Azure 门户中,转到配置存储并单击“功能管理器”。In the Azure portal, go to your configuration store and click Feature manager.

  3. 单击在快速入门中创建的 Beta 功能标志的上下文菜单。Click on the context menu for the Beta feature flag that you created in the quickstart. 单击 “编辑”Click Edit.

    编辑 Beta 功能标志Edit Beta feature flag

  4. 在“编辑”屏幕上,选择“开”单选按钮(如果它尚未处于选中状态)。In the Edit screen, select the On radio button if it isn't already selected. 然后单击“添加筛选器”按钮。Then click the Add Filter button. (“开”单选按钮的标签将更改为显示“条件”。) (The On radio button's label will change to read Conditional.)

  5. 在“键”字段中,输入“Microsoft.Percentage”。In the Key field, enter Microsoft.Percentage.

    添加功能筛选器Add feature filter

  6. 单击功能筛选键旁边的上下文菜单。Click the context menu next to the feature filter key. 单击“编辑参数”。Click Edit Parameters.

    编辑功能筛选器参数Edit feature filter parameters

  7. 将鼠标悬停在“名称”标题上,使文本框显示在网格中。Hover under the Name header so that text boxes appear in the grid. 输入“值”作为“名称”,并输入“50”作为“值”。Enter a Name of Value and a Value of 50. “值”字段指示要启用功能筛选器的请求的百分比。The Value field indicates the percentage of requests for which to enable the feature filter.

    设置功能筛选器参数Set feature filter parameters

  8. 单击“应用”以返回到“编辑功能标志”屏幕。Click Apply to return to the Edit feature flag screen. 然后再次单击“应用”以保存功能标志设置。Then click Apply again to save the feature flag settings.

  9. 功能标志的状态现在显示为“条件”。The State of the feature flag now appears as Conditional. 此状态指示,功能标志将根据功能筛选器强制实施的条件,基于每个请求而被启用或禁用。This state indicates that the feature flag will be enabled or disabled on a per-request basis, based on the criteria enforced by the feature filter.

    条件功能标志Conditional feature flag

正在起作用的功能筛选器Feature filters in action

若要查看此功能标志的效果,请在浏览器中启动该应用程序,并多次点击“刷新”按钮。To see the effects of this feature flag, launch the application and hit the Refresh button in your browser multiple times. 你会看到 Beta 项在大约 50% 的时间中出现在工具栏上。You'll see that the Beta item appears on the toolbar about 50% of the time. 它在其余时间隐藏,因为 PercentageFilter 会为一部分请求停用 Beta 功能。It's hidden the rest of the time, because the PercentageFilter deactivates the Beta feature for a subset of requests. 以下视频显示了正在起作用的这一行为。The following video shows this behavior in action.

TargetingFilter 的实际运用TargetingFilter in action

后续步骤Next steps