All Projects → JohnApache → egg-kafka-node

JohnApache / egg-kafka-node

Licence: MIT license
kafka plugin for egg.js

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to egg-kafka-node

egg-kafkajs
☎️kafka plugin for eggjs
Stars: ✭ 26 (-27.78%)
Mutual labels:  egg, egg-plugin, kafka-node
Egg View Ejs
egg view plugin for ejs.
Stars: ✭ 54 (+50%)
Mutual labels:  egg, egg-plugin
Egg Authz
egg-authz is an authorization middleware for Egg.js based on Casbin
Stars: ✭ 50 (+38.89%)
Mutual labels:  egg, egg-plugin
Egg Redis
redis plugin for egg
Stars: ✭ 234 (+550%)
Mutual labels:  egg, egg-plugin
Egg View
Stars: ✭ 35 (-2.78%)
Mutual labels:  egg, egg-plugin
Egg Cancan
cancancan like authorization plugin for Egg.js
Stars: ✭ 47 (+30.56%)
Mutual labels:  egg, egg-plugin
Egg Schedule
Schedule plugin for egg
Stars: ✭ 76 (+111.11%)
Mutual labels:  egg, egg-plugin
Egg Restfulapi
🏅 基于Egg.js 2.0 & {mongoose,jwt}RESTful API 模板,用于快速集成开发RESTful前后端分离的服务端。
Stars: ✭ 524 (+1355.56%)
Mutual labels:  egg, egg-plugin
Egg Router Plus
The missing router feature for eggjs
Stars: ✭ 117 (+225%)
Mutual labels:  egg, egg-plugin
Egg Validate
validate plugin for egg
Stars: ✭ 224 (+522.22%)
Mutual labels:  egg, egg-plugin
Egg Socket.io
socket.io plugin for eggjs.
Stars: ✭ 209 (+480.56%)
Mutual labels:  egg, egg-plugin
Cool Admin Api
cool-admin-api 是基于egg.js、typeorm、jwt等封装的api开发脚手架、快速开发api接口
Stars: ✭ 188 (+422.22%)
Mutual labels:  egg, egg-plugin
Egg Valid
👮Validation plugin for eggjs
Stars: ✭ 10 (-72.22%)
Mutual labels:  egg, egg-plugin
Egg Oauth2 Server
🌟 OAuth2 server plugin for egg.js based on node-oauth2-server
Stars: ✭ 174 (+383.33%)
Mutual labels:  egg, egg-plugin
Egg Sequelize
Sequelize for Egg.js
Stars: ✭ 540 (+1400%)
Mutual labels:  egg, egg-plugin
Egg View React Ssr
Egg React Server Side Render (SSR) Plugin
Stars: ✭ 55 (+52.78%)
Mutual labels:  egg, egg-plugin
Egg Security
Security plugin for egg, force performance too.
Stars: ✭ 204 (+466.67%)
Mutual labels:  egg, egg-plugin
Egg Mongoose
Stars: ✭ 386 (+972.22%)
Mutual labels:  egg, egg-plugin
Egg 24time
A Twitter-like news and social server for Egg. 微信小程序社区全栈解决方案
Stars: ✭ 493 (+1269.44%)
Mutual labels:  egg, egg-plugin
Egg Passport
passport plugin for egg
Stars: ✭ 98 (+172.22%)
Mutual labels:  egg, egg-plugin

egg-kafka-node

NPM version build status Test coverage Known Vulnerabilities npm download

