[kafka-producer-network-thread | producer-1] WARNorg.apache.kafka.clients.NetworkClient - [Producer clientId=producer-1] Error connecting to node xxxxx:9092 (id: 1 rack: null)java.net.UnknownHostException: xxxxxat java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:797)at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1519)at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1378)at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1306)at org.apache.kafka.clients.DefaultHostResolver.resolve(DefaultHostResolver.java:27)at org.apache.kafka.clients.ClientUtils.resolve(ClientUtils.java:111)at org.apache.kafka.clients.ClusterConnectionStates$NodeConnectionState.currentAddress(ClusterConnectionStates.java:512)at org.apache.kafka.clients.ClusterConnectionStates$NodeConnectionState.access$200(ClusterConnectionStates.java:466)at org.apache.kafka.clients.ClusterConnectionStates.currentAddress(ClusterConnectionStates.java:172)at org.apache.kafka.clients.NetworkClient.initiateConnect(NetworkClient.java:985)at org.apache.kafka.clients.NetworkClient.ready(NetworkClient.java:311)at org.apache.kafka.clients.producer.internals.Sender.sendProducerData(Sender.java:353)at org.apache.kafka.clients.producer.internals.Sender.runOnce(Sender.java:326)at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:242)at java.base/java.lang.Thread.run(Thread.java:829)

error日志:

[kafka-producer-network-thread | producer-1] ERROR org.apache.kafka.clients.producer.internals.Sender - [Producer clientId=producer-1] Uncaught error in kafka producer I/O thread: java.lang.IllegalStateException: No entry found for connection 1at org.apache.kafka.clients.ClusterConnectionStates.nodeState(ClusterConnectionStates.java:330)at org.apache.kafka.clients.ClusterConnectionStates.disconnected(ClusterConnectionStates.java:134)at org.apache.kafka.clients.NetworkClient.initiateConnect(NetworkClient.java:921)at org.apache.kafka.clients.NetworkClient.access$700(NetworkClient.java:67)at org.apache.kafka.clients.NetworkClient$DefaultMetadataUpdater.maybeUpdate(NetworkClient.java:1086)at org.apache.kafka.clients.NetworkClient$DefaultMetadataUpdater.maybeUpdate(NetworkClient.java:971)at org.apache.kafka.clients.NetworkClient.poll(NetworkClient.java:533)at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:309)at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:233)at java.base/java.lang.Thread.run(Thread.java:829) 

本质原因是kafka配置文件server.properites中默认监听hostname:

# Hostname and port the broker will advertise to producers and consumers. If not set, # it uses the value for "listeners" if configured.Otherwise, it will use the value# returned from java.net.InetAddress.getCanonicalHostName().#advertised.listeners=PLAINTEXT://your.host.name:9092 #advertised.listeners=PLAINTEXT://192.34.12.12:9092

两种解决方案

1.修改kafka服务的配置文件,改为监听ip,将 advertised.listeners 中的your.host.name 改为服务器的IP地址即可:

#advertised.listeners=PLAINTEXT://your.host.name:9092 advertised.listeners=PLAINTEXT://192.34.12.12:9092

2.修改producer客户端所在的电脑的host文件,在host文件中添加如下信息:

192.34.12.12 xxxxx