Apache kafka 生产/消费远程卡夫卡不起作用
我已经通过Bitnami AMI映像设置了一个运行ApacheKafka 0.8的AWSEC2实例。服务器属性几乎是默认的(Kafka位于localhost:9092,zookeeper位于localhost:2181) 当我使用SSH连接到机器中时,我可以使用Kafka提供的脚本(位于Kafka/bin)生成/使用数据。 要生成,我运行以下命令:Apache kafka 生产/消费远程卡夫卡不起作用,apache-kafka,kafka-python,pykafka,Apache Kafka,Kafka Python,Pykafka,我已经通过Bitnami AMI映像设置了一个运行ApacheKafka 0.8的AWSEC2实例。服务器属性几乎是默认的(Kafka位于localhost:9092,zookeeper位于localhost:2181) 当我使用SSH连接到机器中时,我可以使用Kafka提供的脚本(位于Kafka/bin)生成/使用数据。 要生成,我运行以下命令: ./kafka-console-producer.sh --broker-list localhost:9092 --topic test 消费:
./kafka-console-producer.sh --broker-list localhost:9092 --topic test
消费:
./kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
这是正确的,因此我确定卡夫卡的功能是正确的。接下来,我尝试使用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打开。
我的问题如下:
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)
- 有没有办法调试这些问题并找到根本原因?如果存在一些连接问题,我希望库抛出异常
- 发生了什么事
侦听器
属性从listeners=PLAINTEXT://localhost:9092
更改为listeners=PLAINTEXT://0.0.0:9092
。使用0.0.0.0
的IP地址允许Kafka侦听来自机器外部的连接,而拥有localhost
只允许从同一台机器内部进行连接。