Yarp是微軟開源的一個用.net實現的反向代理工具包,github庫就叫reverse-proxy(反向代理)(吐槽一下微軟起名字233333)
nuget包preview9之前都叫Microsoft.ReverseProxy,preview10變成Yarp.ReverseProxy了
放上鏈接https://github.com/microsoft/reverse-proxy
使用背景
由於公司技術歷史原因,原來的網關規則是{paramA}_ {paramB} _ {paramC}_{paramD}這樣子的。
想要換個新網關,又要兼容舊的這種規則,調研過目前幾種API網關發現,apiSix支持這種操作(用$1占位符匹配參數)。
但是目前暫時不需要功能那么強大的api網關,而且不是.net技術實現的,出問題不好排查。
這是剛好發現Yarp這個東東,剛剛好符合需求,就拿來試試。
怎么用Yarp
Yarp主要要配置的東西就是Cluster(集群)和ProxyRoute(路由)
最簡單的用法直接使用appsettings.json配置集群和路由配置
下面內容照搬Yarp 的Getting Started內容
詳細的配置項可以直接看文檔~~
"ReverseProxy": {
"Routes": [
{
"RouteId": "route1",
"ClusterId": "cluster1",
"Match": {
"Path": "{**catch-all}"
},
}
],
"Clusters": {
"cluster1": {
"Destinations": {
"cluster1/destination1": {
"Address": "https://example.com/"
}
}
}
}
}
需要在startup.cs中配置Yarp
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
// Add the reverse proxy to capability to the server
var proxyBuilder = services.AddReverseProxy();
// Initialize the reverse proxy from the "ReverseProxy" section of configuration
proxyBuilder.LoadFromConfig(Configuration.GetSection("ReverseProxy"));
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// Enable endpoint routing, required for the reverse proxy
app.UseRouting();
// Register the reverse proxy routes
app.UseEndpoints(endpoints =>
{
endpoints.MapReverseProxy();
});
}
改造一下用法,使用持久化儲存配置
第一步,翻源碼,找到配置的實體對象,照搬微調一遍,大概就是下圖那么多個了。
第二步,實現IProxyConfigProvider和IProxyConfig,基本都是照搬原本代碼微整形一下233333
由於源碼里面ICertificateConfigLoader是internal類型,只能自己重新抄一次並實現了。
第三步,用EFCore持久化到數據庫
搞個DBContext,把實體全部加進去,配置好關系之后,CodeFirst直接生成數據庫哈哈哈哈
第四步,搞幾個Management管理數據(CURD)
第五步,實現一下配置熱更新
使用IChangeToken接口實現一個EFCoreReloadToken
在InStoreConfigProvider的GetConfig用ChangeToken.OnChange綁定一下事件
public IProxyConfig GetConfig()
{
// First time load
if (_config == null)
{
_subscription = ChangeToken.OnChange(_strore.GetReloadToken, UpdateConfig);
UpdateConfig();
}
return _config;
}
_strore是EFCoreReverseProxyStore對象,里面包含EFCoreReloadToken對象;
public class EFCoreReverseProxyStore : IReverseProxyStore
{
private EFCoreReloadToken _reloadToken = new EFCoreReloadToken();
private IServiceProvider _sp;
private IMemoryCache _cache;
private readonly ICertificateConfigLoader _certificateConfigLoader;
public EFCoreReverseProxyStore(IServiceProvider sp, IMemoryCache cache, ICertificateConfigLoader certificateConfigLoader)
{
_sp = sp;
_cache = cache;
_certificateConfigLoader = certificateConfigLoader;
}
//more code....
}
在要觸發更新配置的時候調用一下IReverseProxyStore.Reload()就可以重新加載配置了~~
第六步,再寫個擴展方法替換原本的IProxyConfigProvider
最后一步,在Startup中用一下
services.AddReverseProxy()
.LoadFromEFCore();
數據管理好了,差個界面(找前端小姐姐要一個)
第一步,搞個控制器,加上簡單CURD接口
第二步,找前端小姐姐要個界面對接一下API
好了搞完可以用了,試一試
搞一個測試WebAPi
加一個集群
加一個路由
用Postman測一測
好了正常使用,搞定。
路由匹配可以用多個占位符自由組合,然后在PathPattern轉換里面可以使用這些參數
測試一下性能1000個並發100000個請求
最后放下代碼~~
歡迎吐槽提意見或者一起改進哈哈哈
ReverseProxy.Store