@Override public String lookupIPAddress(final String dnsServer, final String host) { final DnsClient client = vertx.createDnsClient(53, dnsServer); final CompletableFuture<String> future = new CompletableFuture<>(); client.lookup("vertx.io", result -> { if (result.succeeded()) { future.complete(result.result()); } else { future.completeExceptionally(result.cause()); } }); try { return future.get(); } catch (final InterruptedException | ExecutionException ex) { throw new RuntimeException(ex); } }
/** * Load the services nodes by its "${SRV.service}${SRV.name}". * * @param callback callback * @param serviceURL serviceURL */ public void loadServiceEndpointsByDNSService(final Callback<List<EndpointDefinition>> callback, final String serviceURL) { final DnsClient dnsClient = dnsClientProvider.get(); dnsClient.resolveSRV(serviceURL, event -> { if (event.succeeded()) { if (debug) logger.debug("loadServiceEndpointsByDNSService SUCCESS serviceURL={} ", serviceURL); callback.resolve(convertEndpoints(event.result())); } else { Throwable error = event.cause(); logger.info("loadServiceEndpointsByDNSService FAILURE " + serviceURL, error); attemptRecover(callback, serviceURL, error); } } ); }
private void attemptRecover(final Callback<List<EndpointDefinition>> callback, final String serviceURL, final Throwable error) { final DnsClient dnsClient2 = dnsClientProvider.getIfErrors(); dnsClient2.resolveSRV(serviceURL, event -> { if (event.succeeded()) { if (debug) logger.debug("loadServiceEndpointsByDNSService FAIL OVER SUCCESS serviceURL={} ", serviceURL); callback.resolve(convertEndpoints(event.result())); } else { logger.info("loadServiceEndpointsByDNSService FAIL OVER FAILURE " + serviceURL, event.cause()); //Send the first failure callback.onError(error); } }); }
@Test public void testDnsClient() throws Exception { final DnsClient client = vertx.createDnsClient(53, "8.8.8.8"); final CompletableFuture<String> future = new CompletableFuture<>(); client.lookup("vertx.io", result -> { if (result.succeeded()) { future.complete(result.result()); } else { future.completeExceptionally(result.cause()); } }); log.info(future.get()); }
/** * Get a DNS client. * * @return dns client. */ @Override public DnsClient get() { final URI uri = addressList.get(index); try { if (debug) logger.debug("DnsClient.get port {} host {}", uri.getPort(), uri.getHost()); return vertx.createDnsClient(uri.getPort(), uri.getHost()); } catch (Exception ex) { logger.error("DnsClient.get EXCEPTION ", ex); logger.error("DnsClient.get EXCEPTION port {} host {}", uri.getPort(), uri.getHost()); return getIfErrors(); } }
@Override public DnsClient getIfErrors() { nextAddress(); final URI uri = addressList.get(index); if (debug) logger.debug("DnsClient.get FAIL OVER port {} host {}", uri.getPort(), uri.getHost()); return vertx.createDnsClient(uri.getPort(), uri.getHost()); }
/** * Supply an instance of DnsClient. * * @return DnsClient. */ @Override public DnsClient get() { if (debug) logger.debug("DnsSingleClientSupplier.get()::host = {}, port = {}", host, port); return vertx.createDnsClient(port, host); }
@Override public DnsClient createDnsClient(int port, String host) { return vertx.createDnsClient(port, host); }
default DnsClient getIfErrors() { return get(); }
/** * Sets the DNS client to use for checking availability of an <em>Authentication</em> service. * <p> * If not set, the vert.x instance's address resolver is used instead. * * @param dnsClient The client. * @throws NullPointerException if client is {@code null}. */ @Autowired(required = false) public void setDnsClient(final DnsClient dnsClient) { this.dnsClient = Objects.requireNonNull(dnsClient); }