Vertx HttpClient getNow not working

我怕爱的太早我们不能终老 提交于 2020-05-13 14:47:09

问题


I have problem with vertx HttpClient. Here's code which shows that tests GET using vertx and plain java.

    Vertx vertx = Vertx.vertx();
    HttpClientOptions options = new HttpClientOptions()
            .setTrustAll(true)
            .setSsl(false)
            .setDefaultPort(80)
            .setProtocolVersion(HttpVersion.HTTP_1_1)
            .setLogActivity(true);
    HttpClient client = vertx.createHttpClient(options);

    client.getNow("google.com", "/", response -> {
        System.out.println("Received response with status code " + response.statusCode());
    });
    System.out.println(getHTML("http://google.com"));

Where getHTML() is from here: How do I do a HTTP GET in Java?

This is my output:

<!doctype html><html... etc <- correct output from plain java
Feb 08, 2017 11:31:21 AM io.vertx.core.http.impl.HttpClientRequestImpl
SEVERE: java.net.UnknownHostException: failed to resolve 'google.com'.     Exceeded max queries per resolve 3 

But vertx can't connect. What's wrong here? I'm not using any proxy.


回答1:


For reference: a solution, as described in this question and in tsegismont's comment here, is to set the flag vertx.disableDnsResolver to true:

-Dvertx.disableDnsResolver=true

in order to fall back to the JVM DNS resolver as explained here:

sometimes it can be desirable to use the JVM built-in resolver, the JVM system property -Dvertx.disableDnsResolver=true activates this behavior

I observed this DNS resolution issue with a redis client in a kubernetes environment.




回答2:


I had this issue, what caused it for me was stale DNS servers being picked up by the Java runtime, i.e. servers registered for a network the machine was no longer connected to. The issue is first in the Sun JNDI implementation, it also exists in Netty which uses JNDI to bootstrap its list of name servers on most platforms, then finally shows up in VertX.

I think a good place to fix this would be in the Netty layer where the set of default DNS servers is bootstrapped. I have raised a ticket with the Netty project so we'll see if they agree with me! Here is the Netty ticket

In the mean time a fairly basic workaround is to filter the default DNS servers detected by Netty, based on whether they are reachable or not. Here is a code Sample in Kotlin to apply before constructing the main VertX instance.

// The default set of name servers provided by JNDI can contain stale entries
// This default set is picked up by Netty and in turn by VertX
// To work around this, we filter for only reachable name servers on startup

val nameServers = DefaultDnsServerAddressStreamProvider.defaultAddressList()

val reachableNameServers = nameServers.stream()
        .filter {ns -> ns.address.isReachable(NS_REACHABLE_TIMEOUT)}
        .map {ns -> ns.address.hostAddress}
        .collect(Collectors.toList())

if (reachableNameServers.size == 0)
    throw StartupException("There are no reachable name servers available")

val opts = VertxOptions()
opts.addressResolverOptions.servers = reachableNameServers

// The primary Vertx instance
val vertx = Vertx.vertx(opts)

A little more detail in case it is helpful. I have a company machine, which at some point was connected to the company network by a physical cable. Details of the company's internal name servers were set up by DHCP on the physical interface. Using the wireless interface at home, DNS for the wireless interface gets set to my home DNS while the config for the physical interface is not updated. This is fine since that device is not active, ipconfig /all does not show the internal company DNS servers. However, looking in the registry they are still there:

Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces

They get picked up by the JNDI mechanism, which feeds Netty and in turn VertX. Since they are not reachable from my home location, DNS resolution fails. I can imagine this home/office situation is not unique to me! I don't know whether something similar could occur with multiple virtual interfaces on containers or VMs, it could be worth looking at if you are having problems.




回答3:


Here is the sample code which works for me.

public class TemplVerticle extends HttpVerticle {

public static void main(String[] args) {
    Vertx vertx = Vertx.vertx();

    // Create the web client and enable SSL/TLS with a trust store
    WebClient client = WebClient.create(vertx,
            new WebClientOptions()
                    .setSsl(true)
                    .setTrustAll(true)
                    .setDefaultPort(443)
                    .setKeepAlive(true)
                    .setDefaultHost("www.w3schools.com")

    );


    client.get("www.w3schools.com")

            .as(BodyCodec.string())
            .send(ar -> {
                if (ar.succeeded()) {
                    HttpResponse<String> response = ar.result();
                    System.out.println("Got HTTP response body");
                    System.out.println(response.body().toString());

                } else {
                    ar.cause().printStackTrace();
                }
            });
}

}




回答4:


Try using web client instead of httpclient, here you have an example (with rx):

private val client: WebClient = WebClient.create(vertx, WebClientOptions()
        .setSsl(true)
        .setTrustAll(true)
        .setDefaultPort(443)
        .setKeepAlive(true)
)

open fun <T> get(uri: String, marshaller: Class<T>): Single<T> {
    return client.getAbs(host + uri).rxSend()
            .map { extractJson(it, uri, marshaller) }
}

Another option is to use getAbs.



来源:https://stackoverflow.com/questions/42110790/vertx-httpclient-getnow-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!