Vertx DNS解析源码

最近在生产环境碰到过多次域名解析失败的问题,有时候还是客户windowns环境报障,是时候深入了解下Vertx内部的域名解析机制了。

1、Vertx使用DNS方法
import java.util.Arrays;

import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.dns.AddressResolverOptions;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpClientRequest;

public class DemoMain {
public static void main(String[] args) throws Exception {
AddressResolverOptions addressResolverOptions = new AddressResolverOptions();
addressResolverOptions.setNdots(1);
addressResolverOptions.setServers(Arrays.asList("8.8.8.8"));
addressResolverOptions.setSearchDomains(Arrays.asList(".com"));

VertxOptions vertxOptions = new VertxOptions();
vertxOptions.setAddressResolverOptions(addressResolverOptions);
Vertx vertx = Vertx.vertx(vertxOptions);
HttpClientOptions clientOp = new HttpClientOptions();
clientOp.setSsl(true);
clientOp.setTrustAll(true);
clientOp.setVerifyHost(false);
HttpClient httpClient = vertx.createHttpClient(clientOp);
HttpClientRequest req = httpClient.get(443, "www.baidu.com", "/index.html", resp -> {
  System.out.println(resp.statusCode());
  vertx.close();
});
req.end();

}
}
AddressResolverOptions 有几个重要属性
servers: 8.8.8.8,8.8.4.4 #对应Linux /etc/resolv.conf的nameserver,DNS服务器地址,支持配置多个,以逗号隔开
ndots: 1 #对应linux /etc/resolv.conf里面的options: ndots, 作用就是如果给的域名里面包含的点的个数少于该阈值,那么DNS解析的时候就会默认加上searchDomains的值,这个必须和searchDomains搭配使用,Linux默认为1,华为公有云PAAS(包含容器)默认是4
searchDomains: a,b,c #对应linux /etc/resolv.conf里面的search,和ndots搭配使用,如果当前域名的点个数少于设置值,解析时就会把这些值添加到域名后面一起解析,比如ndots设置的为4,当前域名为servicecomb.cn-north-1.myhwclouds.com,只有三个点,那么解析的时候就会自动加上servicecomb.cn-north-1.myhwclouds.com.a去解析,没解析出来在用servicecomb.cn-north-1.myhwclouds.com.b,直到能最后解析出来
optResourceEnabled: true #optional record is automatically included in DNS queries
cacheMinTimeToLive: 0 #最小缓存时间
cacheMaxTimeToLive: 10000 #最大缓存时间
cacheNegativeTimeToLive: 0 #DNS解析失败后,下次重试的等待时间
queryTimeout: 5000 #查询超时时间
maxQueries: 4 #查询次数
rdFlag: true #设置DNS递归查询
rotateServers: true #设置是否支持轮询,如果有多个域名服务器,轮训可以加快域名解析速度

2、代码解析过程
使用Vertx vertx = Vertx.vertx(vertxOptions),在VertxImpl构造方法里面会初始化this.addressResolver = new AddressResolver(this, options.getAddressResolverOptions());
2.1 io.vertx.core.impl.AddressResolver里面有个static块,会读取/etc/resolv.conf文件,解析得到ndots和rotate默认值,在构造方法内初始化
public AddressResolver(Vertx vertx, AddressResolverOptions options) {
this.provider = ResolverProvider.factory(vertx, options);
this.resolverGroup = provider.resolver(options);
this.vertx = vertx;
}

ResolverPr

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值