问题:
通过 简写主机名、完整主机名(hostname)、主机IP 向kafka broker 生产数据出错,具体错误如下,
- 错误1
[root@localhost ]# ./bin/kafka-console-producer.sh --broker-list node01:9092 --topic test_topic
[2020-12-1 16:25:33,972] WARN Removing server node01:9092 from bootstrap.servers as DNS resolution failed for node01 (org.apache.kafka.clients.ClientUtils)
org.apache.kafka.common.KafkaException: Failed to construct kafka producer
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:415)
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:287)
at kafka.producer.NewShinyProducer.<init>(BaseProducer.scala:40)
at kafka.tools.ConsoleProducer$.main(ConsoleProducer.scala:48)
at kafka.tools.ConsoleProducer.main(ConsoleProducer.scala)
Caused by: org.apache.kafka.common.config.ConfigException: No resolvable bootstrap urls given in bootstrap.servers
at org.apache.kafka.clients.ClientUtils.parseAndValidateAddresses(ClientUtils.java:64)
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:372)
... 4 more
- 错误2
[root@localhost ]# ./bin/kafka-console-producer.sh --broker-list node01.example.com:9092 --topic test_topic
[2020-12-1 16:27:59,444] WARN Removing server node01.example.com:9092 from bootstrap.servers as DNS resolution failed for node01.example.com (org.apache.kafka.clients.ClientUtils)
org.apache.kafka.common.KafkaException: Failed to construct kafka producer
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:415)
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:287)
at kafka.producer.NewShinyProducer.<init>(BaseProducer.scala:40)
at kafka.tools.ConsoleProducer$.main(ConsoleProducer.scala:48)
at kafka.tools.ConsoleProducer.main(ConsoleProducer.scala)
Caused by: org.apache.kafka.common.config.ConfigException: No resolvable bootstrap urls given in bootstrap.servers
at org.apache.kafka.clients.ClientUtils.parseAndValidateAddresses(ClientUtils.java:64)
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:372)
... 4 more
- 错误3
[root@localhost ]# ./bin/kafka-console-producer.sh --broker-list 192.168.1.1:9092 --topic test_topic
>hello world
>[2020-12-1 16:12:24,583] ERROR Error when sending message to topic test_topic with key: null, value: 11 bytes with error: (org.apache.kafka.clients.producer.internals.ErrorLoggingCallback)
org.apache.kafka.common.errors.TimeoutException: Expiring 1 record(s) for test_topic-0: 40094 ms has passed since batch creation plus linger time
原因:
这个问题主要是因为kafka客户端无法连接到kafka的broker节点。
错误1和错误2是因为本机无法解析简写主机名和完整主机名,需要配置/etc/hosts 文件, 将简写主机名和完整主机名对应的IP地址添加到hosts文件。
错误3是因为 没有将kafka broker 节点的 IP 和主机名添加到/etc/hosts 文件,同样需要将kafka broker 的主机名 对应的IP地址添加到hosts文件。
其实造成上面问题的原因是因为 kafka client 请求kafka ,kafka返回给 client 的是主机名而不是IP,而client主机上面如果没有配hosts的话就无法解析主机名导致上传消息失败。
解决:
方法1:修改Client 主机 hosts
打开/etc/hosts 文件 ,添加如下内容:
192.168.1.1 node01 node01.example.com
192.168.1.2 node02 node02.example.com
192.168.1.3 node03 node03.example.com
方法2:修改kafka配置
修改server.properties 文件,将advertised.host.name字段设置为broker 的ip
advertised.host.name= 192.168.0.1,192.168.0.2,192.168.0.3
注意:
hosts 中的主机名 一定要包含kafka broker 的主机名,这里是node01.example.com 、node02.example.com、 node03.example.com ,否则kafka也不能正常使用。
参考:
1.https://www.cnblogs.com/snifferhu/p/5102629.html
2.https://blog.csdn.net/u013686990/article/details/103026842