Azure应用服务用YARP取代了Nginx,获得了80%以上的吞吐量。他们每天处理160B多个请求(1.9 m RPS)。这是微软的一项了不起的技术创新。
首先我们来介绍一下什么是Yarp
YARP(Yet Another Reverse Proxy)是一个开源的、高性能的反向代理库,由Microsoft开发,使用C#语言编写。它旨在作为.NET平台上构建反向代理服务器的基础。YARP主要针对.NET 5及以上版本,允许开发者在.NET应用程序中轻松地实现反向代理的功能。
创建一个WebApi的项目
安装Nuget
包
<ItemGroup> <PackageReference Include="Yarp.ReverseProxy" Version="2.0.0" /> </ItemGroup>
打开appsettings.json
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*", "ReverseProxy": { "Routes": { "route1" : { "ClusterId": "cluster1", "Match": { "Path": "{**catch-all}" } } }, "Clusters": { "cluster1": { "Destinations": { "destination1": { "Address": "https://cn.bing.com/" } } } } } }
打开Program.cs
var builder = WebApplication.CreateBuilder(args); builder.Services.AddReverseProxy() .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy")); var app = builder.Build(); app.MapReverseProxy(); app.Run();
然后启动项目,访问我们的api就会被代理转发到bing
上
下面我们在提供一个在中间件使用yarp的方式
我们需要用到IHttpForwarder
先修改Program.cs
在这里我们注入了HttpForwarder
,然后提供一个Run中间件,在中间件中手动指定了端点的地址https://cn.bing.com/
然后我们启动一下项目。
using Yarp.ReverseProxy.Forwarder; var builder = WebApplication.CreateBuilder(args); builder.Services.AddHttpForwarder(); // 注入IHttpForwarder var app = builder.Build(); var httpMessage = new HttpMessageInvoker(new HttpClientHandler()); app.Run((async context => { var httpForwarder = context.RequestServices.GetRequiredService<IHttpForwarder>(); var destinationPrefix = "https://cn.bing.com/"; await httpForwarder.SendAsync(context, destinationPrefix, httpMessage); })); app.Run();
也是一样会被代理过去,但是于简单使用不一样的是我们是在代码层面控制代理的。
我们继续基于上面的代理使用进行修改bing的相应内容!
打开Program.cs
var builder = WebApplication.CreateBuilder(args); builder.Services.AddHttpForwarder(); // 注入IHttpForwarder var app = builder.Build(); var httpMessage = new HttpMessageInvoker(new HttpClientHandler() { // 忽略https错误 ServerCertificateCustomValidationCallback = (_, _, _, _) => true, AllowAutoRedirect = false, AutomaticDecompression = DecompressionMethods.GZip, UseCookies = false, UseProxy = false, UseDefaultCredentials = true, }); var destinationPrefix = "https://cn.bing.com/"; var bingTransformer = new BingTransformer(); app.Run((async context => { var httpForwarder = context.RequestServices.GetRequiredService<IHttpForwarder>(); await httpForwarder.SendAsync(context, destinationPrefix, httpMessage, new ForwarderRequestConfig(), bingTransformer); })); app.Run();
创建BingTransformer.cs
public class BingTransformer : HttpTransformer { public override async ValueTask TransformRequestAsync(HttpContext httpContext, HttpRequestMessage proxyRequest, string destinationPrefix, CancellationToken cancellationToken) { var uri = RequestUtilities.MakeDestinationAddress(destinationPrefix, httpContext.Request.Path, httpContext.Request.QueryString); proxyRequest.RequestUri = uri; proxyRequest.Headers.Host = uri.Host; await base.TransformRequestAsync(httpContext, proxyRequest, destinationPrefix, cancellationToken); } public override async ValueTask<bool> TransformResponseAsync(HttpContext httpContext, HttpResponseMessage? proxyResponse, CancellationToken cancellationToken) { await base.TransformResponseAsync(httpContext, proxyResponse, cancellationToken); if (httpContext.Request.Method == "GET" && httpContext.Response.Headers["Content-Type"].Any(x => x.StartsWith("text/html"))) { var encoding = proxyResponse.Content.Headers.FirstOrDefault(x => x.Key == "Content-Encoding").Value; if (encoding?.FirstOrDefault() == "gzip") { var content = proxyResponse?.Content.ReadAsByteArrayAsync(cancellationToken).Result; if (content != null) { var result = Encoding.UTF8.GetString(GZipDecompressByte(content)); result = result.Replace("国内版", "Token Bing 搜索 - 国内版"); proxyResponse.Content = new StringContent(GZipDecompressString(result)); } } else if (encoding.FirstOrDefault() == "br") { var content = proxyResponse?.Content.ReadAsByteArrayAsync(cancellationToken).Result; if (content != null) { var result = Encoding.UTF8.GetString(BrDecompress(content)); result = result.Replace("国内版", "Token Bing 搜索 - 国内版"); proxyResponse.Content = new ByteArrayContent(BrCompress(result)); } } else { var content = proxyResponse?.Content.ReadAsStringAsync(cancellationToken).Result; if (content != null) { content = content.Replace("国内版", "Token Bing 搜索 - 国内版"); proxyResponse.Content = new StringContent(content); } } } return true; } /// <summary> /// 解压GZip /// </summary> /// <param name="bytes"></param> /// <returns></returns> public static byte[] GZipDecompressByte(byte[] bytes) { using var targetStream = new MemoryStream(); using var compressStream = new MemoryStream(bytes); using var zipStream = new GZipStream(compressStream, CompressionMode.Decompress); using (var decompressionStream = new GZipStream(compressStream, CompressionMode.Decompress)) { decompressionStream.CopyTo(targetStream); } return targetStream.ToArray(); } /// <summary> /// 解压GZip /// </summary> /// <param name="str"></param> /// <returns></returns> public static string GZipDecompressString(string str) { using var compressStream = new MemoryStream(Encoding.UTF8.GetBytes(str)); using var zipStream = new GZipStream(compressStream, CompressionMode.Decompress); using var resultStream = new StreamReader(new MemoryStream(compressStream.ToArray())); return resultStream.ReadToEnd(); } /// <summary> /// Br压缩 /// </summary> /// <param name="input"></param> /// <returns></returns> public static byte[] BrCompress(string str) { using var outputStream = new MemoryStream(); using (var compressionStream = new BrotliStream(outputStream, CompressionMode.Compress)) { compressionStream.Write(Encoding.UTF8.GetBytes(str)); } return outputStream.ToArray(); } /// <summary> /// Br解压 /// </summary> /// <param name="input"></param> /// <returns></returns> public static byte[] BrDecompress(byte[] input) { using (var inputStream = new MemoryStream(input)) using (var outputStream = new MemoryStream()) using (var decompressionStream = new BrotliStream(inputStream, CompressionMode.Decompress)) { decompressionStream.CopyTo(outputStream); return outputStream.ToArray(); } } }
得到的效果我们将国内版
修改成了Token Bing 搜索 - 国内版
技术交流群:737776595
官方文档:https://microsoft.github.io/reverse-proxy/articles/getting-started.html
来着token的分享