This plug-in is a package of [kafka-node] (https://github.com/SOHU-Co/kafka-node). It is an egg-style plug-in for easy use in the environment of egg.js. It also provides a detailed configuration of methods for sending Kafka messages. Refer to [https://github.com/SOHU-Co/kafka-node] (https://github.com/SOHU-Co/kafka-node).

Install

$ npm i egg-kafka-node --save

Usage

// {app_root}/config/plugin.js
exports.kafkaNode = {
  enable: true,
  package: 'egg-kafka-node',
};

Configuration

// {app_root}/config/config.default.js
exports.kafkaNode = {
  kafkaHost: '127.0.0.1:9092', // kafka connect host
  clientOption: {}, // KafkaClient option, more documentation please visit kafka-node
  consumerOption: [{
    groupId: 'group1', // consumerGroup's groupId
    topics: [ 'testTopic1' ], // topics under the same consumer group 
    options: {
      fetchMaxWaitMs: 100,
      fetchMinBytes: 1,
      fetchMaxBytes: 1024 * 1024,
    }, // relevant configuration for each consumer group, more documentation please visit kafka-node
  }, {
    groupId: 'group2',
    topics: [ 'testTopic2' ],
    options: {},
  }, {
    groupId: 'group3',
    topics: [ 'testTopic3' ],
  }],
  
  // HighLevelProducer option, more documentation please visit kafka-node
  producerOption: {
    requireAcks: 1, 
    ackTimeoutMs: 100, 
    partitionerType: 2, 
    autoCreateTopic: true, // Whether to turn on automatic topic creation. default true
    topics: [ 'testTopic1', 'testTopic2', 'testTopic3' ], // Topics that all consumers need to consume
  },
  messageOption: {
    partition: 0,
    attributes: 0, // send message option
  },
  // NewConfig 
  baseConsumersDir: './app/kafka', // support read consumers files base dir
};

see config/config.default.js for more detail.

Structure

egg-project
├── package.json
├── app.js (optional)
├── app
|   ├── router.js
   ├── controller
   |   └── home.js
   ├── service (optional)
   |   └── user.js
   |   └── response_time.js
   └── kafka (optional)  --------> like `controller, service...`
       ├── someTopic (optional)  -------> topic name of kafka
            ├── someKey1Consumer.js(optional)  ------> `someKey1` is a key of someTopic
|            └── someKey2Consumer.js(optional)  ------> `someKey2` is an another key of someTopic
|            └── someKey3Consumer.ts(optional)  ------> support load ts file consumers
├── config
|   ├── plugin.js
|   ├── config.default.js
   ├── config.prod.js
|   ├── config.test.js (optional)
|   ├── config.local.js (optional)
|   └── config.unittest.js (optional)

USE TIPS

Note: The producer option topics of the kafkaNode configuration must create a corresponding topic directory under the {app-root}/kafka directory. Kafka-node automatically reads the file containing the'Consumers'filename under the topic, and the key needs to be passed in when sendMessage to facilitate business differentiation.

Note: You must set app.config.baseDir, kafka need to load consumers base on the baseDir.

Note: SendMessage messages max bytes depending on the configuration of you set.

Example

// {app_root}/controller/index.js
class IndexController extends Controller {
  async index() {
    await this.ctx.kafka.sendMessage({
      topic: 'someTopic', // Specify topics in the Kafka directory
      key: 'someKey', // Specify consumer for the corresponding key under topic
      messages: JSON.stringify({
        username: 'JohnApache',
        userId: 10001,
        gender: 0
      })
    });
  }

  async some() {
    this.ctx.kafka.sendMessageSync({
      topic: 'someTopic', // Specify topics in the Kafka directory
      key: 'someKey', // Specify consumer for the corresponding key under topic
      messages: JSON.stringify({
        username: 'JohnApache',
        userId: 10001,
        gender: 0
      })
    }, () => {
      // success callback 
    }, () => {
      // error callback 
    })
  }
}

// {app_root}/kafka/someTopic/someKeyConsumer.js
class SomeKeySubscription extends Subscription {
  async subscribe(message) {
    const {value, topic, key} = message;
    this.ctx.logger.info(`consume message ${value} by topic ${topic} key ${key} consumer`);
    await asyncTask();
  }
}

Questions & Suggestions

Please open an issue here.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].