[英]Confluent_kafka Producer does not publish messages into topic
I tried to install Kafka on my Raspberry.我试图在我的 Raspberry 上安装 Kafka。 And test it on 'hello-kafka' topic:并在“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
Then I tried to check that server works from another machine.然后我试图检查服务器是否在另一台机器上工作。 Checking zookeeper:检查动物园管理员:
(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.
And Kafka:还有卡夫卡:
(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.
Then I wrote a Python script:然后我写了一个 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)
There is script output (no any prints):有脚本 output (没有任何打印):
(venv) $ python kafka-producer.py
(venv) $ python kafka-producer.py
(venv) $
And no new messages in Kafka: 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
Somebody can tell me what I am doing wrong?有人可以告诉我我做错了什么吗?
The correct fix is to update your broker configuration in server.properties
to set the advertised listener correctly.正确的解决方法是更新server.properties
中的代理配置以正确设置广告侦听器。 If your client cannot resolve raspberrypi
then change the advertised listener to something that your client can reach, ie the IP address:如果您的客户端无法解析raspberrypi
,则将广告侦听器更改为您的客户端可以访问的内容,即 IP 地址:
advertised.listeners=PLAINTEXT://192.168.1.10:9092
Changing the /etc/hosts
file on your client is a workaround that for a test project with a Raspberry Pi is fine, but as a general best practice should be discouraged (because the client will break as soon as it's moved to another machine which doesn't have the /etc/hosts
hack)更改客户端上的/etc/hosts
文件是一种解决方法,对于使用 Raspberry Pi 的测试项目来说很好,但作为一般的最佳实践应该不鼓励(因为客户端一旦移动到另一台没有'没有/etc/hosts
破解)
I turned on log and saw next message:我打开日志并看到下一条消息:
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)
Then I added to /etc/hosts on client machine next string:然后我添加到客户端机器上的 /etc/hosts 下一个字符串:
192.168.1.10 raspberrypi
And it completely fix this situation.它完全解决了这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.