Kestrel的ListenAnyIP和ListenLocalhost的区别
問題
在上篇文章,把AAStore.ProductCatalog.Api部署到docker中運(yùn)行,輸入地址訪問報(bào)錯(cuò)如下圖,說明外部無法訪問這個(gè)url。(當(dāng)然本地開發(fā)環(huán)境測(cè)試是可以訪問的)。后來修改此處options.ListenLocalhost(8081)的代碼改成options.ListenAnyIP(8081),可以訪問了。那這兩種寫法有什么區(qū)別呢?
在區(qū)別之前,我們先熟悉幾個(gè)概念(如果網(wǎng)絡(luò)知識(shí)比較好的,可以跳過):
本地回環(huán)地址(Loopback Address):
百度定義的定義,127.0.0.1,通常被稱為本地回環(huán)地址(Loopback Address),不屬于任何一個(gè)有類別地址類。它代表設(shè)備的本地虛擬接口,所以默認(rèn)被看作是永遠(yuǎn)不會(huì)宕掉的接口。在Windows操作系統(tǒng)中也有相似的定義,所以通常在安裝網(wǎng)卡前就可以ping通這個(gè)本地回環(huán)地址。一般都會(huì)用來檢查本地網(wǎng)絡(luò)協(xié)議、基本數(shù)據(jù)接口等是否正常的。
IPv6的本地回環(huán)地址形式:0:0:0:0:0:0:0:1,同IPV4中127.0.0.1地址的含義一樣,表示節(jié)點(diǎn)自已,也可以是::1,大多數(shù)windows和linux電腦上都將localhost指向了127.0.0.1這個(gè)地址,相當(dāng)于是本機(jī)地址。
ip地址類型
公有地址
公有地址(Public address)由Inter NIC(Internet Network Information Center因特網(wǎng)信息中心)負(fù)責(zé)。這些IP地址分配給注冊(cè)并向Inter NIC提出申請(qǐng)的組織機(jī)構(gòu)。通過它直接訪問因特網(wǎng)。
私有地址
私有地址(Private address)屬于非注冊(cè)地址,專門為組織機(jī)構(gòu)內(nèi)部使用。以下列出留用的內(nèi)部私有地址
- A類 10.0.0.0--10.255.255.255 
- B類 172.16.0.0--172.31.255.255 
- C類 192.168.0.0--192.168.255.255 
IPv6 [::] ( 0000:0000:0000:0000:0000:0000:0000:0000的簡(jiǎn)寫), IPv4 0.0.0.0 含義:
維基百科解釋,表示無效的,未知,不可用的目標(biāo)
在服務(wù)器中,常常表示監(jiān)聽本機(jī)所有的ip地址。一般我們?cè)诜?wù)端綁定端口的時(shí)候可以選擇綁定到0.0.0.0,這樣就可以通過多個(gè)ip地址訪問我的服務(wù)。
ListenLocalhost 和ListenAnyIP 區(qū)別
通過編碼配置Kestrel監(jiān)聽端口有三個(gè)方法可以實(shí)現(xiàn)ListenLocalhost、ListenAnyIP、Listen,其中ListenLocalhost等同于Listen的IPAddress.IPv6Loopback 和IPAddress.Loopback,ListenAnyIP等同于Listen的IPAddress.IPv6Any和IPAddress.Any。下面我看看可以查看他們的源代碼。
ListenLocalhost、ListenAnyIP 兩個(gè)方法的源碼
/// <summary>/// Listens on ::1 and 127.0.0.1 with the given port. Requesting a dynamic port by specifying 0 is not supported/// for this type of endpoint./// </summary>public void ListenLocalhost(int port, Action<ListenOptions> configure){if (configure == null){throw new ArgumentNullException(nameof(configure));}var listenOptions = new LocalhostListenOptions(port);ApplyEndpointDefaults(listenOptions);configure(listenOptions);ListenOptions.Add(listenOptions);} /// <summary>/// Listens on all IPs using IPv6 [::], or IPv4 0.0.0.0 if IPv6 is not supported./// </summary>public void ListenAnyIP(int port, Action<ListenOptions> configure){if (configure == null){throw new ArgumentNullException(nameof(configure));}var listenOptions = new AnyIPListenOptions(port);ApplyEndpointDefaults(listenOptions);configure(listenOptions);ListenOptions.Add(listenOptions);}通過源碼我們可以發(fā)現(xiàn),他們之間的區(qū)別是在構(gòu)造listenopthons對(duì)象不同,分別使用LocalhostListenOptions和AnyIPListenOptions進(jìn)行new創(chuàng)建實(shí)例,而AnyIPListenOptions和LocalhostListenOptions都繼承類ListenOptions,并且重寫BindAsync方法。源碼如下:
internal sealed class LocalhostListenOptions : ListenOptions{internal LocalhostListenOptions(int port): base(new IPEndPoint(IPAddress.Loopback, port)){if (port == 0){throw new InvalidOperationException(CoreStrings.DynamicPortOnLocalhostNotSupported);}}//綁定回環(huán)地址ipv4是127.0.0.1 ,iPV6是::1internal override async Task BindAsync(AddressBindContext context){var exceptions = new List<Exception>();try{var v4Options = Clone(IPAddress.Loopback);await AddressBinder.BindEndpointAsync(v4Options, context).ConfigureAwait(false);}catch (Exception ex) when (!(ex is IOException)){context.Logger.LogWarning(0, CoreStrings.NetworkInterfaceBindingFailed, GetDisplayName(), "IPv4 loopback", ex.Message);exceptions.Add(ex);}try{var v6Options = Clone(IPAddress.IPv6Loopback);await AddressBinder.BindEndpointAsync(v6Options, context).ConfigureAwait(false);}catch (Exception ex) when (!(ex is IOException)){context.Logger.LogWarning(0, CoreStrings.NetworkInterfaceBindingFailed, GetDisplayName(), "IPv6 loopback", ex.Message);exceptions.Add(ex);}if (exceptions.Count == 2){throw new IOException(CoreStrings.FormatAddressBindingFailed(GetDisplayName()), new AggregateException(exceptions));}// If StartLocalhost doesn't throw, there is at least one listener.// The port cannot change for "localhost".context.Addresses.Add(GetDisplayName());}} internal sealed class AnyIPListenOptions : ListenOptions{internal AnyIPListenOptions(int port): base(new IPEndPoint(IPAddress.IPv6Any, port)){}//如果本機(jī)不支持 IPv6就綁定ipv4internal override async Task BindAsync(AddressBindContext context){// when address is 'http://hostname:port', 'http://*:port', or 'http://+:port'try{await base.BindAsync(context).ConfigureAwait(false);}catch (Exception ex) when (!(ex is IOException)){context.Logger.LogDebug(CoreStrings.FormatFallbackToIPv4Any(IPEndPoint.Port));// for machines that do not support IPv6EndPoint = new IPEndPoint(IPAddress.Any, IPEndPoint.Port);await base.BindAsync(context).ConfigureAwait(false);}}}小結(jié):通過以上分析,端口綁定時(shí),建議使用IPAddress.Any,可以支持ipv6和ipv4地址。
webBuilder.ConfigureKestrel(options =>{//1.ListenLocalhost方法//options.ListenLocalhost(8081);//2.ListenAnyIP方法options.ListenAnyIP(8081);//3.Listen方法// options.Listen(IPAddress.Loopback, 8081);// Setup a HTTP/2 endpoint without TLS.options.ListenAnyIP(18081, o => o.Protocols =HttpProtocols.Http1AndHttp2);});參考:https://juejin.im/post/5d258b6ae51d454f73356dcf
總結(jié)
以上是生活随笔為你收集整理的Kestrel的ListenAnyIP和ListenLocalhost的区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 推荐:适合小白入门的Asp.Net Co
- 下一篇: 龙芯完成.NET移植稳步推进生态建设
