浅析微软的网关项目 -- ReverseProxy
浅析微软的网关项目 ReverseProxy
Intro
最近微软新开了一个项目 ReverseProxy ,也叫做 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 配置:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddReverseProxy()
.LoadFromConfig(_configuration.GetSection("ReverseProxy"))
.AddProxyConfigFilter<CustomConfigFilter>();
}
/// <summary>
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// </summary>
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<IAvailableDestinationsFeature>();
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
proxy endpoint 实现
public static void MapReverseProxy(this IEndpointRouteBuilder endpoints, Action<IApplicationBuilder> 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<DestinationInitializerMiddleware>();
configureApp(appBuilder);
appBuilder.UseMiddleware<ProxyInvokerMiddleware>();
var app = appBuilder.Build();
var routeBuilder = endpoints.ServiceProvider.GetRequiredService<IRuntimeRouteBuilder>();
routeBuilder.SetProxyPipeline(app);
var dataSource = (EndpointDataSource)endpoints.ServiceProvider.GetRequiredService<IProxyDynamicEndpointDataSource>();
endpoints.DataSources.Add(dataSource);
}
从上面的代码可以看到,针对反向代理的处理流程是也是一套中间件管道处理,
首先执行的是 DestinationInitializerMiddleware 在这一中间件,会获取可用的下游节点,并通过 HttpContext 的 Features 来传递给后面的中间件,
然后会调用传进来的自定义中间件的逻辑,在这个逻辑中,可以加一些我们自己的业务逻辑,十分灵活,可扩展性极强
之后会执行 ProxyInvokerMiddleware,在这个中间件中会去调用下游服务,并生成 response 返回给客户端
More
目前这个项目在还是在积极开发中,可以关注一下,但是暂时不建议在项目中使用,目前还没发布 preview 版本,原本这个项目只支持 .net 5,不支持 .netcore3.1 ,在许多人的呼吁之下,微软打算在 preview2 版本中提供对 dotnetcore 3.1 的支持,详细可以参考 issue: https://github.com/microsoft/reverse-proxy/issues/159,希望提供 .net core 3.1 支持的可以去点个赞哈
Reference
- https://github.com/microsoft/reverse-proxy
- https://github.com/microsoft/reverse-proxy/blob/master/samples/ReverseProxy.Sample/Startup.cs
浅析微软的网关项目 -- ReverseProxy的更多相关文章
- soul开源网关项目搭建学习
1. soul开源网关项目搭建学习 1.1. 地址 https://gitee.com/shuaiqiyu/soul 1.2. 介绍 官方介绍:这是一个异步的,高性能的,跨语言的,响应式的API网关. ...
- C#网络编程技术微软Socket实战项目演练(三)
一.课程介绍 本次分享课程属于<C#高级编程实战技能开发宝典课程系列>中的第三部分,阿笨后续会计划将实际项目中的一些比较实用的关于C#高级编程的技巧分享出来给大家进行学习,不断的收集.整理 ...
- 创建网关项目(Spring Cloud Gateway)
创建网关项目 加入网关后微服务的架构图 创建项目 POM文件 <properties> <java.version>1.8</java.version> <s ...
- 分享一个基于Abp Vnext开发的API网关项目
这个项目起源于去年公司相要尝试用微服务构建项目,在网关的技术选型中,我们原本确认了ApiSix 网关,如果需要写网关插件需要基于Lua脚本去写,我和另外一个同事当时基于这个写了一个简单的插件,但是开发 ...
- 走进云背后:微软Azure web 项目通过web service部署web site
探索云那不为人知的故事(一):Web Services部署web site 前奏:Windows Azure是微软基于云计算的操作系统,现在更名为“Microsoft Azure”,和Azure Se ...
- SQLServer 微软团队开源项目 (web 版?)
http://www.codeplex.com/site/users/view/SQLTeamAdmin
- Amazing 2020
Amazing 2020 Intro 2020 转眼即逝,2020 是比较艰辛的一年,因为疫情原因,很多人的工作以及生活都多多少少受到了一些影响. 引用网上盛传的一句话--2020 实"鼠& ...
- 接微软技术(c#,.net,vb.net, asp.net, sql server, bi, dw etc)项目
最近闲赋在家,接微软技术的项目,主要有c#,.net,vb.net, asp.net, sql server, bi, dw etc,欢迎推荐.不好意思,借首页发一下.
- 一大波开发者福利来了,一份微软官方Github上发布的开源项目清单等你签收
目录 微软Github开源项目入口 微软开源项目受欢迎程度排名 Visual Studio Code TypeScript RxJS .NET Core 基础类库 CNTK Microsoft cal ...
随机推荐
- 【转】动态规划之最长公共子序列(LCS)
[原文链接]最长公共子序列(Longest Common Subsequence,简称 LCS)是一道非常经典的面试题目,因为它的解法是典型的二维动态规划,大部分比较困难的字符串问题都和这个问题一个套 ...
- Spring5:Java Config
@Configuration @Bean @ComponentScan @ImportResource 使用Java的方式配置spring,完全不使用spring配置文件,交给java来做! 两个注解 ...
- Java集合案例(产生不重复随机数)
获取10个1-20之间的随机数,要求不能重复 用数组实现,但是数组的长度是固定的,长度不好确定.所以我们使用集合实现. 分析:A:创建产生随机数的对象B:创建一个存储随机数的集合C:定义一个统计变量. ...
- ES6中的let关键字,有什么用呢?
来吧,开始本节的学习! ES6 给开发者带来很多令人激动的特性,其中let关键字就是其中之一. 那么,let关键字是什么东西? let 的用途 我们回想一下,我们平时在写代码的时候,用var来声明一个 ...
- javascript-如何获取标签的内容
<input>标签的: document.getElementById("id").value ; 其他文本标签的: document.getElementById(& ...
- 关于php抑错方法
在循环里,如果@不能用的话,就使用try catch,是可以的
- python学习04数据
#1.**幂 //返回商的整数部分x=5y=3print(x**y)print(x//y)print(5/2)#2.复数a+bjc=2+5jprint(c.real)#返回复数的实部print(c.i ...
- QT 无法抓住异常
出处:https://stackoverflow.com/questions/40980171/qt5core-dll-crashing I've found that enabling /EHa ( ...
- [Batch 脚本] 批量生成文件夹
@echo off echo start set time=30000 echo %time% for /l %%i in (1,1, %time%) do ( echo %%i% md " ...
- zabbix监控ftp
[root@agent ~]# yum -y install vsftpd [root@agent ~]# systemctl start vsftpd[root@agent ~]# systemct ...