Unable to connect to kafka host on gcp

时间:2019-04-16 23:13:39

标签: node.js google-cloud-platform apache-kafka

I'm trying to consume messages from a topic on GCP using nodejs but I do not see any output.

The producer and consumer run properly using on the instance. I'm trying that using following commands:
Producer:

kafka-console-producer.sh --broker-list X.X.X.X:9092 --producer.c onfig /opt/bitnami/kafka/conf/producer.properties --topic test

Consumer :

/opt/bitnami/kafka/bin/kafka-console-consumer.sh --bootstrap-server X.X.X.X:9092 --top ic test --consumer.config /opt/bitnami/kafka/conf/consumer.properties --from-beginning

Now, I try to consume the messages using the following nodejs code.

var kafka = require('kafka-node'),
    Consumer = kafka.Consumer,
    client = new kafka.KafkaClient({kafkaHost: 'X.X.X.X:9092', connectTimeout: 3000, requestTimeout:3000}),
    consumer = new Consumer(client,
        [{ topic: 'Posts', offset: 0}],
        {
            autoCommit: false
        }
    );

consumer.on('message', function (message) {
    console.log(message);
});

consumer.on('error', function (err) {
    console.log('Error:',err);
})

consumer.on('offsetOutOfRange', function (err) {
    console.log('offsetOutOfRange:',err);
})

I should see the messages produced by the producer as output.

0 个答案:

没有答案