背景描述:
在虚拟机集群上搭建了zk、kafka(version:0.11)集群,在控制台上测试生产消费时正常。而后在windows端使用java api向虚拟机消费端生产发送数据,发现程序正常运行起来过了大概十秒后结束( exit code 0),但在虚拟机开启的消费者端并没有收到数据。经过几次检查,确保通信没问题,在java api端给未创建的topic发送数据时还可以发现在kafka集群端主动创建了这个topic。但发送的数据在虚拟机的控制台消费者端就是没有打印(测试将数据打印到控制台上的)。
相关代码:

properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "chdp01:9092,chdp02:9092,chdp03:9092");

   
   

打开log4j debug发现以下错误:

2019-09-17 15:58:26,966 DEBUG [org.apache.kafka.clients.NetworkClient] - Initiating connection to node 1 at chdp11:9092.
2019-09-17 15:58:26,966 DEBUG [org.apache.kafka.clients.NetworkClient] - Error connecting to node 1 at chdp11:9092:
java.io.IOException: Can't resolve address: chdp11:9092
	at org.apache.kafka.common.network.Selector.connect(Selector.java:195)
	at org.apache.kafka.clients.NetworkClient.initiateConnect(NetworkClient.java:762)
	at org.apache.kafka.clients.NetworkClient.ready(NetworkClient.java:224)
	at org.apache.kafka.clients.producer.internals.Sender.sendProducerData(Sender.java:247)
	at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:223)
	at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:175)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.nio.channels.UnresolvedAddressException
	at sun.nio.ch.Net.checkAddress(Net.java:101)
	at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:622)
	at org.apache.kafka.common.network.Selector.connect(Selector.java:192)
	... 6 more
2019-09-17 15:58:26,966 DEBUG [org.apache.kafka.clients.NetworkClient] - Initialize connection to node 3 for sending metadata request
2019-09-17 15:58:26,966 DEBUG [org.apache.kafka.clients.NetworkClient] - Initiating connection to node 3 at chdp13:9092.
2019-09-17 15:58:26,966 DEBUG [org.apache.kafka.clients.NetworkClient] - Error connecting to node 3 at chdp13:9092:
java.io.IOException: Can't resolve address: chdp13:9092
	at org.apache.kafka.common.network.Selector.connect(Selector.java:195)
	at org.apache.kafka.clients.NetworkClient.initiateConnect(NetworkClient.java:762)
	at org.apache.kafka.clients.NetworkClient.access$600(NetworkClient.java:60)
	at org.apache.kafka.clients.NetworkClient$DefaultMetadataUpdater.maybeUpdate(NetworkClient.java:906)
	at org.apache.kafka.clients.NetworkClient$DefaultMetadataUpdater.maybeUpdate(NetworkClient.java:817)
	at org.apache.kafka.clients.NetworkClient.poll(NetworkClient.java:430)
	at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:224)
	at org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:175)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.nio.channels.UnresolvedAddressException
	at sun.nio.ch.Net.checkAddress(Net.java:101)
	at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:622)
	at org.apache.kafka.common.network.Selector.connect(Selector.java:192)
	... 8 more

抽取主要几行:

Error connecting to node 1 at chdp11:9092:
java.io.IOException: Can't resolve address: chdp11:9092
。。。
Caused by: java.nio.channels.UnresolvedAddressException

再来看一下windows与虚拟机上的相关dns配置

虚拟机dns(/etc/hosts)

192.168.11.11 chdp11
192.168.11.12 chdp12
192.168.11.13 chdp13

windows(C:\Windows\System32\drivers\etc\hosts)

192.168.11.11	chdp01
192.168.11.12	chdp02
192.168.11.13	chdp03

 
 

仔细看看这两个dns配置的ip与域名。
因为日常学习测试的原因在自己电脑上搭建了好几套虚拟机,每套虚拟机的命名都不一样但ip一样(为了避免每次去修改windows host文件这里偷了个懒)。
很明显了,在java api运行建立tcp连接时(三次握手)还会去查看运用kafka服务节点所在的dns,而这个dns在windows本地是没有配置的,故而就出现了UnresolvedAddressException这个让人看起来有点奇怪的异常(我在windows通信用的dns没这个呀,这不是在虚拟机上配置的dns吗。。。)

此外org.apache.kafka.common.errors.TimeoutException:也可能有该问题引起。
将windows上的dns映射域名修改为与虚拟机一致问题解决

192.168.11.11	chdp11
192.168.11.12	chdp12
192.168.11.13	chdp13

想要具体了解其中原因或许可以从这位大佬的博客得到启发:https://www.cnblogs.com/huxi2b/p/10112228.html

Logo

Kafka开源项目指南提供详尽教程,助开发者掌握其架构、配置和使用,实现高效数据流管理和实时处理。它高性能、可扩展,适合日志收集和实时数据处理,通过持久化保障数据安全,是企业大数据生态系统的核心。

更多推荐