测试环境:
Ubuntu 22.04.3 LTS (GNU/Linux 6.5.0-14-generic x86_64)
Intel(R) Xeon(R) CPU E5-2673 v3 @ 2.40GHz
*2
运行内存:94.3G
yarp环境
.NET 8 SDK
Program.cs
代码:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddReverseProxy()
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
var app = builder.Build();
app.MapReverseProxy();
app.Run();
Test.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0TargetFramework>
<Nullable>enableNullable>
<ImplicitUsings>enableImplicitUsings>
<InvariantGlobalization>trueInvariantGlobalization>
<PublishAot>truePublishAot>
<StackTraceSupport>falseStackTraceSupport>
<OptimizationPreference>SizeOptimizationPreference>
<PublishTrimmed>truePublishTrimmed>
<BlazorEnableTimeZoneSupport>falseBlazorEnableTimeZoneSupport>
<EventSourceSupport>falseEventSourceSupport>
<HttpActivityPropagationSupport>falseHttpActivityPropagationSupport>
<EnableUnsafeBinaryFormatterSerialization>falseEnableUnsafeBinaryFormatterSerialization>
<MetadataUpdaterSupport>falseMetadataUpdaterSupport>
<UseNativeHttpHandler>trueUseNativeHttpHandler>
<TrimMode>linkTrimMode>
PropertyGroup>
<ItemGroup>
<PackageReference Include="Yarp.ReverseProxy" Version="2.1.0" />
ItemGroup>
Project>
参考 Native AOT deployment overview - .NET | Microsoft Learn 在服务器中安装aot环境
使用以下指令构建aot程序
dotnet publish -c Release -r linux-x64 --self-contained true /p:PublishAot=true --output ../output
Nginx安装
在服务器中安装nginx
sudo apt install nginx
在/etc/nginx/conf.d
目录下创建一个wwwroot.conf
server {
listen 7771;
server_name localhost;
location / {
add_header 'Access-Control-Allow-Origin' 'http://localhost:8088';
add_header 'Cache-Control' 'public, max-age=604800';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
rewrite ^/proxy/bing/(.*)$ /$1 break;
proxy_pass http://127.0.0.1:7777/;
}
}
代理的服务
.NET 8 SDK
创建一个用于测试的代理服务,提供一个简单的接口,直接返回空的字符串。我们将这个服务发布成linux-64的程序,
Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
var app = builder.Build();
app.MapGet("/weatherforecast", () => string.Empty)
.WithName("GetWeatherForecast");
app.Run();
并且使用
chmod +x WebApplication1
然后启动我们的代理测试端点
./WebApplication1 urls="http://*:7777"
使用的测试工具
Apipost-Team/runnerGo: A tool similar to apache bench (ab) (github.com)
由ApiPOST开源的基于Go语言实现的压测工具,我们去Release下载发布好的win-64位程序,然后执行,
然后打开测试界面runnerGo UI (apipost.cn)
压测结果
http://192.168.31.251:7772/weatherforecast Yarp代理的服务
http://192.168.31.251:7771/weatherforecast Nginx代理的服务
第一轮测试:
YARP压测结果:
Nginx压测结果:
第二轮测试:
Yarp压测结果:
Nginx压测结果:
第三轮压测:
Yarp压测结果:
Nginx压测结果:
结论
以上测试都是在内网测试,都属于同一个局域网,由测试结果得出Yarp基本完胜Nginx,虽然说基本性能超越,但是Yarp也并发完全可替代Nginx,Nginx是支持TCP/UDP代理的,而Yarp默认是只支持Http协议的代理。
来自token的分享
技术交流群:737776595