> dns.resolve('google.com',(error,addresses) => { console.error(error); console.log(addresses); }); QueryReqWrap { bindingName: 'queryA',callback: { [Function: asyncCallback] immediately: true },hostname: 'google.com',oncomplete: [Function: onresolve],domain: Domain { domain: null,_events: { error: [Function] },_eventsCount: 1,_maxListeners: undefined,members: [] } } > null [ '216.58.194.174' ]
和:
> dns.lookup('google.com',address,family) => { console.error(error); console.log(address); console.log(family); }); GetAddrInfoReqWrap { callback: { [Function: asyncCallback] immediately: true },family: 0,oncomplete: [Function: onlookup],members: [] } } > null 216.58.194.174 4
两者都返回相同的IPv4地址. dns.lookup()和dns.resolve()有什么区别?而且,每秒钟的大量请求更高效?
解决方法
dns
documentation已经描述了差异:
Although dns.lookup() and the varIoUs dns.resolve*()/dns.reverse() functions have the same goal of associating a network name with a network address (or vice versa),their behavior is quite different. These differences can have subtle but significant consequences on the behavior of Node.js programs.
dns.lookup()
Under the hood,dns.lookup() uses the same operating system facilities as most other programs. For instance,dns.lookup() will almost always resolve a given name the same way as the ping command. On most POSIX-like operating systems,the behavior of the dns.lookup() function can be modified by changing settings in nsswitch.conf(5) and/or resolv.conf(5),but note that changing these files will change the behavior of all other programs running on the same operating system.Though the call to dns.lookup() will be asynchronous from JavaScript’s perspective,it is implemented as a synchronous call to getaddrinfo(3) that runs on libuv’s threadpool. Because libuv’s threadpool has a fixed size,it means that if for whatever reason the call to getaddrinfo(3) takes a long time,other operations that could run on libuv’s threadpool (such as filesystem operations) will experience degraded performance. In order to mitigate this issue,one potential solution is to increase the size of libuv’s threadpool by setting the ‘UV_THREADPOOL_SIZE’ environment variable to a value greater than 4 (its current default value). For more information on libuv’s threadpool,see the official libuv documentation.
dns.resolve(),dns.resolve*() and dns.reverse()
These functions are implemented quite differently than dns.lookup(). They do not use getaddrinfo(3) and they always perform a DNS query on the network. This network communication is always done asynchronously,and does not use libuv’s threadpool.As a result,these functions cannot have the same negative impact on other processing that happens on libuv’s threadpool that dns.lookup() can have.
They do not use the same set of configuration files than what dns.lookup() uses. For instance,they do not use the configuration from /etc/hosts.
@H_301_35@就并发性而言,最好使用dns.resolve *(),因为这些请求不会在线程池中结束,而dns.lookup()请求会这样做,因为它们会调用通常阻塞的OS DNS解析器(虽然现在有某种异步接口 – 但它们并不一定在所有地方实现.
目前,node在内部使用dns.lookup()进行任何自动DNS解析,例如将主机名传递给http.request()时.