在nodejs microservice中设置kafka侦听器

y53ybaqx  于 2021-06-08  发布在  Kafka
关注(0)|答案(0)|浏览(217)

我想知道如何继续设置Kafka听众。我应该运行两个独立的进程吗 node server.js 以及 node kafka-listener.js 每一个微服务?
我有两个的示例代码:
这是 server.js 运行节点服务器的

require('dotenv').config() // Accesses the environment variables from .env file at the root of the folder or from the 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 are using a different port for testing because we might want to have the development server ON when we 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; // for testing

这是一个示例Kafka听众( 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);
})

侦听器是否应该作为单独的进程运行?或者还有其他的选择吗?
如果您有输入,那么一旦有消息发送给侦听器,路由是如何完成的。请再加上这个。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题