diff --git clients/src/main/java/org/apache/kafka/clients/NetworkClient.java clients/src/main/java/org/apache/kafka/clients/NetworkClient.java index b7ae595..a58de7e 100644 --- clients/src/main/java/org/apache/kafka/clients/NetworkClient.java +++ clients/src/main/java/org/apache/kafka/clients/NetworkClient.java @@ -13,6 +13,7 @@ package org.apache.kafka.clients; import java.io.IOException; +import java.net.InetAddress; import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.List; @@ -494,12 +495,26 @@ public class NetworkClient implements KafkaClient { */ private void initiateConnect(Node node, long now) { try { - log.debug("Initiating connection to node {} at {}:{}.", node.id(), node.host(), node.port()); - this.connectionStates.connecting(node.id(), now); - selector.connect(node.id(), - new InetSocketAddress(node.host(), node.port()), - this.socketSendBuffer, - this.socketReceiveBuffer); + InetAddress[] hostAddresses = InetAddress.getAllByName(node.host()); + InetSocketAddress conn = null; + IOException lastException = null; + for (InetAddress addr : hostAddresses) { + if (conn == null) { + try { + lastException = null; + conn = new InetSocketAddress(addr, node.port()); + selector.connect(node.id(), + conn, + this.socketSendBuffer, + this.socketReceiveBuffer); + log.debug("Initiating connection to node {} at {}:{}.", node.id(), addr.toString(), node.port()); + this.connectionStates.connecting(node.id(), now); + } catch (IOException e) { + lastException = e; + } + } + } + if (lastException != null) throw lastException; } catch (IOException e) { /* attempt failed, we'll try again after the backoff */ connectionStates.disconnected(node.id());