简体   繁体   English

使用 Kafka-python 处理生产者和消费者

[英]Handling a producer and consumer using Kafka-python

First I would like to say that I am a newbie to Kafka and also stackoverflow, So I am sorry if I am not asking this in the right way.首先,我想说我是 Kafka 的新手,也是 stackoverflow,所以如果我没有以正确的方式提出这个问题,我很抱歉。 I am trying to implement the producer-consumer using kafka-python.我正在尝试使用 kafka-python 实现生产者-消费者。 But its not working properly但它不能正常工作

I have the zookeeper installed and its up and running.我已经安装了zookeeper并且它正在运行。 I have up the kafka-server also.我也有 kafka 服务器。 But when I am running the consumer and producer through pycharm, the messages are not getting received by the receiver.The consumer keeps on running but the producer stops.但是当我通过 pycharm 运行消费者和生产者时,接收者没有收到消息。消费者继续运行但生产者停止。

consumer.py消费者.py

from kafka import KafkaConsumer

consumer = KafkaConsumer('test', group_id='test-consumer-group', 
bootstrap_servers=['my_ip:9092'], api_version=(0, 10, 1),
                     auto_offset_reset='earliest')
print("Consuming messages")
for msg in consumer:
    print(msg)

producer.py生产者.py

from kafka import KafkaProducer

print('above producer')
producer = KafkaProducer(bootstrap_servers=['my_ip:9092'], api_version=(0, 10, 1),
                         compression_type=None
                         )
print('after producer')
for _ in range(100):
    producer.send('test', b'HELLO NITHIN chandran')

print('after sending messages')

In the place of my_ip, I have provided with my system ip address from ipconfig.在 my_ip 的位置,我从 ipconfig 提供了我的系统 IP 地址。

consumer.py Output - consumer.py 输出 -

Consuming messages

The consumer.py doesnt stop running consumer.py 不会停止运行

producer.py Output - producer.py 输出 -

above producer
after producer
after sending messages

Process finished with exit code 0

The producer.py stops running and the process is finished as shown in the output. producer.py 停止运行,过程完成,如输出所示。

Please help me in resolving this issue.请帮我解决这个问题。 All help are appreciated感谢所有帮助

Your code is ok, the problem is about your broker configuration.您的代码没问题,问题在于您的代理配置。 Please set it to initial configuration, just change the log.dirs to the path that you want to store Kafka data.请将其设置为初始配置,只需将log.dirs更改为您要存储 Kafka 数据的路径即可。 After changing the config file follow these steps:更改配置文件后,请按照下列步骤操作:

  • Stop zookeeper and kafka停止动物园管理员和卡夫卡
  • Clear both kafka and zookeeper data dir清除kafka和zookeeper数据目录
  • Run zookeeper and kafa运行zookeeper和kafa
  • Start consumer and producer启动消费者和生产者

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

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