Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache kafka 生产/消费远程卡夫卡不起作用_Apache Kafka_Kafka Python_Pykafka - Fatal编程技术网

Apache kafka 生产/消费远程卡夫卡不起作用

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 消费:

我已经通过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-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)
  • 有没有办法调试这些问题并找到根本原因?如果存在一些连接问题,我希望库抛出异常
  • 发生了什么事

只是好奇:为什么卡夫卡的版本如此古老?而且,默认情况下,卡夫卡不会从EC2外部监听。它不像端口转发那么简单。除了打开AWS EC2实例上的任何端口和权限外,您可能还必须将
侦听器
属性从
listeners=PLAINTEXT://localhost:9092
更改为
listeners=PLAINTEXT://0.0.0:9092
。使用
0.0.0.0
的IP地址允许Kafka侦听来自机器外部的连接,而拥有
localhost
只允许从同一台机器内部进行连接。