作者:周文洋
leansoftX.com研发总监,认证 ScrumMaster,曾为多家客户提供微软Team Foundation Server实施咨询、二次开发、报表定制等服务,包括:中国农业银行,博时基金,斯伦贝谢,京东商城,国电南自等,现负责公司核心产品的开发工作。
Feature Toggle介绍
Feature Flag (又名 Feature Toggle、Feature switch、Flip等) 是一种可以通过配置(配置文件、数据库等)或自动化(特定用户、特定时间等)控制线上功能开启或者关闭的方式。核心思想就是将功能的开发和代码的发布解耦。
发布功能开关:
通过发布功能开关,开发团队可以将未完成的功能在发布时设置为隐藏,这样就可以持续的发布新功能到生产也不会影响到用户的使用,同时避免了一个复杂功能需要较长开发周期导致的在合并代码的时候出现各种冲突难以解决的问题。直到新功能稳定,开启功能开关或者删除对应功能开关来完成功能上线。
业务功能开关:
- 实现A/B测试。
- 针对特定人群发布功能尽早获得反馈。
- 针对特定条件开启或者关闭功能。例如可以在特定时间、特定地域、特定人员开启,能线上开启或者关闭,实现快速回滚。
.Net Core实战
开源框架:
using FeatureToggle;
namespace LeansoftX_FeatureToggle.Models.FeatureToggles
{
public class WechatNotifyFeature:RandomFeatureToggle{ }
}
using FeatureToggle;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LeansoftX_FeatureToggle.Models.FeatureToggles;
namespace LeansoftX_FeatureToggle.Models
{
public class HomeViewModel
{
public WechatNotifyFeature WechatNotifyFeature
{
get { return new WechatNotifyFeature(); }
}
}
}
public IActionResult Index()
{
return View (new Models.HomeIndexViewModel());
}
@model LeansoftX_FeatureToggle.Models.HomeIndexViewModel
@if (Model.WechatNotifyFeature.FeatureEnabled)
{
微信通知
}
usingFeatureToggle;
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;usingSystem.Threading.Tasks;
namespaceLeansoftX_FeatureToggle.Models.FeatureToggles
{
publicclassEmailNotifyFeature:SimpleFeatureToggle
{
}
}
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Threading.Tasks;
usingFeatureToggle.Internal;
usingMicrosoft.AspNetCore.Builder;
usingMicrosoft.AspNetCore.Hosting;
usingMicrosoft.Extensions.Configuration;
usingMicrosoft.Extensions.DependencyInjection;
usingLeansoftX_FeatureToggle.Models.FeatureToggles;
namespaceLeansoftX_FeatureToggle
{
public classStartup
{
publicIConfigurationRoot Configuration {get; }
publicStartup(IHostingEnvironment env)
{
varbuilder =newConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional:
false, reloadOnChange:true)
.AddJsonFile($"appsettings.
.json", optional:true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
// This method gets called by the runtime. Use this method to
add services to the container.
publicvoidConfigureServices(IServiceCollection services)
{
varprovider =newAppSettingsProvider
{ Configuration = Configuration };
services.AddSingleton(newEmailNotifyFeature
{
ToggleValueProvider = provider
});
services.AddMvc();
}
// This method gets called by the runtime. Use this method
to configure the HTTP request pipeline.
publicvoidConfigure(IApplicationBuilder app, IHostingEnvironmen
t env)
{
if(env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name:"default",
template:"//");
});
}
}
}
{
"FeatureToggle": {
"EmailNotifyFeature": "true"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default":"Debug",
"System":"Information",
"Microsoft":"Information"
}
}
}
privatereadonlyEmailNotifyFeature _emailNotifyFeature;
publicHomeController(EmailNotifyFeature emailNotifyFeature)
{
_emailNotifyFeature = emailNotifyFeature;
}
publicIActionResult Index()
{
ViewBag.EmailNotifyFeature = _emailNotifyFeature;
return View(newModels.HomeIndexViewModel());
}
@model LeansoftX_FeatureToggle.Models.HomeIndexViewModel
@{
var emailNotifyFeature =
(LeansoftX_FeatureToggle.Models.FeatureToggles.EmailNotifyFeature)ViewBag.EmailNotifyFeature;
ViewData["Title"]= "LeansoftX";
Layout = null;
}
主页
@if (Model.WechatNotifyFeature.FeatureEnabled)
{
微信通知
}
@if (emailNotifyFeature.FeatureEnabled)
{
邮件通知
}
publicclassSpecificUsersFeatureToggle:IFeatureToggle{
publicboolFeatureEnabled
{
get{
varuser =newModels.Users().GetUser();if(user.Name =="jackyzhou"|| user.Name =="leixu")
{
returntrue;
}
else
{
returnfalse;
}
}
}
}
}
public class SMSNotifyFeature:CustomToggles.SpecificUsersFeatureToggle
{
}
public class HomeIndexViewModel
{
public WechatNotifyFeature WechatNotifyFeature
{
get { return new WechatNotifyFeature();
}
public SMSNotifyFeature SMSNotifyFeature
{
get { return new SMSNotifyFeature(); }
}
}
@if (Model.WechatNotifyFeature.FeatureEnabled)
{
微信通知
}
@if (emailNotifyFeature.FeatureEnabled)
{
邮件通知
}
@if (Model.SMSNotifyFeature.FeatureEnabled)
{
短信通知
}
领取专属 10元无门槛券
私享最新 技术干货