DnsServer & DnsClient - RockyLOMO/rxlib GitHub Wiki

dns

Jdk底层dns解析方法在InetAddress.getAllByName()中,不管是FeignClient url 还是其它任何组件,域名解析都会走这个方法。Rxlib实现注入InetAddress.getAllByName()变更要查询的dnsServer地址,支持非53端口。这样类似FeignClient url可以写死,在dns层面解析对应ip,全局代码无入侵! 代码已贡献给Netty

    @Test
    public void dns() {
        final String domain = "devops.f-li.cn";
        final InetAddress hostResult = InetAddress.getByName("2.2.2.2");
        DnsServer server = new DnsServer(54);
        server.getCustomHosts().put(domain, hostResult.getAddress());

        //注入InetAddress.getAllByName()变更要查询的dnsServer的地址,支持非53端口
        Sockets.injectNameService(Sockets.parseEndpoint("127.0.0.1:54"));
//        System.out.println(HttpClient.godaddyDns("", "f-li.cn", "dd", "3.3.3.3"));

        List<InetAddress> r0 = DnsClient.inlandClient().resolveAll(domain);
        InetAddress[] r1 = InetAddress.getAllByName(domain);
        System.out.println(r0 + "\n" + toJsonArray(r1));
        assert !r0.get(0).equals(r1[0]);

        sleep(2000);
        DnsClient client = new DnsClient(Sockets.parseEndpoint("127.0.0.1:54"));
        InetAddress result = client.resolve(domain);
        assert result.equals(hostResult);
    }