Confluent_kafka 生产者不将消息发布到主题中
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)
有脚本输出(无任何打印):
(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
有人可以告诉我我做错了什么吗?
我打开了日志并看到了下一条消息:
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
它完全解决了这种情况。
正确的解决方法是更新 server.properties
中的代理配置以正确设置播发的侦听器。如果您的客户端无法解析 raspberrypi
,则将通告的侦听器更改为您的客户端 可以 到达的内容,即 IP 地址:
advertised.listeners=PLAINTEXT://192.168.1.10:9092
更改客户端上的 /etc/hosts
文件是一种变通方法,对于具有 Raspberry Pi 的测试项目来说很好,但作为一般的最佳实践,应该不鼓励(因为客户端会中断一旦它被转移到另一台 没有 的机器上 /etc/hosts
hack)
我尝试在我的 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)
有脚本输出(无任何打印):
(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
有人可以告诉我我做错了什么吗?
我打开了日志并看到了下一条消息:
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
它完全解决了这种情况。
正确的解决方法是更新 server.properties
中的代理配置以正确设置播发的侦听器。如果您的客户端无法解析 raspberrypi
,则将通告的侦听器更改为您的客户端 可以 到达的内容,即 IP 地址:
advertised.listeners=PLAINTEXT://192.168.1.10:9092
更改客户端上的 /etc/hosts
文件是一种变通方法,对于具有 Raspberry Pi 的测试项目来说很好,但作为一般的最佳实践,应该不鼓励(因为客户端会中断一旦它被转移到另一台 没有 的机器上 /etc/hosts
hack)