我正在研究一个用例,我需要使用node.js(v6.9.5)和Kafka-node包来使用来自Kafka(0.9.x)的消息。我使用下面的代码片段从node.js连接到Kafka。
我的Kafka环境启用了Kerberos。我不知道如何使用node.js连接到安全的Kafka环境。如果有人之前已经这样做过,请分享解决方案。
var payloads = [ { topic: 'nodejs_topic' }];
var options = {
groupId: 'kafka-node-group1',//consumer group id, default `kafka-node-group`
// Auto commit config
autoCommit: true,
autoCommitIntervalMs: 5000,
// The max wait time is the maximum amount of time in milliseconds to block waiting if insufficient data is available at the time the request is issued, default 100ms
fetchMaxWaitMs: 100,
// This is the minimum number of bytes of messages that must be available to give a response, default 1 byte
fetchMinBytes: 1,
// The maximum bytes to include in the message set for this partition. This helps bound the size of the response.
fetchMaxBytes: 1024 * 1024,
// If set true, consumer will fetch message from the given offset in the payloads
fromOffset: false,
//securityProtocol:'PLAINTEXTSASL'
// If set to 'buffer', values will be returned as raw buffer objects.
encoding: 'utf8'
};
var kafka = require('kafka-node'),
client = new kafka.Client('server-1:2181,server-2:2181,server-3:2181'),
consumer = new kafka.Consumer(client,payloads,options);
client.on('ready', function(){
console.log('Client ready!');
});
//console.log(client);
//console.log(consumer);
consumer.on('error', function (err) {
console.log("Kafka Error: Consumer - " + err);
});
consumer.on('offsetOutOfRange', function (err){
console.log("Kafka offsetOutOfRange: " + err);
});
consumer.on('message', function(message){
console.log(message);
});