简体   繁体   English

来自 python NoBrokersAvailable 的 Kafka Consumer

[英]Kafka Consumer from python NoBrokersAvailable

I am using docker to run a kafka producer with the command我正在使用 docker 通过命令运行 kafka 生产者

kafka-console-producer.sh --topic USER_CREATED_TOPIC --broker-list xxx.xx.x.x:9092`

where the x are numbers from the assigned broker ip.其中 x 是来自指定代理 ip 的数字。

My server.properties file contains我的server.properties文件包含

advertised.port=9092
advertised.host.name=xxx.xx.x.x.
listeners=PLAINTEXT://xxx.xx.x.x:9092 line
advertised.listeners=PLAINTEXT://xxx.xx.x.x:9092

Whenever i am starting a consumer from the docker container with the command每当我使用命令从 docker 容器启动消费者时

kafka-console-consumer.sh --topic USER_CREATED_TOPIC --from-beginning --bootstrap-server xxx.xx.x.x:9092

and write something in my producer console i get the result in the consumer.(no error here)并在我的生产者控制台中写一些东西,我在消费者中得到结果。(这里没有错误)

However, when i try to connect via python script using:但是,当我尝试使用以下命令通过 python 脚本进行连接时:

from kafka import KafkaConsumer
  consumer = 
  kafkaConsumer("USER_CREATED_TOPIC",bootstrap_servers= 
  ['xxx.xx.x.x:9092'])
for msg in consumer:
     print (msg)

I am getting an NoBrokersAvailable error.我收到NoBrokersAvailable错误。

I read couple of threads on stackoverflow ( listed the added items on the server.properties based on those answers) but i am still unable to connect to the kafka producer via python.我在 stackoverflow 上阅读了几个线程(根据这些答案在server.properties上列出了添加的项目),但我仍然无法通过 python 连接到 kafka 生产者。

Any help is appreciated.任何帮助表示赞赏。

The only syntaxical issue I see is that the server address should not be in a list, so like this:我看到的唯一语法问题是服务器地址不应在列表中,如下所示:

from kafka import KafkaConsumer
consumer = KafkaConsumer('sample', bootstrap_servers='0.0.0.0:9092')
for message in consumer:
    print(message)

No running cluster would also give that error.没有正在运行的集群也会出现该错误。 First create a topic:首先创建一个主题:

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

Then test if it exists:然后测试它是否存在:

bin/kafka-topics.sh --list --zookeeper localhost:2181
# sample

Lastly make sure there is a producer creating messages to listen to:最后确保有一个生产者创建要收听的消息:

from kafka import KafkaProducer
producer = KafkaProducer(bootstrap_servers='0.0.0.0:9092')
producer.send('sample', b'Hello, World!')
producer.send('sample', key=b'message-two', value=b'This is Kafka-Python')

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

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