代码如下:
local resolver = require "resty.dns.resolver"
local resolver_ins, err = resolver:new{
nameservers = {"172.21.0.10"},
retrans = 5, -- 5 retransmissions on receive timeout
timeout = 2000, -- 2 sec
}
if not resolver_ins then
ngx.say("failed to instantiate the resolver: ", err)
return
end
-- query
local host = "test-0.test-svc"
local answers, err, tries = resolver_ins:query(host, nil, {})
if not answers then
ngx.log(ngx.ERR, "failed to query the DNS server: ", err)
ngx.log(ngx.ERR, "retry historie:\n ", table.concat(tries, "\n "))
return
end
if answers.errcode then
ngx.log(ngx.ERR, "server returned error code: ", answers.errcode,
", err: ", answers.errstr, ", hostname: ", host)
end