你的域名是如何变成 IP 地址的?
<section id="nice" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="padding: 0 10px; word-spacing: 0; word-wrap: break-word; text-align: left; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, "PingFang SC", Cambria, Cochin, Georgia, Times, "Times New Roman", serif; line-height: 1.6; letter-spacing: 0.034em; color: rgba(63, 63, 63, 1); font-size: 16px"><blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(53, 179, 120, 1); font-size: 0.9em; overflow: auto; overflow-scrolling: touch; background: rgba(251, 249, 253, 1); color: rgba(106, 115, 125, 1); padding: 15px 20px; margin-bottom: 20px; margin-top: 20px; line-height: 27px; display: block"><p style="padding: 0; margin: 0; line-height: 26px; font-size: 15px; color: rgba(89, 89, 89, 1)">我的 个人网站 上线了,上面可以更好的检索历史文章,并且可以对文章进行留言,欢迎大家访问</p>
</blockquote>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">可能大家都知道或者被问过一个问题,那就是很经典的「从浏览器输入 URL 再到页面展示,都发生了什么」。这个问题虽然简单,但是真的能够从回答的各种细节上看出不同人之间的水平差距。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">这篇文章主要是聊一聊输入 URL 之后的第一步——<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">域名解析</strong></p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">域名就类似于 www.google.com,而通过 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">ping</code> 命令,就可以查询到对应域名的 IP 地址了。</p>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://tva1.sinaimg.cn/large/008i3skNgy1gtjt4x3mopj611e0kc0wd02.jpg" alt="" style="display: block; margin: 0 auto 25px; max-width: 100%; border-radius: 4px"></figure>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">那为什么又要有域名,又要有 IP 呢?</p>
<h3 data-tool="mdnice编辑器" style="margin-bottom: 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px; margin-top: 1.2em"><span style="background-image: url("https://files.mdnice.com/koala-3.png"); background-size: 100% 100%; background-repeat: no-repeat; display: inline-block; width: 16px; height: 15px; line-height: 15px; margin-bottom: -1px"></span><span class="prefix" style="display: none"></span><span class="content" style="font-size: 17px; font-weight: bold; display: inline-block; margin-left: 8px; color: rgba(72, 179, 120, 1)">域名、IP 共存</span><span class="suffix" style="display: none"></span></h3>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">首先还是解释一下,为什么会出现现在这种域名、IP 地址共存的情况。主要有两个点:</p>
<ol data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500"><strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">提升用户体验</strong></section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500"><strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">提高运行效率</strong></section></li></ol>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">分别解释一下,IP 地址长度为 32 位,平时用十进制来表示的话,就长这样——<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">192.168.1.0</code> ,但是想象一下,如果我们要访问某个网站需要让我们输入这么一长串数字,体验肯定相当差,首先记忆这么长串数字对很多人来说就很痛苦,更何况我们常用的网站肯定不止一个。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">除此之外,如果你给其他人推广你的网站,你吧啦吧啦说了一大堆,然后来个「如果你感兴趣,请访问我们的网站 192.168.1.0」,然后就没有然后了。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">这也是为啥现在仍然在使用域名,方便人脑去记忆。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">那为啥还需要 IP 地址呢?因为 IPv4 中的 IP 地址只需要 <strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">4 个字节</strong>,而用字符串表示的域名最少也需要几十个字节,长的甚至达到几百字节,而这会大大的增加底层路由器的负担。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">这也是为啥 IP 地址仍然在被使用。人来使用域名,而路由器层则使用 IP 地址,就跟我们书写的是我们能认识的字符,而最终计算机认识的是一堆二进制一样。</p>
<h3 data-tool="mdnice编辑器" style="margin-bottom: 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px; margin-top: 1.2em"><span style="background-image: url("https://files.mdnice.com/koala-3.png"); background-size: 100% 100%; background-repeat: no-repeat; display: inline-block; width: 16px; height: 15px; line-height: 15px; margin-bottom: -1px"></span><span class="prefix" style="display: none"></span><span class="content" style="font-size: 17px; font-weight: bold; display: inline-block; margin-left: 8px; color: rgba(72, 179, 120, 1)">DNS 解析</span><span class="suffix" style="display: none"></span></h3>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">知道了这个背景之后,我们就可以来看看「域名」是如果变成「IP 地址」的。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">首先我们知道,会往 DNS 服务器发送请求,那问题就来了,浏览器怎么知道 DNS 服务器的地址是啥?</p>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://tva1.sinaimg.cn/large/008i3skNgy1gtjvyd8pmaj60cv03ndfp02.jpg" alt="" style="display: block; margin: 0 auto 25px; max-width: 100%; border-radius: 4px"></figure>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">答案是<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">提前配置好的</strong>。当然这不是唯一的方式,DNS 也有可能通过 DHCP(<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">D</strong>ynamic <strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">H</strong>ost <strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">C</strong>onfiguration <strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">P</strong>rotocol) 动态分配的。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">例如,MacOS 中的 DNS 配置就长下面这样。</p>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://tva1.sinaimg.cn/large/008i3skNgy1gtjw4xx3n6j61110u0gmy02.jpg" alt="" style="display: block; margin: 0 auto 25px; max-width: 100%; border-radius: 4px"></figure>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">当然,你也可以通过命令行来查看、修改,地址在 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">/etc/resolv.conf</code> 。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">有了 DNS 服务器,那么你可能会觉得,接下来的事情就很简单了:</p>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://tva1.sinaimg.cn/large/008i3skNgy1gtk0divvc6j60cv049gli02.jpg" alt="" style="display: block; margin: 0 auto 25px; max-width: 100%; border-radius: 4px"></figure>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">我给你传个域名,你返给我对应的 IP 地址即可。那问题来了,现在互联网中有数万台的 DNS 服务器,我怎么知道数据在哪台服务器上?难道要一台一台的遍历请求这数万台服务器吗?</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">我相信你肯定没有感知到在浏览器中输入域名到页面展示会花费那么久,这也说明肯定不是一台一台服务器进行遍历的。</p>
<h3 data-tool="mdnice编辑器" style="margin-bottom: 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px; margin-top: 1.2em"><span style="background-image: url("https://files.mdnice.com/koala-3.png"); background-size: 100% 100%; background-repeat: no-repeat; display: inline-block; width: 16px; height: 15px; line-height: 15px; margin-bottom: -1px"></span><span class="prefix" style="display: none"></span><span class="content" style="font-size: 17px; font-weight: bold; display: inline-block; margin-left: 8px; color: rgba(72, 179, 120, 1)">域名的组成</span><span class="suffix" style="display: none"></span></h3>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">要了解 DNS 是如何对其进行优化的,我们需要先知道域名的组成部分。看到这,很可能你会这么想:</p>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(53, 179, 120, 1); font-size: 0.9em; overflow: auto; overflow-scrolling: touch; background: rgba(251, 249, 253, 1); color: rgba(106, 115, 125, 1); padding: 15px 20px; margin-bottom: 20px; margin-top: 20px; line-height: 27px; display: block">
<p style="padding: 0; margin: 0; line-height: 26px; font-size: 15px; color: rgba(89, 89, 89, 1)">啥组成?不就是一堆字符串吗?</p>
</blockquote>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">实际上,域名是有由不同的<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">域</strong>组成的,每个 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">.</code>隔开的部分就是一个<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">域</strong>。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">这里举个例子,假设我们分析的域名为 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">www.google.com</code> ,从我们平时写快递的收货地址的惯性思维来看,这个域的各个部分大小可能是这样的:</p>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(53, 179, 120, 1); font-size: 0.9em; overflow: auto; overflow-scrolling: touch; background: rgba(251, 249, 253, 1); color: rgba(106, 115, 125, 1); padding: 15px 20px; margin-bottom: 20px; margin-top: 20px; line-height: 27px; display: block">
<p style="padding: 0; margin: 0; line-height: 26px; font-size: 15px; color: rgba(89, 89, 89, 1)">www > google > com</p>
</blockquote>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">但是实际上并不是这样,而是:</p>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(53, 179, 120, 1); font-size: 0.9em; overflow: auto; overflow-scrolling: touch; background: rgba(251, 249, 253, 1); color: rgba(106, 115, 125, 1); padding: 15px 20px; margin-bottom: 20px; margin-top: 20px; line-height: 27px; display: block">
<p style="padding: 0; margin: 0; line-height: 26px; font-size: 15px; color: rgba(89, 89, 89, 1)">. > com > google > www</p>
</blockquote>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">你甚至发现,最大的还是个 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">.</code> 。其实完整的域名应该是 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">www.google.com.</code>,<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">.</code> 代表<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">根域</strong>,因为根域对于所有的域名来说,意义都一样,所以平时我们都把最后的点给省略了。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">每个域都有自己的专属名词:</p>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(53, 179, 120, 1); font-size: 0.9em; overflow: auto; overflow-scrolling: touch; background: rgba(251, 249, 253, 1); color: rgba(106, 115, 125, 1); padding: 15px 20px; margin-bottom: 20px; margin-top: 20px; line-height: 27px; display: block">
<p style="padding: 0; margin: 0; line-height: 26px; font-size: 15px; color: rgba(89, 89, 89, 1)">. > com > google > www</p>
<p style="padding: 0; margin: 0; line-height: 26px; font-size: 15px; color: rgba(89, 89, 89, 1)">根域 | 一级级域|二级域名|(子域名)|主机名</p>
</blockquote>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">当然,我们知道还可以针对二级域名再划分子域名,类似于 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">mail.google.com</code>。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">所以看到这,你应该能够理解<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">域名是由层次的</strong>这个概念了,我再举个比较的通俗的例子。</p>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(53, 179, 120, 1); font-size: 0.9em; overflow: auto; overflow-scrolling: touch; background: rgba(251, 249, 253, 1); color: rgba(106, 115, 125, 1); padding: 15px 20px; margin-bottom: 20px; margin-top: 20px; line-height: 27px; display: block">
<p style="padding: 0; margin: 0; line-height: 26px; font-size: 15px; color: rgba(89, 89, 89, 1)">com 公司的 google 部门的 www。https://mail.google.com/mail/u/0/#inbox</p>
</blockquote>
<h3 data-tool="mdnice编辑器" style="margin-bottom: 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px; margin-top: 1.2em"><span style="background-image: url("https://files.mdnice.com/koala-3.png"); background-size: 100% 100%; background-repeat: no-repeat; display: inline-block; width: 16px; height: 15px; line-height: 15px; margin-bottom: -1px"></span><span class="prefix" style="display: none"></span><span class="content" style="font-size: 17px; font-weight: bold; display: inline-block; margin-left: 8px; color: rgba(72, 179, 120, 1)">DNS 的分层</span><span class="suffix" style="display: none"></span></h3>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">了解完域名的分层之后,DNS 是如何优化<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">域名解析</strong>的问题就迎刃而解了,那就是——分层。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">DNS 服务器会将域名的数据<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">分布式</strong>的存储在各个 DNS 服务器上,但是同一个域的数据,会存储在同一台 DNS 服务器上,同一台 DNS 服务器可以存储多个域的数据。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">这么说可能会有些抽象,一图胜千言,其实就是这样:</p>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://tva1.sinaimg.cn/large/008i3skNgy1gtk0yks053j60lo085t8w02.jpg" alt="" style="display: block; margin: 0 auto 25px; max-width: 100%; border-radius: 4px"></figure>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">有了对数据的分层,那么查询数据就会很有<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">节奏感</strong>。</p>
<h3 data-tool="mdnice编辑器" style="margin-bottom: 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px; margin-top: 1.2em"><span style="background-image: url("https://files.mdnice.com/koala-3.png"); background-size: 100% 100%; background-repeat: no-repeat; display: inline-block; width: 16px; height: 15px; line-height: 15px; margin-bottom: -1px"></span><span class="prefix" style="display: none"></span><span class="content" style="font-size: 17px; font-weight: bold; display: inline-block; margin-left: 8px; color: rgba(72, 179, 120, 1)">查询域名数据</span><span class="suffix" style="display: none"></span></h3>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">一图胜千言,有了分层的机制,整个的查询过程就会长这样:</p>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://tva1.sinaimg.cn/large/008i3skNgy1gu22qwhda7j60od0iqaap02.jpg" alt="" style="display: block; margin: 0 auto 25px; max-width: 100%; border-radius: 4px"></figure>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">首先会去配置的 DNS 服务器中查询,这个其实一般都是本地或者内网中的 DNS 服务器。如果没有找到,就会去问<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">根域</strong>要,说哥们,我这里需要 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">www.google.com</code> 的 IP 地址。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">根域一看,我这里没有啊,但是我知道 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">com</code> 域的 DNS 服务器地址,他可能知道。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">然后 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">com</code> 域的 DNS 服务器一看,<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">www.google.com</code> 的 IP 地址我也不知道,但是我知道 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">google.com</code>域的 DNS服务器的地址,他可能知道,你再去问问他。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">就这样一路问下去,最终就能够找到 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">www.google.com</code> 所对应的 IP 地址了。</p>
<h3 data-tool="mdnice编辑器" style="margin-bottom: 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px; margin-top: 1.2em"><span style="background-image: url("https://files.mdnice.com/koala-3.png"); background-size: 100% 100%; background-repeat: no-repeat; display: inline-block; width: 16px; height: 15px; line-height: 15px; margin-bottom: -1px"></span><span class="prefix" style="display: none"></span><span class="content" style="font-size: 17px; font-weight: bold; display: inline-block; margin-left: 8px; color: rgba(72, 179, 120, 1)">根域名服务器</span><span class="suffix" style="display: none"></span></h3>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">看了上面的流程,可能你还是会有点疑问。因为去找 DNS 服务器查询 IP 地址时,初始的 DNS 的服务器的 IP 地址是走的本地计算机的配置的。那在分层查询时,我怎么知道有哪些<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">根服务器</strong>?以及我怎么知道这些根服务器的 IP 地址是啥?</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em"><strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">答案是内置</strong>。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">我们的设备,或者说所有能上网的设备都会内置根服务器的列表。总共有 13 台根 DNS 服务器,分别是<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">.root-servers.net</code> ,这些根服务器的地址根本不需要查询就能直接获取。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">当然,稍微想想也知道,13 台服务器是很难扛住全球互联网用户的请求的,实际上对于这 13 台服务器有很多的<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">镜像服务器</strong>。</p>
<h3 data-tool="mdnice编辑器" style="margin-bottom: 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px; margin-top: 1.2em"><span style="background-image: url("https://files.mdnice.com/koala-3.png"); background-size: 100% 100%; background-repeat: no-repeat; display: inline-block; width: 16px; height: 15px; line-height: 15px; margin-bottom: -1px"></span><span class="prefix" style="display: none"></span><span class="content" style="font-size: 17px; font-weight: bold; display: inline-block; margin-left: 8px; color: rgba(72, 179, 120, 1)">眼见为实</span><span class="suffix" style="display: none"></span></h3>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">说了这么多虚的概念,接下来我们通过 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">dig</code> 命令来实际操作一下。</p>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://tva1.sinaimg.cn/large/008i3skNgy1gtkp5lco16j60y40u0n0q02.jpg" alt="" style="display: block; margin: 0 auto 25px; max-width: 100%; border-radius: 4px"></figure>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">可以看到,在 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">QUESTION SECTION</code> 下的完整域名是 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">www.google.com.</code> 是带了<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">根域</strong>的,那后面的这个 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">IN</code> 和 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">A</code> 又是啥意思呢?</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">这是因为,在向 DNS 服务器查询请求的时候,<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">需要三个参数</strong>,分别是:</p>
<ol data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: decimal">
<li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500">域名(例如 www.google.com)</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500">网络类型(Class 设计之初,考虑到了多种网络并存的场景,但是目前实际上只有一种网络——互联网,所以该参数的值一直都会为 —— <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">IN</code> )</section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgba(1, 1, 1, 1); font-weight: 500">类型(例如 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">A</code> 表示 IP 地址,而 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">MX</code> 则表示邮件服务器的地址)</section></li></ol>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">而在 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">ANSWER SECTION</code> 中,则是 DNS 服务的响应结果,上图中显示了总有 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">6</code> 条 DNS 记录,并且在后面返回了其对应的 IP 地址。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">而其中的 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">69</code> 则是 TTL,单位是秒,代表了在 69 之内都不用再次发送请求了。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">最下面则是统计的信息,本次 DNS 查询所话费的时间,以及请求的 DNS 服务器的地址和端口。这个服务器地址是我们本机配置的 DNS 服务器的地址。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">眼尖的可能发现了,上图中根本没有设计到对<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">根服务器</strong>的请求。这是因为这个命令把这部分给<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">省略掉了</strong>,我们可能通过加上 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">+trace</code> 命令行参数来查看详细的<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">分级查询过程</strong>。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">这次我们以 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">www.36kr.com</code> 来作为例子。</p>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://tva1.sinaimg.cn/large/008i3skNgy1gtkpugc8crj60u0128n3m02.jpg" alt="" style="display: block; margin: 0 auto 25px; max-width: 100%; border-radius: 4px"></figure>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">可以看到,上图中列出了所有的根域名服务器,然后去找 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">com</code> 域要,然后再找 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">36kr.com</code> 域去要,最终是拿到了 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">www.36kr.com</code> 的 IP 地址。</p>
<h3 data-tool="mdnice编辑器" style="margin-bottom: 15px; padding: 0; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px; margin-top: 1.2em"><span style="background-image: url("https://files.mdnice.com/koala-3.png"); background-size: 100% 100%; background-repeat: no-repeat; display: inline-block; width: 16px; height: 15px; line-height: 15px; margin-bottom: -1px"></span><span class="prefix" style="display: none"></span><span class="content" style="font-size: 17px; font-weight: bold; display: inline-block; margin-left: 8px; color: rgba(72, 179, 120, 1)">缓存机制</span><span class="suffix" style="display: none"></span></h3>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">当然,如果每次都从根服务器开始往下找,明显是不合理的,因为域名和 IP 地址的对应关系本来变动的就不频繁,所以 DNS 服务器是都会将结果缓存的。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">并且,在下图中:</p>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://tva1.sinaimg.cn/large/008i3skNgy1gtk0yks053j60lo085t8w02.jpg" alt="" style="display: block; margin: 0 auto 25px; max-width: 100%; border-radius: 4px"></figure>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">我只写了一个 DNS 服务器中有同级别的域信息,但是实际上<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">不同层级的域信息可能存在于同一个 DNS 服务器</strong>,举个例子,<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">com</code> 域和 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(40, 202, 113, 1)">google.com</code> 域可能在同一台机器上。</p>
<p data-tool="mdnice编辑器" style="font-size: 16px; padding-bottom: 8px; margin: 0; padding-top: 1em; color: rgba(74, 74, 74, 1); line-height: 1.75em">但是,这个缓存是有有效期的。如果在这个有效期内 DNS 数据发生了变化,那么缓存中的数据就会不正确,此时需要手动的将 DNS 删除。</p>
<blockquote class="multiquote-1" data-tool="mdnice编辑器" style="border-top: none; border-right: none; border-bottom: none; border-left: 3px solid rgba(53, 179, 120, 1); font-size: 0.9em; overflow: auto; overflow-scrolling: touch; background: rgba(251, 249, 253, 1); color: rgba(106, 115, 125, 1); padding: 15px 20px; margin-bottom: 20px; margin-top: 20px; line-height: 27px; display: block">
<p style="padding: 0; margin: 0; line-height: 26px; font-size: 15px; color: rgba(89, 89, 89, 1)">本篇文章已放到我的 <strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">Github</strong> github.com/sh-blog 中,欢迎 Star。微信搜索关注【<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">SH的全栈笔记</strong>】,回复【<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">队列</strong>】获取MQ学习资料,包含基础概念解析和RocketMQ详细的源码解析,持续更新中。</p>
<p style="padding: 0; margin: 0; line-height: 26px; font-size: 15px; color: rgba(89, 89, 89, 1)">如果你觉得这篇文章对你有帮助,还麻烦<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">点个赞</strong>,<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">关个注</strong>,<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">分个享</strong>,<strong style="font-weight: bold; line-height: 1.75em; color: rgba(74, 74, 74, 1)">留个言</strong>。</p>
</blockquote>
<figure data-tool="mdnice编辑器" style="margin: 10px 0; display: flex; flex-direction: column; justify-content: center; align-items: center"><img src="https://tva1.sinaimg.cn/large/008i3skNgy1gtn1yw3t2cj61eg0hc40n02.jpg" alt="" style="display: block; margin: 0 auto 25px; max-width: 100%; border-radius: 4px"></figure>
</section><br><br>
来源:https://www.cnblogs.com/detectiveHLH/p/15213382.html
頁:
[1]