我为卡夫卡客户端和生产者创建了单一类,只创建一个对象。 我需要多次发布相同的主题,而无需创建新的客户端和生产者实例。 我发现producer.on('ready',fn(){})没有使用相同的客户端和生产者实例触发,只有当我有新的客户端和生产者对象时它才会被触发。就绪事件上的kafka节点没有被触发
在这里,示例代码:
Singleton类:
const kafka = require('kafka-node');
const logger = require('./../../../../applogger');
const kafkaConfig = require('./../../../../config/config');
function ConnectionProvider() {
let kafkaConnection = undefined;
let client = undefined;
this.getConnection =() => {
if (!this.kafkaConnection) {
logger.info("Creating new kafka connection ------------------------------------- ");
this.client = new kafka.Client(kafkaConfig.ZOOKPER_HOST);
this.kafkaConnection = new kafka.Producer(this.client);
}
return this.kafkaConnection;
};
this.getClient =() => {
if (!this.client) {
logger.info("Creating new kafka Client ------------------------------------- ");
this.client = new kafka.Client(kafkaConfig.ZOOKPER_HOST);
}
return this.client;
}
process.on('SIGINT', function() {
logger.info("Going to terminate kafka connection...!");
process.exit(0);
});
}
module.exports = exports = new ConnectionProvider;
主题发布方法:
const kafkaClient = require('./../core/kafkaConnection');
const publishToKafka = function(dataPayload, callback) {
logger.debug('Publishing to topic ', topicName, ' with data: ', dataPayload);
let producer = kafkaClient.getConnection();
producer.on('ready', function() {
let payloads = dataPayload;
producer.send(payloads, function(err, data) {
if (err) {
logger.error(
'Error in publishing message to messaging pipeline ', err
);
callback(err, null);
return;
}
logger.debug('Published message to messaging pipeline topic ', topicName, ' with result: ', data);
callback(null, data);
return;
});
});
producer.on('error', function(err) {
logger.error(
'Error in publishing message to messaging pipeline ', err
);
producer.close();
});
};
DataPayload是: 让dataPayload = [{主题:某话题,消息:someMessage }]
我需要调用PublishToKafka方法mult很多次,但只想创建一个kafka客户端和生产者实例。 但生产者没有发布主题,因为producer.on('ready',function(){})在使用客户端和生产者的同一对象时没有被触发。
在此先感谢。