.NET Core 3.0之深入源码理解Kestrel的集成与应用(一)

网友投稿 819 2022-11-18

.NET Core 3.0之深入源码理解Kestrel的集成与应用(一)

.NET Core 3.0之深入源码理解Kestrel的集成与应用(一)

写在前面

ASP.NET Core 的 Web 服务器默认采用Kestrel,这是一个跨平台、轻量级的Web服务器。

在开始之前,先回顾一下.NET Core 3.0默认的main()方法模板中,我们会调用Host.CreateDefaultBuilder方法,该方法的主要功能是配置应用主机及设置主机的属性,设置Kestrel 服务器配置为 Web 服务器,另外还包括日志功能、应用配置加载等等,此处不做展开。

作为一个轻量级的Web Server,它并没有IIS、Apache那些大而全的功能,但它依然可以单独运行,也可以搭配IIS、Apache等反向代理服务器结合使用。

本文将从源码角度讨论ASP.NET Core应用在Kestrel的相关知识点。

Kestrel

Kestrel的存在意义

了解这个问题,首先需要强调的是.NET Core应用的目标就是跨平台,既然要跨平台那么就需要适用各个平台上的Web服务器,各个服务器的启动、配置等等都是不尽相同的,如果每个服务器提供一套实现出来,如果未来出现了一个新的Web Server,然后又要增加新的实现,这会导致.NET Core应用的适用性滞后,也会很消耗人力,无法很好的达到跨平台的目标。

我们可以把Kestrel视作一个中间件,一个适配的功能,它抽象了各个服务器的特性,使得各个应用只需要调用同样的接口,即可最大限度的在各个平台上运行。

.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) }.csharpcode pre { margin: 0 }.csharpcode .rem { color: rgba(0, 128, 0, 1) }.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }.csharpcode .str { color: rgba(0, 96, 128, 1) }.csharpcode .op { color: rgba(0, 0, 192, 1) }.csharpcode .preproc { color: rgba(204, 102, 51, 1) }.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }.csharpcode .html { color: rgba(128, 0, 0, 1) }.csharpcode .attr { color: rgba(255, 0, 0, 1) }.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }.csharpcode .lnum { color: rgba(96, 96, 96, 1) }

运行方式

.NET Core 3.0下,Kestrel的集成已经相当成熟了,也提供了相应的自定义配置,以使得Kestrel的使用更加具有灵活性和可配性。它可以独立运行,也可以与反向代理服务器结合使用。

Kestrel本身是不支持多个应用共享同一个端口的,但是我们可以通过反向代理服务器来实现统一对外的相同的端口的共享。

以下是其单独运行示意图:

以下是其结合反向代理使用示意图:

Microsoft.AspNetCore.Server.Kestrel.Core

该类库是Kestrel的核心类库,里面包含了该功能的多个逻辑实现,以下简称改类库为Kestrel.Core。

Kestrel适配逻辑

AdaptedPipeline有两个公共方法:

RunAsync():用于读取(读取后会有Flush操作)和写入数据,并分别装载到Task中

CompleteAsync():完成读取和写入操作,并取消基础流的读取

另外还包括四个公共属性,如下所示:

1: public RawStream TransportStream { get; } 2:   3: public Pipe Input { get; } 4:   5: public Pipe Output { get; } 6:   7: public IKestrelTrace Log { get; }

它定义了可从中读取并写入数据的双工管道的对象。IDuplexPipe有两个属性,System.IO.Pipelines.PipeReader Input { get; }和System.IO.Pipelines.PipeReader Output { get; }。AdaptedPipeline还通过构造函数获取到了Pipe对象。

RawStream类继承自Stream,并重写了Stream的关键属性及方法,主要目标是提供适合于Kestrel读写数据方式的内部封装。

LoggingStream类也同样继承自Stream,和RawStream不同的是,里面增加操作过程的日志记录,主要用于记录在连接适配过程中的信息,不过需要启用日志才能把日志信息记录下来,以下是其对外的使用方式:

1: public static class ListenOptionsConnectionLoggingExtensions 2: { 3: ///

4: /// Emits verbose logs for bytes read from and written to the connection. 5: /// 6: /// 7: /// The . 8: /// 9: public static ListenOptions UseConnectionLogging(this ListenOptions listenOptions) 10: { 11: return listenOptions.UseConnectionLogging(loggerName: null); 12: } 13:   14: /// 15: /// Emits verbose logs for bytes read from and written to the connection. 16: /// 17: /// 18: /// The . 19: /// 20: public static ListenOptions UseConnectionLogging(this ListenOptions listenOptions, string loggerName) 21: { 22: var loggerFactory = listenOptions.KestrelServerOptions.ApplicationServices.GetRequiredService(); 23: var logger = loggerName == null ? loggerFactory.CreateLogger() : loggerFactory.CreateLogger(loggerName); 24: listenOptions.ConnectionAdapters.Add(new LoggingConnectionAdapter(logger)); 25: return listenOptions; 26: } 27: }

.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) }.csharpcode pre { margin: 0 }.csharpcode .rem { color: rgba(0, 128, 0, 1) }.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }.csharpcode .str { color: rgba(0, 96, 128, 1) }.csharpcode .op { color: rgba(0, 0, 192, 1) }.csharpcode .preproc { color: rgba(204, 102, 51, 1) }.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }.csharpcode .html { color: rgba(128, 0, 0, 1) }.csharpcode .attr { color: rgba(255, 0, 0, 1) }.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }.csharpcode .lnum { color: rgba(96, 96, 96, 1) }

