生产/消费远程Kafka不起作用

mlmc2os5  于 2021-06-06  发布在  Kafka
关注(0)|答案(0)|浏览(288)

我已经通过bitnami映像设置了一个运行apachekafka0.8的aws ec2示例。服务器属性几乎是默认的(kafka位于localhost:9092 and Zookeeper位于localhost:2181).
当我ssh到机器中时,我可以使用kafka提供的脚本(位于kafka/bin)生成/使用数据。要生成,我运行以下命令:

./kafka-console-producer.sh --broker-list localhost:9092 --topic test

消费:

./kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning

这是正确的,因此我确定Kafka是正确的运作。接下来,我尝试使用python库pykafka从我的机器生成/使用:

client = KafkaClient(hosts = KAFKA_HOST)
topic = client.topics[sys.argv[1]]

try:
    with topic.get_producer(max_queued_messages=1, auto_start=True) as producer:
        while True:
            for i in range(10):
                message = "Test message sent on: " + str(datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y"))
                encoded_message = message.encode("utf-8")
                mess = producer.produce(encoded_message)
except Exception as error:
    print('Something went wrong; printing exception:')
    print(error)

我消费如下:

client = KafkaClient(hosts = KAFKA_HOST)
topic = client.topics[sys.argv[1]]

try:    
    while True:
        consumer = topic.get_simple_consumer(auto_start=True)
        for message in consumer:
            if message is not None:
                print (message.offset, message.value)
except Exception as error:
    print('Something went wrong; printing exception:')
    print(error)

这些代码段运行时没有错误或异常,但是没有生成或使用任何消息,甚至没有通过本地脚本创建的消息。
我已经确认,端口9092和2181都是通过telnet开放的。我的问题如下:
有没有办法调试这些问题并找出根本原因?如果存在一些连接问题,我希望库抛出异常。
怎么回事?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题