.net core下网关yarp试用,跟ocelot性能比较

YARP一个.net core平台下新的网关框架,微软自己家的,应该不错大家可以拿来用下。不过yarp比较新所以只支持.net core3.1以上版本的,如果你的项目时3.1以前的,那没办法赶紧升版本吧。

yarp的官方文档YARP Documentation,官网上写道这是一个高性能的网关。拿来用下看看,顺便跟之前的ocelot做下简单比较,哪个性能好。

使用方法,创建一个.net core3.1项目,nuget安装Yarp.ReverseProxy

 然后在appsetting.json中添加节点配置如下,yarp也跟ocelot一样是通过配置文件来实现对代理地址设置。http://localhost:5001是我之前就创建好的一个api项目,准备用yarp代理到这个api地址来访问。

  "ReverseProxy": {
    "Routes": {
      "route2": {
        "ClusterId": "cluster_product",
        "Match": {
          "Path": "/api/{*all}"
        },
        "Transforms": [
          {}
        ]
      }
    },
    "Clusters": {
      "cluster_product": {
        "Destinations": {
          "first_destination": {
            "Address": "http://localhost:5001"
          },
          "two_destination": {
            "Address": "http://localhost:5001"
          }
        }
      }
    }
  }

在startup中代码如下

    public class Startup {
        public readonly IConfiguration _configuration;
        public Startup(IConfiguration configuration) {
            _configuration = configuration;
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        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"));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
            if (env.IsDevelopment()) {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints => {
                //endpoints.MapGet("/", async context => {
                //    await context.Response.WriteAsync("Hello World!");
                //});
                endpoints.MapReverseProxy();
            });
        }
    }

然后运行此程序

同时我又用ocelot也创建了一个项目,同样也是代理到这个地址http://localhost:5001

两个网关项目都运行起来,同时api项目也运行起来。通过postman进行调用测试

5003端口是yarp网关,8001端口是ocelot网关

yarp 

ocelot 

 经过多次调用同一个接口发现,yarp的速度要比ocelot快不少,至少30%以上。的确不错哦,可以拿来一用的。

我只是简单的一测试,不过大体能说明yarp性能确实不错,大家可以自己去做下测试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值