首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Net Core功能开关实战

作者:周文洋

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)

{

短信通知

}

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20190826A0MOJ700?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券