域名解析和缓存

name resolve

当浏览器访问某个网站域名或者应用服务通过域名方式访问API接口的时候,需要用IP和port建立TCP连接或者复用底层连接,IP地址的获取依赖对域名的解析,完成解析的角色称为域名解析器(dns resolver)。解析的大致过程就是检查cache是否有该记录,本地hosts文件是否有,都没有命中就查询dns server进行CNAME和A记录的查询。在linux系统下,dns server的IP一般在/etc/resolv.conf文件中。
域名解析常用dig命令,以及在https://www.whatsmydns.net/进行域名解析测试。

dns resolver cache(dns cache)

考虑到域名IP地址不是经常变动,减少查询dns的冗余,并显著降低高QPS应用服务查询dns的压力(最后一节有benchmark对比),需要对dns信息进行缓存。因为软件应用不同、开发语言不同、操作系统不同,dns resolver的实现和封装也不同,会遇到不同的层面的cache。比如windows的dns resolver会有cache,linux默认不缓存;go语言可以选择cgo或者自己实现的dns resolver;chrome浏览器也会有自己的cache。
dns cache除了好处以外,也带来了其他问题。比如dns cache可能被恶意病毒修改,将真实IP改成钓鱼网站的IP,对用户进行诱导和钓鱼。还有在服务发现的这种特定场景下,dns cache是不被允许的,会出现IP更新不及时导致API流量的损失和错误,例如部署上线或者宕机,相比之下,运维响应的时长会造成更大的损失。但为了解决这个问题,在client和server端中间增加一层代理,dns记录指向这个代理。如图:


image.png

代理职责一般有:

  • 服务发现,帮助客户端发现有多少服务实例。
  • 健康检查,对服务实例进行健康检查,及时剔除不健康实例,避免流量的损失。并通过此机制完成服务的无损上线。
  • 流量负载均衡。

代理一般分为:

类别 名称
四层负载均衡 LVS F5
七层负载均衡 nginx openresty

四层代理对外暴露的IP一般称为虚IP(VIP)

go name resolve

  • net/net.go package comment:
Name Resolution

The method for resolving domain names, whether indirectly with functions like Dial
or directly with functions like LookupHost and LookupAddr, varies by operating system.

On Unix systems, the resolver has two options for resolving names.
It can use a pure Go resolver that sends DNS requests directly to the servers
listed in /etc/resolv.conf, or it can use a cgo-based resolver that calls C
library routines such as getaddrinfo and getnameinfo.

By default the pure Go resolver is used, because a blocked DNS request consumes
only a goroutine, while a blocked C call consumes an operating system thread.
When cgo is available, the cgo-based resolver is used instead under a variety of
conditions: on systems that do not let programs make direct DNS requests (OS X),
when the LOCALDOMAIN environment variable is present (even if empty),
when the RES_OPTIONS or HOSTALIASES environment variable is non-empty,
when the ASR_CONFIG environment variable is non-empty (OpenBSD only),
when /etc/resolv.conf or /etc/nsswitch.conf specify the use of features that the
Go resolver does not implement, and when the name being looked up ends in .local
or is an mDNS name.

The resolver decision can be overridden by setting the netdns value of the
GODEBUG environment variable (see package runtime) to go or cgo, as in:

    export GODEBUG=netdns=go    # force pure Go resolver
    export GODEBUG=netdns=cgo   # force cgo resolver

The decision can also be forced while building the Go source tree
by setting the netgo or netcgo build tag.

A numeric netdns setting, as in GODEBUG=netdns=1, causes the resolver
to print debugging information about its decisions.
To force a particular resolver while also printing debugging information,
join the two settings by a plus sign, as in GODEBUG=netdns=go+1.

On Plan 9, the resolver always accesses /net/cs and /net/dns.

On Windows, the resolver always uses C library functions, such as GetAddrInfo and DnsQuery.
  • go sdk做域名解析有两种策略:
export GODEBUG=netdns=go    # force pure Go resolver
export GODEBUG=netdns=cgo   # force cgo resolver
  • 写个demo测试一下有无cache的性能区别:
    example.go
package example

import (
    "net"
)

func queryDNS(name string) []net.IP {
    ips, err := net.LookupIP(name)
    if err != nil {
        panic(err)
    }
    return ips
}

func QueryDNSNoCache() []net.IP {
    return queryDNS("music.163.com")
}

func QueryDNSWithCache() []net.IP {
    return queryDNS("music.163.com")
}

example_test.go

package example

import (
    "os"
    "testing"
)

func BenchmarkQueryDNSNoCache(b *testing.B) {
    _ = os.Setenv("GODEBUG", "netdns=go+1")
    for i := 0; i < b.N; i++ {
        QueryDNSNoCache()
    }
}

func BenchmarkQueryDNSWithCache(b *testing.B) {
    _ = os.Setenv("GODEBUG", "netdns=cgo+1")
    for i := 0; i < b.N; i++ {
        QueryDNSWithCache()
    }
}

性能对比:


image.png

从对比中可看出:go的pure resolver因没有cache和网络不稳定的因素,总耗时较多。而cgo的resolver比较稳定且耗时较低。

dns cache查看和清除

darwin

sudo killall -HUP mDNSResponder

linux(unix)

linux或类unix系统是没有操作系统级别的dns cache。除非安装了dnsmasq或者
nscd(Name Service Caching Daemon),并开启。

sudo strings /var/cache/nscd/hosts
/etc/init.d/nscd stop
/etc/init.d/nscd start
sudo nscd -g

windows

ipconfig /displaydns
ipconfig /flushdns

chrome

chrome://net-internals/#dns
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,651评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,468评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,931评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,218评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,234评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,198评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,084评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,926评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,341评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,563评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,731评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,430评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,036评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,676评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,829评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,743评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,629评论 2 354

推荐阅读更多精彩内容

  • 非常好的文章,怕博主删除,再也找不到这么好的文章了,所以复制了一份,博主是2016年写,但是是到现在为止看到的,思...
    吭声_cfdc阅读 1,652评论 0 4
  • 1. 概述 在网络环境中一般用户只需要在浏览器中输入url如www.sunny.com就可以到对应服务器获取相应的...
    ghbsunny阅读 2,888评论 0 7
  • 在使用consul做docker容器服务化的过程中,使用到了dnsmasq做DNS请求转发,于是研究了下DNS协议...
    __七把刀__阅读 3,993评论 2 13
  • https://nodejs.org/api/documentation.html 工具模块 Assert 测试 ...
    KeKeMars阅读 6,330评论 0 6
  • 上上个星期去益阳和正在创业的同学吃饭聊天,他推荐了一部电视剧——前段时间热播的《人民的名义》。刚开始我摆手拒绝,打...
    缓步向前阅读 296评论 0 0