咱們知道,目前大多數應用程序在正式發佈到生產環境以前都會經歷多個不一樣的測試環境,經過讓應用程序在多個不一樣的環境中運行來及時發現並解決問題,避免在線上發生沒必要要的損失。這是對於整個軟件的發佈流程來說。可是若是想讓咱們的應用程序在線上環境中經過知足一些動態條件(好比電商平臺在某一時間段的促銷活動)從而能開啓一些臨時功能的話又該怎麼辦呢?若是你試圖經過從新打包發佈的方式來解決這個問題,可能有些過於大動干戈了。本文,筆者將介紹經過 Feature Flag 的方式來解決這個問題。html
Feature Flag 中文可譯爲 功能開關。經過使用這種方式,能夠對咱們的功能進行條件化配置,當程序在線上環境運行時,若是當前環境符合咱們某一特性功能開啓/關閉的條件時,應用程序會自動開啓/關閉該功能。整個過程不須要人工參與,所有都是由系統自己來完成相應功能的開啓和關閉。ios
那在 .NET Core
中,咱們該如何實現該功能呢?git
微軟爲咱們很貼心地提供了兩個開發包:Microsoft.FeatureManagement 和 Microsoft.FeatureManagement.AspNetCore,該實現是基於 .NET Core 的配置系統 ,因此任何 .NET Core 程序均可以輕易集成該功能。github
目前還處於預覽版階段,須要在 NuGet 上勾選
use prerelease
json
所以,咱們只需將對應包安裝到咱們的應用程序中便可。c#
接下來,咱們就一塊兒看一下如何在 ASP.NET Core 中集成該功能。api
建立一個 ASP.NET Core Web Application 後,安裝以下包:瀏覽器
Install-Package Microsoft.FeatureManagement.AspNetCore -Version 2.0.0-preview-010610001-1263app
接着在 Startup
中的 ConfigureServices
進行相應配置,示例以下:dom
public void ConfigureServices(IServiceCollection services) { services.AddFeatureManagement(); services.AddControllersWithViews(); }
至此,咱們的程序已經支持 Feature Flag 功能了,使用方式就簡單了,這裏展現一個相對簡單的方式。
首先,在 appsettings.json
進行配置,以下所示:
"FeatureManagement": { "NewFeatureFlag": true, }
而後,在 Index.cshtml
經過使用 feature
標籤來進行相應配置,示例以下所示:
@using Microsoft.FeatureManagement @inject IFeatureManager FeatureManager @addTagHelper *,Microsoft.FeatureManagement.AspNetCore @{ ViewData["Title"] = "Home Page"; } <div class="text-center"> <h1 class="display-4">Welcome</h1> <feature name="NewFeatureFlag" requirement="All"> <a asp-action="NewFeature">Go to the new feature.</a> </feature> </div>
此時,咱們運行起程序後就能夠看到 feature
標籤內的內容就能夠渲染出來,若是咱們在配置中將 NewFeatureFlag
值設置爲 False
後,feature
標籤內的內容就會消失,你能夠經過查看網頁源碼的方式來查看具體細節。
接下來筆者介紹一下微軟爲咱們內置的兩個功能開關:
PercentageFilter 是支持百分比的隨機開關,經過使用這種方式,可讓一個功能在每次請求中以一個百分比機率的形式來開啓/關閉。
Startup.cs
public void ConfigureServices(IServiceCollection services) { services.AddFeatureManagement() .AddFeatureFilter<PercentageFilter>(); services.AddControllersWithViews(); }
appsettings.json
"FeatureManagement": { "RandomFlag": { "EnabledFor": [ { "Name": "Percentage", "Parameters": { "Value": 50 } } ] } }
這裏配置的是在每次請求時以 50% 的機率來開啓該功能,其對應的配置類爲:
PercentageFilterSettings
Index.cshtml
@using Microsoft.FeatureManagement @inject IFeatureManager FeatureManager @addTagHelper *,Microsoft.FeatureManagement.AspNetCore @{ ViewData["Title"] = "Home Page"; } <div class="text-center"> <h1 class="display-4">Welcome</h1> <feature name="RandomFlag"> <h2>I am a Random Flag!</h2> </feature> <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p> </div>
這時,當咱們運行起程序後就會看到以下圖所示的效果:
TimeWindowFilter 是時間段的隨機開關,經過使用這種方式,可讓一個功能在指定的時間段內來開啓/關閉。
Startup.cs
public void ConfigureServices(IServiceCollection services) { services.AddFeatureManagement() .AddFeatureFilter<TimeWindowFilter>(); services.AddControllersWithViews(); }
appsettings.json
"FeatureManagement": { "RandomFlag": { "EnabledFor": [ { "Name": "Percentage", "Parameters": { "Start": "2019/12/27 5:04:00 +00:00", "End": "2019/12/27 5:04:05 +00:00" } } ] } }
這裏須要注意的是,配置裏面的
Start
和End
是DateTimeOffset
類型,而且須要配置爲 UTC 的時間,因此在實際使用過程當中須要考慮時區問題(你能夠經過調用DateTimeOffset.UtcNow
的方式來獲取相應時間的格式)。其對應的配置類爲:TimeWindowFilterSettings
Index.cshtml
@using Microsoft.FeatureManagement @inject IFeatureManager FeatureManager @addTagHelper *,Microsoft.FeatureManagement.AspNetCore @{ ViewData["Title"] = "Home Page"; } <div class="text-center"> <h1 class="display-4">Welcome</h1> <feature name="TimedFlag"> <h2>I am a Timed Flag!</h2> </feature> <p>@DateTimeOffset.UtcNow.ToString()</p> <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p> </div>
這時,當咱們運行起程序後就會看到以下圖所示的效果:
最後要介紹的是若是建立和使用自定義的功能開關,筆者這裏作一個這樣的示例,當網站被 Microsoft Edge 瀏覽器訪問時,顯示功能,其他瀏覽器則隱藏功能。
這裏,筆者建立一個配置的映射類 BrowserFilterSettings
和執行過濾的操做類 BrowserFeatureFilter
,示例代碼以下所示:
public class BrowserFilterSettings { public string[] AllowedBrowsers { get; set; } } [FilterAlias("BrowserFilter")] public class BrowserFeatureFilter : IFeatureFilter { private readonly IHttpContextAccessor _httpContextAccessor; public BrowserFeatureFilter(IHttpContextAccessor httpContextAccessor) { _httpContextAccessor = httpContextAccessor; } public Task<bool> EvaluateAsync(FeatureFilterEvaluationContext context) { var userAgent = _httpContextAccessor.HttpContext.Request.Headers["User-Agent"].ToString(); var settings = context.Parameters.Get<BrowserFilterSettings>(); return Task.FromResult(settings.AllowedBrowsers.Any(userAgent.Contains)); } }
接着,進行功能開關的注入,示例代碼以下所示:
public void ConfigureServices(IServiceCollection services) { services.AddHttpContextAccessor(); services.AddFeatureManagement() .AddFeatureFilter<BrowserFeatureFilter>(); services.AddControllersWithViews(); }
而後,進行功能開關的配置,示例配置以下所示:
"FeatureManagement": { "BrowserFlag": { "EnabledFor": [ { "Name": "BrowserFilter", "Parameters": { "AllowedBrowsers": [ "Edge" ] } } ] } }
接着,使用方式以下所示:
@using Microsoft.FeatureManagement @inject IFeatureManager FeatureManager @addTagHelper *,Microsoft.FeatureManagement.AspNetCore @{ ViewData["Title"] = "Home Page"; } <div class="text-center"> <h1 class="display-4">Welcome</h1> <feature name="BrowserFlag"> <h2>I am a Browser Flag only on Edge!</h2> </feature> <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p> </div>
這時,當咱們分別用 Microsoft Edge 和 Google Chrome 訪問站點時就會看到以下圖所示的效果:
藉助於 Microsoft.FeatureManagement.AspNetCore 擴展包,咱們能夠很容易實現 Feature Flag 效果。又因爲這種實現是基於 IConfiguration 的,因此很具備通用性。這裏列出官方給出的優勢:
很是感謝你能閱讀這篇文章,但願它能對你有所幫助。