node.js - Setting up kafka listener in nodejs microservice -
i wondering how proceed set of kafka listener. should running 2 separate processes node server.js
, node kafka-listener.js
every micro service?
i have sample code of both of them:
this server.js
runs node server
require('dotenv').config() // accesses environment variables .env file @ root of folder or docker environment var port = process.env.port; const express = require("express"), createtables = require("./config/createtables.js"), vogels = require('vogels'), morgan = require('morgan'), logger = require('./config/logger'), app = express(), bodyparser = require("body-parser"); createtables(); app.use(bodyparser.urlencoded({extended:true})); app.use(bodyparser.json()); //we using different port testing because might want have development server on when run tests if (process.env.node_env === "test") { port = 3002 } app.use(morgan('dev', { skip: function (req, res) { return res.statuscode < 400 }, stream: process.stderr })); app.use(morgan('dev', { skip: function (req, res) { return res.statuscode >= 400 }, stream: process.stdout })); var routes = require('./api/routes/lrroutes'); routes(app); app.listen(port, function(){ logger.info('example app listening on port ' + port); }); module.exports = app; // testing
this sample kafka listener (kafka-listener.js
):
var kafka = require('kafka-node'), consumer = kafka.consumer, client = new kafka.client(), consumer = new consumer(client, [{ topic: 'name', 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); })
should listener run separate process? or there other alternative this?
if have inputs how routing done once there message listener. please add too.
Comments
Post a Comment