百度App网络深度优化系列《一》DNS优化
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">1、</span>前言</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">网络优化是客户端几大技术方向中公认的一个深度<span style="color: black;">行业</span>,<span style="color: black;">因此</span>百度App给<span style="color: black;">大众</span>带来网络深度优化系列<span style="color: black;">文案</span>,其中<span style="color: black;">包括</span>系列《一》DNS优化,系列《二》连接优化,系列《三》弱网优化,<span style="color: black;">期盼</span>对<span style="color: black;">大众</span>在网络方向的学习和实践有所<span style="color: black;">帮忙</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">百度起家于搜索,<span style="color: black;">全部</span><span style="color: black;">机构</span>的网络架构和<span style="color: black;">安排</span>都是基于标准的internet协议,<span style="color: black;">日前</span><span style="color: black;">已然</span>是全栈HTTPS,来到移动互联网时代后,总的<span style="color: black;">基本</span>架构不变,但在客户端上<span style="color: black;">必须</span>做<span style="color: black;">非常多</span>优化工作。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">DNS(Domain Name System),它的<span style="color: black;">功效</span>是<span style="color: black;">按照</span>域名查出IP<span style="color: black;">位置</span>,它是HTTP协议的前提,<span style="color: black;">仅有</span>将域名正确的解析成IP<span style="color: black;">位置</span>后,后面的HTTP流程<span style="color: black;">才可</span>进行,<span style="color: black;">因此</span><span style="color: black;">通常</span>做网络优化会首选优化DNS。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">2、</span>背景</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">DNS优化核心<span style="color: black;">必须</span><span style="color: black;">处理</span>的问题有两点:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【1】<span style="color: black;">因为</span>DNS劫持或故障<span style="color: black;">导致</span>的服务不可用,<span style="color: black;">从而</span>影响用户体验,影响<span style="color: black;">机构</span>的收入。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【2】<span style="color: black;">因为</span>DNS调度不准确<span style="color: black;">引起</span>的性能退化,<span style="color: black;">从而</span>影响用户体验。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">百度App承载着亿级流量,每年都会遇到运营商DNS劫持或运营商DNS故障,整体影响非常<span style="color: black;">欠好</span>,<span style="color: black;">因此</span>DNS优化刻不容缓,<span style="color: black;">经过</span>下图会更直观的<span style="color: black;">认识</span>运营商劫持或故障的原理。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/Ce6bSqXkduxBnkXgVcbfchK6TChPRUEnQKxrZZgn8hLgpyOV91Hg2MUYfWOCn1bWibgiahBqzLPgcWKbMrfxKqNw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span>运营商劫持或故障的原理</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">3、</span>HTTPDNS</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">既然<span style="color: black;">咱们</span>面临这么严峻的问题,<span style="color: black;">那样</span><span style="color: black;">咱们</span><span style="color: black;">怎样</span>优化DNS呢?答案<span style="color: black;">便是</span>HTTPDNS。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">大部分标准DNS都是基于UDP与DNS服务器交互的,HTTPDNS则是利用HTTP协议与DNS服务器交互,绕开了运营商的Local DNS服务,有效防止了域名劫持,<span style="color: black;">加强</span>域名解析效率,下图是HTTPDNS的原理。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/Ce6bSqXkduxBnkXgVcbfchK6TChPRUEnNPFqFqqzJB7Cn12yJwxNcp0VjDWPITEHa5e6Vjia1DvicmMiaOWiaxU1Aw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span>HTTPDNS原理</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">百度App HTTPDNS端上的实现是基于百度SYS团队的HTTPDNS服务,下图介绍了HTTPDNS的服务端<span style="color: black;">安排</span>结构。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/Ce6bSqXkduxBnkXgVcbfchK6TChPRUEnrfeOiaOompflsW4urd6GwJQNHvmBsHuWl1l9r5NZcVWdXNfBLoOqeoQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span>HTTPDNS<span style="color: black;">安排</span>结构</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">HTTPDNS服务是基于BGP接入的,BGP英文Border Gateway Protocol,即边界网关协议,是一种在自治系统之间动态的交换路由信息的路由协议,BGP<span style="color: black;">能够</span><span style="color: black;">按照</span>当前用户的运营商路由到百度服务点的对应集群上,<span style="color: black;">针对</span>第三方域名,服务点会<span style="color: black;">经过</span>百度<span style="color: black;">安排</span>在运营商的CDN节点向其他域名权威DNS发起<span style="color: black;">查找</span>,<span style="color: black;">查找</span>这个运营商下域名的最优IP。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">百度App独立实现了端的HTTPDNS SDK,下图介绍了端HTTPDNS的整体架构。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/Ce6bSqXkduxBnkXgVcbfchK6TChPRUEn0Yk8mXWY5dx3n6j05Oic1gpyVgofqD1rYreskYIIdypenfu8ZZR2PuA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span>端HTTPDNS的整体架构</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">DNS接口层:</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">DNS接口层<span style="color: black;">处理</span>的问题是屏蔽底层的细节,对外<span style="color: black;">供给</span>简单整洁的API,降低<span style="color: black;">运用</span>者的上手成本,<span style="color: black;">加强</span><span style="color: black;">研发</span>效率。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">DNS策略层:</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">DNS策略层<span style="color: black;">经过</span>多种策略的组合,使HTTPDNS服务在性能,稳定性,可用性上均保持较高的水准,下面讲解下<span style="color: black;">每一个</span>策略设计的初衷和<span style="color: black;">详细</span>实现。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;">1.容灾策略</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这是一个非常关键的策略,<span style="color: black;">重点</span><span style="color: black;">处理</span>HTTPDNS服务可用性的问题,实践证明,这个策略<span style="color: black;">帮忙</span>百度App在<span style="color: black;">反常</span><span style="color: black;">状况</span>下挽救回<span style="color: black;">非常多</span>流量。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【1】当HTTPDNS服务不可用并且本地<span style="color: black;">亦</span><span style="color: black;">无</span>缓存<span style="color: black;">或</span>缓存失效的时候,会触发降级策略,降级成运营商的localDNS<span style="color: black;">方法</span>,虽然存在运营商事故<span style="color: black;">或</span>劫持的<span style="color: black;">危害</span>,但<span style="color: black;">保证</span>了DNS服务的可用性。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【2】当HTTPDNS服务和localDNS服务双双不可用的<span style="color: black;">状况</span>下,会触发backup策略,<span style="color: black;">运用</span>端上的backup IP。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">什么是backup IP?backup IP是多组<span style="color: black;">按照</span>域名<span style="color: black;">归类</span>的IP列表,可云端动态更新,方便后续运维<span style="color: black;">朋友</span><span style="color: black;">调节</span>服务端的节点IP,不是所有域名都有对应的backup IP列表,<span style="color: black;">日前</span>百度App只能<span style="color: black;">保准</span>核心域名的可用性。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">既然是一组IP,便有<span style="color: black;">选择</span>问题,backup IP<span style="color: black;">选择</span>机制是<span style="color: black;">怎么样</span>的呢?<span style="color: black;">咱们</span>的中心思想<span style="color: black;">便是</span>要在端上利用最小的代价,并且<span style="color: black;">思虑</span>服务端的负载<span style="color: black;">平衡</span>,得到相对正确<span style="color: black;">或</span><span style="color: black;">恰当</span>的<span style="color: black;">选择</span>结果。<span style="color: black;">经过</span>运营商和地理信息,<span style="color: black;">能够</span><span style="color: black;">选取</span>一个相对较优的IP,但获取地理信息<span style="color: black;">必须</span>很大耗时,外加频次很高,代价很大,<span style="color: black;">因此</span><span style="color: black;">咱们</span><span style="color: black;">选取</span>了RR算法来代替上面的<span style="color: black;">办法</span>(RR算法是Round-Robin,轮询调度),<span style="color: black;">这般</span>客户端的代价降低到最小,服务端<span style="color: black;">亦</span>实现了负载<span style="color: black;">平衡</span>。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;">2.安全策略</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【1】HTTPDNS<span style="color: black;">处理</span>的核心问题<span style="color: black;">便是</span>安全,标准的DNS<span style="color: black;">查找</span>大部分是基于UDP的,但<span style="color: black;">亦</span>有基于TCP的,<span style="color: black;">倘若</span>UDP被封禁,就<span style="color: black;">必须</span><span style="color: black;">运用</span>TCP。不管是UDP还是TCP,安全性都是<span style="color: black;">无</span><span style="color: black;">保证</span>的,HTTPDNS<span style="color: black;">查找</span>是基于标准的HTTP协议,为了<span style="color: black;">保准</span>安全<span style="color: black;">咱们</span>会在HTTP上加一层TLS(安全传输层协议),这便是HTTPS。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【2】<span style="color: black;">处理</span>了传输层协议的安全性后,<span style="color: black;">咱们</span>要<span style="color: black;">处理</span>下域名解析的问题,上面<span style="color: black;">咱们</span><span style="color: black;">说到</span>HTTPDNS服务是基于BGP接入的,在端上采用VIP方式请求HTTPDNS数据(VIP即Virtual IP,VIP并<span style="color: black;">无</span>与某设备存在必定的绑定关系,会跟随主备切换之类的<span style="color: black;">状况</span><span style="color: black;">出现</span>而变换,VIP<span style="color: black;">供给</span>的服务是对应到某一台或若干台服务器的),既然请求原始数据<span style="color: black;">必须</span><span style="color: black;">运用</span>IP直连的方式,<span style="color: black;">那样</span>就摆脱了运营商localDNS的解析限制,<span style="color: black;">这般</span>即使运营商<span style="color: black;">显现</span>了故障<span style="color: black;">或</span>被劫持,都不会影响百度App的可用性。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;">3.任务调度策略</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">HTTPDNS服务<span style="color: black;">供给</span>了两类HTTP接口,用于请求最优域名结果。<span style="color: black;">第1</span>种是多域名接口,针对<span style="color: black;">区别</span>的<span style="color: black;">制品</span>线,下发<span style="color: black;">制品</span>线配置的域名,第二种是单域名接口,只返回你要<span style="color: black;">查找</span>的那个域名结果,<span style="color: black;">这般</span>的设计和标准的DNS<span style="color: black;">查找</span>基本是<span style="color: black;">同样</span>的,只<span style="color: black;">不外</span>是从UDP协议变<span style="color: black;">成为了</span>HTTP协议。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【1】多域名接口会在App冷<span style="color: black;">起步</span>和网络切换的时候请求一次,目的是在App的网络环境初始化<span style="color: black;">或</span>变化的时候预先获取域名结果,<span style="color: black;">这般</span><span style="color: black;">亦</span>会减少单域名接口的请求次数。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【2】单域名接口会在本地cache过期后,由用户的操作触发网络请求,<span style="color: black;">从而</span>做一次单域名请求,用户这次操作的DNS结果会降级成localDNS的结果,但在<span style="color: black;">无</span>过期的<span style="color: black;">状况</span>下,下次会返回HTTPDNS的结果。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;">4.IP<span style="color: black;">选择</span>策略</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">IP<span style="color: black;">选择</span>策略<span style="color: black;">处理</span>的核心问题是最优IP的<span style="color: black;">选择</span>,避免<span style="color: black;">由于</span>接入点的<span style="color: black;">选择</span>错误<span style="color: black;">导致</span>的跨运营商耗时。HTTPDNS服务会将最优IP<span style="color: black;">根据</span><span style="color: black;">次序</span>下发,客户端默认<span style="color: black;">选择</span><span style="color: black;">第1</span>个,<span style="color: black;">这儿</span><span style="color: black;">无</span>做客户端的连通性校验的<span style="color: black;">原由</span>,<span style="color: black;">重点</span>还是担心端上的性能问题,<span style="color: black;">不外</span>有容灾策略兜底,综合<span style="color: black;">评定</span>还是<span style="color: black;">能够</span>接受的。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;">5.缓存策略</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">大众</span><span style="color: black;">针对</span>DNS缓存并不陌生,它<span style="color: black;">重点</span>是为了<span style="color: black;">提高</span><span style="color: black;">拜访</span>效率,操作系统,网络库等都会做DNS缓存。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">DNS缓存中一个<span style="color: black;">要紧</span>的概念<span style="color: black;">便是</span>TTL(Time-To-Live),在localDNS中针对<span style="color: black;">区别</span>的域名,TTL的时间是不<span style="color: black;">同样</span>的,在HTTPDNS中这个值由服务端动态下发,百度App<span style="color: black;">日前</span>所有的域名TTL的配置是5分钟,过期后<span style="color: black;">倘若</span><span style="color: black;">无</span>新的IP将继续沿用老的IP,当然<span style="color: black;">亦</span><span style="color: black;">能够</span><span style="color: black;">选取</span>不沿用老的IP,而降级成localDNS的IP,<span style="color: black;">那样</span>这就取决于localDNS<span style="color: black;">针对</span>过期IP的处理。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;">6.命中率策略</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>HTTPDNS的命中率是100%,在<span style="color: black;">保准</span>HTTPDNS服务稳定<span style="color: black;">有效</span>的前提下,<span style="color: black;">咱们</span>就<span style="color: black;">能够</span>做到防劫持,<span style="color: black;">提高</span><span style="color: black;">精细</span>调度的能力。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【1】为了<span style="color: black;">提高</span>HTTPDNS的命中率,<span style="color: black;">咱们</span><span style="color: black;">选取</span><span style="color: black;">运用</span>多域名接口,在冷<span style="color: black;">起步</span>和网络切换的时候,批量拉取域名结果并缓存在本地,便于接下来的请求<span style="color: black;">运用</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【2】为了再一次<span style="color: black;">提高</span>HTTPDNS的命中率,当用户操作触发网络请求,获取域名对应的IP时,会提前进行本地过期时间判断,时间是60s,<span style="color: black;">倘若</span>过期,会发起单域名的请求并缓存起来,<span style="color: black;">这般</span>会<span style="color: black;">连续</span>延长域名结果的过期时间。本地过期时间与上面<span style="color: black;">说到</span>的TTL是客户端和服务端的双重过期时间,目的是在<span style="color: black;">反常</span><span style="color: black;">状况</span>下<span style="color: black;">能够</span>双重<span style="color: black;">保准</span>过期时间的准确性。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">基本</span>能力层:</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">基本</span>能力层<span style="color: black;">重点</span><span style="color: black;">供给</span>给DNS策略层所<span style="color: black;">必须</span>的<span style="color: black;">基本</span>能力,<span style="color: black;">包含</span>IPv4/IPv6协议栈探测的能力,数据传输的能力,缓存实现的能力,下面将讲解每种能力的<span style="color: black;">详细</span>实现</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;">1.IPv4/IPv6协议栈探测:</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">百度App的IPv6改造正在如火如荼的进行中,端上在HTTPDNS的IP<span style="color: black;">选择</span>上<span style="color: black;">怎样</span><span style="color: black;">晓得</span><span style="color: black;">日前</span>属于哪个协议栈<span style="color: black;">作为</span>关键性问题,并且这种判断<span style="color: black;">需求</span>性能极高,<span style="color: black;">由于</span>IP<span style="color: black;">选择</span>的频次实在是太高了。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span><span style="color: black;">选择</span>的<span style="color: black;">方法</span>是UDP Connect,<span style="color: black;">那样</span>何为UDP Connect?<span style="color: black;">大众</span>都<span style="color: black;">晓得</span>TCP是面向连接的,传输数据前客户端都要调用connect<span style="color: black;">办法</span><span style="color: black;">经过</span>三次握手<span style="color: black;">创立</span>连接,UDP是面向<span style="color: black;">没</span>连接的,<span style="color: black;">没</span>需<span style="color: black;">创立</span>连接便能收发数据,<span style="color: black;">然则</span><span style="color: black;">倘若</span><span style="color: black;">咱们</span>调用了UDP的connect<span style="color: black;">办法</span>会<span style="color: black;">出现</span>什么呢?当<span style="color: black;">咱们</span>调用UDP的connect<span style="color: black;">办法</span>时,系统会检测其端口<span style="color: black;">是不是</span>可用,<span style="color: black;">位置</span><span style="color: black;">是不是</span>正确,<span style="color: black;">而后</span>记录对端的IP<span style="color: black;">位置</span>和端口号,返回给调用者,<span style="color: black;">因此</span>UDP Connect不会像TCP Connect发起三次握手,<span style="color: black;">出现</span>网络真实损耗,UDP客户端<span style="color: black;">仅有</span>调用send<span style="color: black;">或</span>sendto<span style="color: black;">办法</span>后才会真正发起真实网络损耗。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/Ce6bSqXkduxBnkXgVcbfchK6TChPRUEnAVF0FcdLvR49CaUglRzzE9fcyibxKL5ppibzKn1wCzJvRh5xz7bq9Iug/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span>UDP Connect原理</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">有了UDP Connect的<span style="color: black;">基本</span><span style="color: black;">保证</span>,<span style="color: black;">咱们</span>在上层做了缓存机制,用来减少系统调用的损耗,<span style="color: black;">机会</span>上<span style="color: black;">日前</span>仅在冷<span style="color: black;">起步</span>和网络切换会触发探测,在同一种网络制式下探测一次基本<span style="color: black;">能够</span><span style="color: black;">保证</span>当前网络是IPv4栈还是IPv6栈。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">日前</span>百度App客户端<span style="color: black;">针对</span>IPv4/IPv6双栈的策略是保守的,仅在IPv6-only的<span style="color: black;">状况</span>下<span style="color: black;">运用</span>v6的IP,其余<span style="color: black;">运用</span>的都是v4的IP,双栈下的<span style="color: black;">方法</span>后续<span style="color: black;">必须</span>优化,业内<span style="color: black;">日前</span>标准的做法是happy eyeball算法,什么叫happy eyeball呢?<span style="color: black;">便是</span>不会<span style="color: black;">由于</span>IPv4或IPv6的故障问题,<span style="color: black;">引起</span>用户的眼球<span style="color: black;">始终</span>在等待加载<span style="color: black;">或</span>出错,这<span style="color: black;">便是</span>happy eyeball名字的由来。happy eyeball有v1版本RFC6555和v2版本RFC8305,前者是Cisco提出来的,后者是苹果提出来的。happy eyeball<span style="color: black;">处理</span>的核心问题是,<span style="color: black;">繁杂</span>环境下v4和v6 IP<span style="color: black;">选择</span>的问题,它是一套整体<span style="color: black;">处理</span><span style="color: black;">方法</span>,<span style="color: black;">针对</span>域名<span style="color: black;">查找</span>的处理,<span style="color: black;">位置</span>的排序,连接的尝试等方面均做出了规定,感兴趣的<span style="color: black;">朋友</span><span style="color: black;">能够</span>查看参考资料里的【5】和【6】。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;">2.数据传输:</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">数据传输<span style="color: black;">重点</span><span style="color: black;">供给</span>网络请求的能力和数据解析的能力。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【1】网络请求失败重试的机制,获取HTTPDNS结果的成功率会大大影响HTTPDNS的命中率,<span style="color: black;">因此</span>客户端会有一个三次重试的机制,<span style="color: black;">保证</span>成功率。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【2】数据解析<span style="color: black;">反常</span>的机制,<span style="color: black;">倘若</span>获取的HTTPDNS的结果存在<span style="color: black;">反常</span>,将不会覆盖端上的缓存。</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;">3.缓存实现:</h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">缓存的实现基本<span style="color: black;">能够</span>分为磁盘缓存和内存缓存,<span style="color: black;">针对</span>HTTPDNS的缓存场景,<span style="color: black;">咱们</span>是选其一还是都<span style="color: black;">选取</span>呢?百度App<span style="color: black;">选取</span>的是内存缓存,目的是防止<span style="color: black;">咱们</span>自己的服务<span style="color: black;">显现</span>问题,运维<span style="color: black;">朋友</span>在紧急<span style="color: black;">状况</span>下切换流量,<span style="color: black;">倘若</span>做了磁盘缓存,会<span style="color: black;">引起</span>百度App在重启后<span style="color: black;">亦</span>可能不可用,但这种问题会<span style="color: black;">引起</span>APP在冷<span style="color: black;">起步</span><span style="color: black;">时期</span>,HTTPDNS结果未返回前,还是存在故障<span style="color: black;">或</span>劫持的<span style="color: black;">危害</span>,综合<span style="color: black;">评定</span>来看<span style="color: black;">能够</span>接受,<span style="color: black;">倘若</span><span style="color: black;">显现</span>这种极端<span style="color: black;">状况</span>,影响的是冷<span style="color: black;">起步</span><span style="color: black;">周期</span>的<span style="color: black;">有些</span>请求,但只要HTTPDNS结果返回后便会恢复正常。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">4、</span>HTTPDNS的最佳实践</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">百度App<span style="color: black;">日前</span>客户端网络架构<span style="color: black;">因为</span>历史<span style="color: black;">原由</span>还未统一,<span style="color: black;">不外</span><span style="color: black;">咱们</span>正朝着这个<span style="color: black;">目的</span><span style="color: black;">奋斗</span>,下面着重介绍下HTTPDNS在Android和iOS网络架构中的位置及实践。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">HTTPDNS在Android网络架构的位置及实践</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">百度App的Android网络流量都在okhttp之上,上层进行了网络门面的封装,封装内部的实现细节和对外友好的API,供各个业务和<span style="color: black;">基本</span>模块<span style="color: black;">运用</span>,在okhttp上<span style="color: black;">咱们</span>扩展了DNS模块,<span style="color: black;">运用</span>HTTPDNS替换了原有的系统DNS。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/Ce6bSqXkduxBnkXgVcbfchK6TChPRUEn14zQAddYJ4q1ibmIVPmN7x61QV6GwwabLshfKIia4AYtcOcMW4zjIeKg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span>HTTPDNS在Android网络架构的位置</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;">HTTPDNS在iOS网络架构的位置及实践</h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">百度App的iOS网络流量都在cronet(chromium的net模块)之上,上层<span style="color: black;">咱们</span><span style="color: black;">运用</span>AOP的方式将cronet stack注入进URLSession里,<span style="color: black;">这般</span><span style="color: black;">咱们</span>就可以直接<span style="color: black;">运用</span>URLSession的API进行网络的操作<span style="color: black;">况且</span>更易于系统<span style="color: black;">守护</span>,在上层封装了网络门面,供各个业务和<span style="color: black;">基本</span>模块<span style="color: black;">运用</span>,在cronet内部<span style="color: black;">咱们</span>修改了DNS模块,除了原有的系统DNS<span style="color: black;">规律</span>外,还添加了HTTPDNS的<span style="color: black;">规律</span>。iOS上还有一部分流量是在原生URLSession上,<span style="color: black;">重点</span>是有些第三方业务<span style="color: black;">无</span><span style="color: black;">运用</span>cronet但还想单独<span style="color: black;">运用</span>HTTPDNS的能力,<span style="color: black;">因此</span>就有了下面的HTTPDNS封装层,<span style="color: black;">办法</span>是在上层直接将域名替换成IP,域名<span style="color: black;">针对</span>底层<span style="color: black;">非常多</span>机制是至关<span style="color: black;">要紧</span>的,<span style="color: black;">例如</span>https校验,cookie,重定向,SNI(Server Name Indication)等,<span style="color: black;">因此</span>将域名修改<span style="color: black;">成为了</span>IP直连后,<span style="color: black;">咱们</span>又处理了以上三种<span style="color: black;">状况</span>,<span style="color: black;">保准</span>请求的可用性。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/Ce6bSqXkduxBnkXgVcbfchK6TChPRUEnPDb5zbh6diaqyicdl5CZn3icEd4iazM30ibkCb6hBXaUWRHbutCRoOTUywA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span>HTTPDNS在iOS网络架构的位置</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">5、</span>收益</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">DNS优化的收益<span style="color: black;">重点</span>有两点,一是防止DNS的劫持(在出问题时<span style="color: black;">显出</span>尤为<span style="color: black;">要紧</span>),降低网络时延(在调度不准确的<span style="color: black;">状况</span>下,会增大网络的时延,降低用户的体验),这两点收益<span style="color: black;">必须</span>结合业务<span style="color: black;">来讲</span>,以百度App Feed业务为例,<span style="color: black;">第1</span>点上<span style="color: black;">咱们</span>取得了比<span style="color: black;">很强</span>的效果,iOS劫持率由0.12%降低到0.0002%,Android劫持率由0.25%降低到0.05%,第二点的收益不<span style="color: black;">显著</span>,<span style="color: black;">原由</span>在于Feed业务<span style="color: black;">重点</span><span style="color: black;">目的</span>群体在国内,百度在国内节点布局相对丰富,服务整体质量<span style="color: black;">亦</span>较高,即使<span style="color: black;">显现</span>调度不准确的<span style="color: black;">状况</span>,差值<span style="color: black;">亦</span>不会太大,但<span style="color: black;">倘若</span>在国外<span style="color: black;">状况</span>可能会差<span style="color: black;">非常多</span>。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">6、</span>结语</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">DNS优化是个<span style="color: black;">连续</span>性的<span style="color: black;">专题</span>,上面介绍的百度App的<span style="color: black;">有些</span>经验和做法并不见得完美,但<span style="color: black;">咱们</span>会<span style="color: black;">连续</span>深入的优化下去,为百度App的DNS能力保驾护航。最后感谢<span style="color: black;">大众</span>的辛苦阅读,<span style="color: black;">期盼</span>对你有所<span style="color: black;">帮忙</span>,后面会继续推出-百度App网络深度优化系列《二》连接优化,敬请期待。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">7、</span>个人心得</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">做为一个工程师,<span style="color: black;">怎样</span><span style="color: black;">才可</span>做好网络优化这件事情,是个值得<span style="color: black;">咱们</span>交流探讨的<span style="color: black;">专题</span>,个人认为应该从以下五方面入手。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【1】<span style="color: black;">基本</span>知识要<span style="color: black;">认识</span>学习,要夯实,网络<span style="color: black;">关联</span>的内容<span style="color: black;">非常多</span>,很杂,<span style="color: black;">很难</span>学习,啃过IETF发布的RFC的<span style="color: black;">朋友</span>应该深有感触。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【2】学会将看不见的网络变成看得见的,<span style="color: black;">非常多</span>自认为<span style="color: black;">针对</span>网络很<span style="color: black;">认识</span>的<span style="color: black;">朋友</span>,动不动就背诵tcp协议原理,拥塞<span style="color: black;">掌控</span>算法,滑动窗口<span style="color: black;">体积</span>等,但真正遇到线上问题,<span style="color: black;">没</span>从下手。<span style="color: black;">针对</span>客户端<span style="color: black;">朋友</span>,<span style="color: black;">咱们</span>在PC上要学会<span style="color: black;">运用</span>tcpdump和Wireshark等<span style="color: black;">工具</span>,适当<span style="color: black;">运用</span>Fiddler和Charles等<span style="color: black;">工具</span>,<span style="color: black;">非常多</span>时候电脑和手机的网络环境不见得一致,<span style="color: black;">因此</span>要在手机上<span style="color: black;">运用</span>iNetTools,Ping&DNS或终端<span style="color: black;">工具</span>。学会<span style="color: black;">运用</span><span style="color: black;">工具</span>后,要学着创造<span style="color: black;">区别</span>的网络环境,有<span style="color: black;">非常多</span><span style="color: black;">工具</span>能<span style="color: black;">帮忙</span>你完成这点,<span style="color: black;">例如</span>苹果的Network Link Conditioner,FaceBook的ATC(Augmented Traffic Control)等。具备以上两个场景后,你的<span style="color: black;">第1</span>条<span style="color: black;">贮存</span>就发挥了<span style="color: black;">功效</span>,你要能看懂握手过程,传输过程,<span style="color: black;">反常</span>断开过程等。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【3】有了以上两点的准备,接下来<span style="color: black;">必须</span>一个会<span style="color: black;">显现</span><span style="color: black;">各样</span>网络问题的平台,给你<span style="color: black;">累积</span>经验,让一个个高压下的线上问题锤炼你,<span style="color: black;">熬煎</span>你。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【4】网络优化是<span style="color: black;">必须</span>数据支撑的,但数据的采集和分析是<span style="color: black;">必须</span>经验的,有些数据一眼看下去<span style="color: black;">便是</span>不<span style="color: black;">可靠</span>的,有些数据怎么分析都是负向收益的,<span style="color: black;">通常</span><span style="color: black;">来讲</span>是有三重奏来对数据进行分析的,一,线下数据的采集和分析,得出正向收益,二,灰度数据的采集和分析,得出正向收益,三,线上数据的采集和分析,得出正向收益。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">【5】数据的正向收益,<span style="color: black;">不可</span>完全证明<span style="color: black;">提高</span>了用户的体验,<span style="color: black;">因此</span><span style="color: black;">非常多</span>时候<span style="color: black;">必须</span>针对特定场景,特定case来分析和优化,就算是<span style="color: black;">大众</span>公认做的很好的<span style="color: black;">微X</span>,<span style="color: black;">亦</span>不是在所有场景下都能<span style="color: black;">保准</span>体验上的最佳。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">8、</span>参考资料</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">https://chromium.googlesource.com/chromium/src/+/HEAD/docs/android_build_instructions.md</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">https://chromium.googlesource.com/chromium/src/+/HEAD/docs/ios/build_instructions.md</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">https://github.com/Tencent/mars</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">https://tools.ietf.org/html/rfc7858</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">https://tools.ietf.org/html/rfc6555</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">https://tools.ietf.org/html/rfc8305</p>
你的见解真是独到,让我受益匪浅。 你说得对,我们一起加油,未来可期。
页:
[1]