[英]Confluent_kafka Producer does not publish messages into topic
我试图在我的 Raspberry 上安装 Kafka。 并在“hello-kafka”主题上进行测试:
~ $ /usr/local/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic hello-kafka
>Test message 1
>Test message 2
>Test message 3
>^Z
[4]+ Stopped /usr/local/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic hello-kafka
$ /usr/local/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic hello-kafka --from-beginning
Test message 1
Test message 2
Test message 3
^CProcessed a total of 3 messages
然后我试图检查服务器是否在另一台机器上工作。 检查动物园管理员:
(venv)$ telnet 192.168.1.10 2181
Trying 192.168.1.10...
Connected to 192.168.1.10.
Escape character is '^]'.
srvr
Zookeeper version: 3.6.0--b4c89dc7f6083829e18fae6e446907ae0b1f22d7, built on 02/25/2020 14:38 GMT
Latency min/avg/max: 0/0.8736/59
Received: 10146
Sent: 10149
Connections: 2
Outstanding: 0
Zxid: 0x96
Mode: standalone
Node count: 139
Connection closed by foreign host.
还有卡夫卡:
(venv) $ telnet 192.168.1.10 9092
Trying 192.168.1.10...
Connected to 192.168.1.10.
Escape character is '^]'.
tets
Connection closed by foreign host.
然后我写了一个 Python 脚本:
# -*- coding: utf-8 -*-
from confluent_kafka import Producer
def callback(err, msg):
if err is not None:
print(f'Failed to deliver message: {str(msg)}: {str(err)}')
else:
print(f'Message produced: {str(msg)}')
config = {
'bootstrap.servers': '192.168.1.10:9092'
}
producer = Producer(config)
producer.produce('hello-kafka', value=b"Hello from Python", callback=callback)
producer.poll(5)
有脚本 output (没有任何打印):
(venv) $ python kafka-producer.py
(venv) $ python kafka-producer.py
(venv) $
Kafka 中没有新消息:
$ /usr/local/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic hello-kafka --from-beginning
Test message 1
Test message 2
Test message 3
^CProcessed a total of 3 messages
$ ^C
有人可以告诉我我做错了什么吗?
正确的解决方法是更新server.properties
中的代理配置以正确设置广告侦听器。 如果您的客户端无法解析raspberrypi
,则将广告侦听器更改为您的客户端可以访问的内容,即 IP 地址:
advertised.listeners=PLAINTEXT://192.168.1.10:9092
更改客户端上的/etc/hosts
文件是一种解决方法,对于使用 Raspberry Pi 的测试项目来说很好,但作为一般的最佳实践应该不鼓励(因为客户端一旦移动到另一台没有'没有/etc/hosts
破解)
我打开日志并看到下一条消息:
WARNING:kafka.conn:DNS lookup failed for raspberrypi:9092, exception was [Errno 8] nodename nor servname provided, or not known. Is your advertised.listeners (called advertised.host.name before Kafka 9) correct and resolvable?
ERROR:kafka.conn:DNS lookup failed for raspberrypi:9092 (AddressFamily.AF_UNSPEC)
然后我添加到客户端机器上的 /etc/hosts 下一个字符串:
192.168.1.10 raspberrypi
它完全解决了这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.