微軟用它取代了nginx吞吐量提升了百分之八十!

微軟用它取代了nginx吞吐量提升了百分之八十!

azure應用服務用yarp取代了nginx,獲得了80%以上的吞吐量。他們每天處理160b多個請求(1.9 m rps)。這是微軟的一項了不起的技術創新。

最后更新 2024/1/14 上午6:19
tokengo
预计阅读 7 分钟
分类
.NET
标签
.NET C# 技術更新

大佬消息截图

azure 應用服務用 yarp 取代了 nginx,獲得了 80%以上的吞吐量。他們每天處理 160b 多個請求(1.9 m rps)。這是微軟的一項了不起的技術創新。

首先我們來居間一下什麼是 yarp

yarp 是什麼?

yarp(yet another reverse proxy)是一個開源的、高性能的反向代理庫,由 microsoft 開發,使用 c#語言編寫。它旨在作為.net 平台上構建反向代理伺服器的基礎。yarp 主要針對.net 5 及以上版本,允許開發者在.net 應用程式中輕鬆地實現反向代理的功能。

yarp 的主要特點和功能:

  1. 模塊化和可擴展性: yarp 設計成高度模塊化的,這意味著可以根據需要替換或擴展內部組件,如 http 請求路由、負載均衡、健康檢查等。
  2. 性能: yarp 針對高性能進行了優化,利用了.net 的異步編程模型和高效的 io 操作,以處理大量並發連接。
  3. 配置驅動: yarp 的行為可以通過配置來控制,支持從文件、資料庫或其他來源動態加載配置。
  4. 路由: 可以基於各種參數(如路徑、頭部、查詢參數)配置請求路由規則。
  5. 負載均衡: 內置多種負載均衡策略,如輪詢、最少連接、隨機選擇等,並且可以自定義負載均衡策略。
  6. 健康檢查: 支持後端服務的健康檢查,以確保請求只會被轉發到健康的後端服務實例。
  7. 轉換器: 允許對請求和響應進行轉換,如修改頭部、路徑或查詢參數。
  8. 會話親和性: 支持會話親和性(session affinity),確保來自同一客戶端的請求被發送到相同的後端服務實例。

使用 yarp 的一些場景:

  • 反向代理: 在客戶端和後端服務之間提供一個中間層,用於請求轉發和負載均衡。
  • api 網關: 作為微服務架構中的 api 網關,提供路由、鑒權、監控等功能。
  • 邊緣服務: 在應用程式和外部世界之間提供安全層,處理 ssl 終止、請求限制等任務。

yarp 簡單的使用

創建一個 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 工具代理使用

下面我們在提供一個在中間件使用 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();

也是一樣會被代理過去,但是與簡單使用不一樣的是我們是在代碼層面控制代理的。

代码控制反代效果一样

使用 yarp 修改 bing 的響應內容

我們繼續基於上面的代理使用進行修改 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 搜索 - 国内版

修改响应内容结果

yarp 相關資料

技術交流群:737776595

官方文檔:https://microsoft.github.io/reverse-proxy/articles/getting-started.html

來著 token 的分享

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 2026/2/7

aot使用經驗總結

從項目創建伊始,就應養成良好的習慣,即只要添加了新功能或使用了較新的語法,就及時進行 aot 發布測試。

继续阅读