Kestrel特性抽象

该模块下的 Kestrel特性,比较重要的有连接超时设置(包括设置超时时间、重置超时时间以及取消超时限制。这个特性使得我们的连接变得更加可控,比如,在某些特殊场景下,特殊条件下,我们需要取消超时限制或者动态重置超时时间),TLS应用程序协议功能,基于Http2.0的StreamId记录功能,用于停止连接计数的功能。

以下是连接超时接口的源代码

1: ///

2: /// Feature for efficiently handling connection timeouts. 3: /// 4: public interface IConnectionTimeoutFeature 5: { 6: /// 7: /// Close the connection after the specified positive finite 8: /// unless the timeout is canceled or reset. This will fail if there is an ongoing timeout. 9: /// 10: void SetTimeout(TimeSpan timeSpan); 11:   12: /// 13: /// Close the connection after the specified positive finite 14: /// unless the timeout is canceled or reset. This will cancel any ongoing timeouts. 15: /// 16: void ResetTimeout(TimeSpan timeSpan); 17:   18: /// 19: /// Prevent the connection from closing after a timeout specified by 20: /// or . 21: /// 22: void CancelTimeout(); 23: }

.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) }.csharpcode pre { margin: 0 }.csharpcode .rem { color: rgba(0, 128, 0, 1) }.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }.csharpcode .str { color: rgba(0, 96, 128, 1) }.csharpcode .op { color: rgba(0, 0, 192, 1) }.csharpcode .preproc { color: rgba(204, 102, 51, 1) }.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }.csharpcode .html { color: rgba(128, 0, 0, 1) }.csharpcode .attr { color: rgba(255, 0, 0, 1) }.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }.csharpcode .lnum { color: rgba(96, 96, 96, 1) }

Kestrel选项及限制功能

Kestrel的选项控制包括监听、Kestrel服务器、HTTPS连接适配。

1、监听选项功能在ListenOptions中实现,该类继承自IConnectionBuilder,ListenOptions的主要作用是描述Kestrel中已经打开的套接字,包括Unix域套接字路径、文件描述符、ipendpoint。ListenOptions内部会维护一个只读的List>()对象,并通过Use()方法加载新的Func对象,然后通过Build方式返回最后加入的Func

1: public IConnectionBuilder Use(Func middleware) 2: { 3: _middleware.Add(middleware); 4: return this; 5: } 6:   7: public ConnectionDelegate Build() 8: { 9: ConnectionDelegate app = context => 10: { 11: return Task.CompletedTask; 12: }; 13:   14: for (int i = _middleware.Count - 1; i >= 0; i--) 15: { 16: var component = _middleware[i]; 17: app = component(app); 18: } 19:   20: return app; 21: }

.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) }.csharpcode pre { margin: 0 }.csharpcode .rem { color: rgba(0, 128, 0, 1) }.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }.csharpcode .str { color: rgba(0, 96, 128, 1) }.csharpcode .op { color: rgba(0, 0, 192, 1) }.csharpcode .preproc { color: rgba(204, 102, 51, 1) }.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }.csharpcode .html { color: rgba(128, 0, 0, 1) }.csharpcode .attr { color: rgba(255, 0, 0, 1) }.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }.csharpcode .lnum { color: rgba(96, 96, 96, 1) }

需要注意的是ListenOptions在该类库内部还有两个子类,AnyIPListenOptions和LocalhostListenOptions,以用于特定场景的监听使用。

2、Kestrel服务器选项是在KestrelServerOptions中实现的,该类用于提供Kestrel特定功能的编程级别配置,该类内部会维护ListenOptions的列表对象,该类将ListenOptions的功能进一步展开,并加入了HTTPS、证书的默认配置与应用,这个类比较大,本文就不贴出源码了,有兴趣的同学可以自己去翻阅。

3、HTTPS连接适配选项在HttpsConnectionAdapterOptions实现,这个类用于设置Kestrel如何处理HTTPS连接,这里引入和证书功能、SSL协议、HTTP协议、超时功能,同时这里还可以自定义HTTPS连接的时候的证书处理模式(AllowCertificate、RequireCertificate等),以下是HttpsConnectionAdapterOptions的构造函数:

1: public HttpsConnectionAdapterOptions() 2: { 3: ClientCertificateMode = ClientCertificateMode.NoCertificate; 4: SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11; 5: HandshakeTimeout = TimeSpan.FromSeconds(10); 6: }

