UNPKG

enn-egg-kafka

Version:

egg kafka plugin base kafkajs module

63 lines (55 loc) 1.71 kB
'use strict'; const assert = require('assert'); const { Kafka, Partitioners } = require('kafkajs'); module.exports = app => { app.addSingleton('kafka', createOneClient); }; async function createOneClient(config, app) { assert(config.brokers && config.brokers.length && config.clientId, `[egg-kafka-enn] 'brokers: ${config.brokers}', 'clientId: ${config.clientId}'are required on config`); app.coreLogger.info('[egg-kafka] connecting %s:%s', config.clientId, config.brokers); const clientConfig = { clientId: 'broker-data-sync', brokers: ['127.0.0.1:9092'], connectionTimeout: 10000, retry: { initialRetryTime: 500, maxRetryTime: 10000, retries: Number.MAX_SAFE_INTEGER, } }; const producerConfig = { allowAutoTopicCreation: true, createPartitioner: Partitioners.LegacyPartitioner }; const consumerConfig = { groupId: 'group-default', }; if (Object.keys(config.producer).length) { Object.assign(producerConfig, config.producer); } if (Object.keys(config.consumer).length) { Object.assign(consumerConfig, config.consumer); } // create Client instance const client = new Kafka(Object.assign(clientConfig, config)); const producer = client.producer(producerConfig); await producer.connect(); const consumer = client.consumer(consumerConfig); const object = { client, producer, consumer, producerSend: async function (topic, messages) { await producer.send({ topic, messages, }); }, consumerInit: async function (topic) { await consumer.connect(); await consumer.subscribe({ topic, fromBeginning: false }); } }; return object; }