1. 程式人生 > >淺析微軟的閘道器專案 -- ReverseProxy

淺析微軟的閘道器專案 -- ReverseProxy

# 淺析微軟的閘道器專案 `ReverseProxy` ## Intro 最近微軟新開了一個專案 [ReverseProxy](https://github.com/microsoft/reverse-proxy) ,也叫做 YARP(A Reverse Proxy) 官方介紹如下: > YARP is a reverse proxy toolkit for building fast proxy servers in .NET using the infrastructure from ASP.NET and .NET. The key differentiator for YARP is that it's been designed to be easily customized and tweaked to match the specific needs of each deployment scenario. 這是一個基於 .net (core) 和 asp.net (core) 的用來代理伺服器的反向代理元件,YARP的主要區別在於它的設計易於定製和調整,以適應每種部署方案的特定需求。 你可以基於這個專案來構建自己的 API Gateway 專案 ## YARP 設計 YARP 主要是基於 endpoint 路由 + asp.net core 中介軟體來設計實現的 來看一下官方的示例 `Startup` 配置: ``` csharp public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddReverseProxy() .LoadFromConfig(_configuration.GetSection("ReverseProxy")) .AddProxyConfigFilter(); } /// /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. ///
public void Configure(IApplicationBuilder app) { app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); endpoints.MapReverseProxy(proxyPipeline => { // Custom endpoint selection proxyPipeline.Use((context, next) =>
{ var someCriteria = false; // MeetsCriteria(context); if (someCriteria) { var availableDestinationsFeature = context.Features.Get(); var destination = availableDestinationsFeature.Destinations[0]; // PickDestination(availableDestinationsFeature.Destinations); // Load balancing will no-op if we've already reduced the list of available destinations to 1. availableDestinationsFeature.Destinations = new[] { destination }; } return next(); }); proxyPipeline.UseProxyLoadBalancing(); }); }); } ``` ### 中介軟體 基於 asp.net core 的中介軟體的設計可以使得一些現有的 asp.net core 的中介軟體可以無縫整合,不得不說微軟的設計真的是很優秀 相對來說 Ocelot 的的設計就會稍顯遜色一些,因為 Ocelot 的設計是 asp.net core 的一箇中間件,在 Ocelot 內部有自己的一套中介軟體,原來基於 asp.net core 的中介軟體要在 Ocelot 中使用就需要進一步開發,變成 Ocelot 的中介軟體,才能正常工作,比如 Ocelot 裡的限流中介軟體就是基於一個 asp.net core 的中介軟體來實現的 [AspNetCoreRateLimit](https://github.com/stefanprodan/AspNetCoreRateLimit) ### proxy endpoint 實現 ``` csharp public static void MapReverseProxy(this IEndpointRouteBuilder endpoints, Action configureApp) { if (endpoints is null) { throw new ArgumentNullException(nameof(endpoints)); } if (configureApp is null) { throw new ArgumentNullException(nameof(configureApp)); } var appBuilder = endpoints.CreateApplicationBuilder(); appBuilder.UseMiddleware(); configureApp(appBuilder); appBuilder.UseMiddleware(); var app = appBuilder.Build(); var routeBuilder = endpoints.ServiceProvider.GetRequiredService(); routeBuilder.SetProxyPipeline(app); var dataSource = (EndpointDataSource)endpoints.ServiceProvider.GetRequiredService(); endpoints.DataSources.Add(dataSource); } ``` 從上面的程式碼可以看到,針對反向代理的處理流程是也是一套中介軟體管道處理, 首先執行的是 `DestinationInitializerMiddleware` 在這一中介軟體,會獲取可用的下游節點,並通過 `HttpContext` 的 `Features` 來傳遞給後面的中介軟體, 然後會呼叫傳進來的自定義中介軟體的邏輯,在這個邏輯中,可以加一些我們自己的業務邏輯,十分靈活,可擴充套件性極強 之後會執行 `ProxyInvokerMiddleware`,在這個中介軟體中會去呼叫下游服務,並生成 response 返回給客戶端 ## More 目前這個專案在還是在積極開發中,可以關注一下,但是暫時不建議在專案中使用,目前還沒釋出 preview 版本,原本這個專案只支援 .net 5,不支援 .netcore3.1 ,在許多人的呼籲之下,微軟打算在 preview2 版本中提供對 dotnetcore 3.1 的支援,詳細可以參考 issue:
,希望提供 .net core 3.1 支援的可以去點個贊哈 ## Reference - -