.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) }.csharpcode pre { margin: 0 }.csharpcode .rem { color: rgba(0, 128, 0, 1) }.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }.csharpcode .str { color: rgba(0, 96, 128, 1) }.csharpcode .op { color: rgba(0, 0, 192, 1) }.csharpcode .preproc { color: rgba(204, 102, 51, 1) }.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }.csharpcode .html { color: rgba(128, 0, 0, 1) }.csharpcode .attr { color: rgba(255, 0, 0, 1) }.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }.csharpcode .lnum { color: rgba(96, 96, 96, 1) }

可以看到,在默认情况下,是无证书模式,其SSL协议包括Tls12 和Tls11以及指定允许进行TLS/SSL握手的最大时间是十秒钟。

4、Kestrel的限制功能在KestrelServerLimits实现,主要包括:

保持活动状态超时客户端最大连接数(默认情况下,最大连接数不受限制 (NULL))请求正文最大大小(默认的请求正文最大大小为 30,000,000 字节,大约 28.6 MB)请求正文最小数据速率(默认的最小速率为 240 字节/秒,包含 5 秒的宽限期)请求标头超时(默认值为 30 秒)每个连接的最大流(默认值为 100)标题表大小(默认值为 4096)最大帧大小(默认值为 2^14)最大请求标头大小(默认值为 8,192)初始连接窗口大小(默认值为 128 KB)初始流窗口大小(默认值为 96 KB)

代码如下所示:

1: .ConfigureKestrel((context, options) => 2: { 3: options.Limits.MaxConcurrentConnections = 100; 4: options.Limits.MaxConcurrentUpgradedConnections = 100; 5: options.Limits.MaxRequestBodySize = 10 * 1024; 6: options.Limits.MinRequestBodyDataRate = 7: new MinDataRate(bytesPerSecond: 100, gracePeriod: TimeSpan.FromSeconds(10)); 8: options.Limits.MinResponseDataRate = 9: new MinDataRate(bytesPerSecond: 100, gracePeriod: TimeSpan.FromSeconds(10)); 10: options.Listen(IPAddress.Loopback, 5000); 11: options.Listen(IPAddress.Loopback, 5001, listenOptions => 12: { 13: listenOptions.UseHttps("testCert.pfx", "testPassword"); 14: }); 15: options.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(2); 16: options.Limits.RequestHeadersTimeout = TimeSpan.FromMinutes(1); 17: options.Limits.Http2.MaxStreamsPerConnection = 100; 18: options.Limits.Http2.HeaderTableSize = 4096; 19: options.Limits.Http2.MaxFrameSize = 16384; 20: options.Limits.Http2.MaxRequestHeaderFieldSize = 8192; 21: options.Limits.Http2.InitialConnectionWindowSize = 131072; 22: options.Limits.Http2.InitialStreamWindowSize = 98304; 23: });

其部分源码如下:

.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) }.csharpcode pre { margin: 0 }.csharpcode .rem { color: rgba(0, 128, 0, 1) }.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }.csharpcode .str { color: rgba(0, 96, 128, 1) }.csharpcode .op { color: rgba(0, 0, 192, 1) }.csharpcode .preproc { color: rgba(204, 102, 51, 1) }.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }.csharpcode .html { color: rgba(128, 0, 0, 1) }.csharpcode .attr { color: rgba(255, 0, 0, 1) }.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }.csharpcode .lnum { color: rgba(96, 96, 96, 1) }

.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) }.csharpcode pre { margin: 0 }.csharpcode .rem { color: rgba(0, 128, 0, 1) }.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }.csharpcode .str { color: rgba(0, 96, 128, 1) }.csharpcode .op { color: rgba(0, 0, 192, 1) }.csharpcode .preproc { color: rgba(204, 102, 51, 1) }.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }.csharpcode .html { color: rgba(128, 0, 0, 1) }.csharpcode .attr { color: rgba(255, 0, 0, 1) }.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }.csharpcode .lnum { color: rgba(96, 96, 96, 1) }

.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) }.csharpcode pre { margin: 0 }.csharpcode .rem { color: rgba(0, 128, 0, 1) }.csharpcode .kwrd { color: rgba(0, 0, 255, 1) }.csharpcode .str { color: rgba(0, 96, 128, 1) }.csharpcode .op { color: rgba(0, 0, 192, 1) }.csharpcode .preproc { color: rgba(204, 102, 51, 1) }.csharpcode .asp { background-color: rgba(255, 255, 0, 1) }.csharpcode .html { color: rgba(128, 0, 0, 1) }.csharpcode .attr { color: rgba(255, 0, 0, 1) }.csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 }.csharpcode .lnum { color: rgba(96, 96, 96, 1) }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:对比QQ浏览器、Chrome和FireFox
下一篇:在SpringBoot中使用lombok的注意事项
相关文章

 发表评论

暂时没有评论,来抢沙发吧~