简体   繁体   English

在 WSL2 Ubuntu 中使用 Python 的 Kafka 消费者/生产者

[英]Kafka consumer/producer with Python in WSL2 Ubuntu

This is a follow-up question from this thread .这是此线程的后续问题。

As per advised from the thread, the possible cause for why my Python code is not working is because I was to connect to a remote server in WSL2.根据线程的建议,我的 Python 代码不起作用的可能原因是因为我要连接到 WSL2 中的远程服务器。 And there could be unknown issues with WSL2 Ubuntu. WSL2 Ubuntu 可能存在未知问题。

So I am testing that hypothesis with the following two approaches of communicating within WLS2 Ubuntu locally (ie via localhost:9092 ):因此,我正在使用以下两种在 WLS2 Ubuntu 本地(即通过localhost:9092 )内进行通信的方法来测试该假设:

Note that, for both approaches below, I already have zookeeper running in one terminal ( T1 ) with:请注意,对于以下两种方法,我已经在一个终端( T1 )中运行了 zookeeper:

bin/zookeeper-server-start.sh config/zookeeper.properties

Approach 1 : communicating with producer/consumer in console as described by the following commands (taken from this tutorial )方法 1 :在控制台中与生产者/消费者通信,如以下命令所述(取自本教程

Step 1 : create a topic TutorialTopic (in terminal T2 )第 1 步:创建主题TutorialTopic (在终端T2中)

~/kafka/bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic TutorialTopic

Step 2 : produce a message in a terminal T3步骤2 :在终端T3中产生一条消息

echo "Hello, World" | ~/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic TutorialTopic

Step 3 : consume the message in another terminal T4第三步:在另一个终端T4消费消息

~/kafka/bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic TutorialTopic --from-beginning

Outcome : I see a message Hello, World in terminal T4结果:我在终端 T4 中看到一条消息Hello, World

Approach 2 : communicate through two Python modules, consumer.py and producer.py as in this tutorial :方法 2 :通过两个 Python 模块进行通信, consumer.pyproducer.py ,如本教程所示

Step 1 : create a topic sample in terminal T5步骤 1 :在终端T5中创建主题sample

bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic sample

Step 2 : run the producer module in terminal T6步骤2 :在终端T6运行生产者模块

producer.py
from kafka import KafkaProducer

producer = KafkaProducer(bootstrap_servers='localhost:9092')
producer.send('sample', value='Hello, World!')

Step 3 : consume that message in terminal T7第 3 步:在终端T7中使用该消息

consumer.py
from kafka import KafkaConsumer

consumer = KafkaConsumer('sample', bootstrap_servers=['localhost: 9092'])

for message in consumer:
    print (message.value)

Outcome : Nothing is displayed on T7 .结果T7上没有任何显示。 The Python console is stuck in the run unless I execute Ctrl+C . Python 控制台卡在运行中,除非我执行Ctrl+C No other errors or messages are produceds.不会产生其他错误或消息。 Unlike the previous thread where I get No Broker error.与我得到No Broker错误的前一个线程不同。

However, within this Approach 2, if I produce the message through a command in T6 as following, I surprisingly receive it in the consumer terminal T7 :但是,在这种方法 2 中,如果我通过以下命令在T6中生成消息,我会在消费者终端T7中意外地收到它:

echo "Hello" | ~/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic sample

My ultimate goal is to consume the messages by Python app from a remote server and work with WSL2 Ubuntu.我的最终目标是通过 Python 应用程序从远程服务器使用消息并使用 WSL2 Ubuntu。 This experiment seems to imply WSL2 is not the issue.这个实验似乎暗示 WSL2 不是问题。 If someone could enlighten as to what is happening here.如果有人可以启发这里发生的事情。

produce the message through a command... I surprisingly receive it in the consumer terminal T7通过命令产生消息......我惊讶地在消费者终端T7中收到它

No surprise here since you've not called producer.flush() or producer.close() in your Python producer app after starting the consumer loop.这并不奇怪,因为在启动消费者循环,您没有在 Python 生产者应用程序中调用producer.flush()producer.close()

The console producer blocks on every record by calling get() on the future - source , effectively flushing its buffer控制台生产者通过在 future- source上调用 get() 来阻塞每条记录,从而有效地刷新其缓冲区

Alternatively, you are missing the matching option for --from-beginning in the Python consumer if you wanted to see the previously sent records或者,如果您想查看先前发送的记录,您在 Python 消费者中缺少 --from --from-beginning的匹配选项


Ultimately, testing a local client/server within the same network adapter/subnet isn't going to help resolve an external network connection最终,在同一个网络适配器/子网中测试本地客户端/服务器并不能帮助解决外部网络连接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM