【发布时间】:2019-01-20 05:40:45
【问题描述】:
我有一个带有 Kafka 订阅者的 node.js 应用程序。 订阅处理程序使用“fetch”来调用远程 REST API(等待 fetch(...))。
我尝试处理高频率的消息,REST 调用因远程服务器过载而失败。
发生过载是因为订阅者处理程序是异步的。
我的问题是: 有没有办法确保异步处理程序被序列化,因此不会同时调用远程 API 服务器?
克里斯: 我正在使用 kafka 节点
这是一个代码示例:
const consumer = new Consumer(this.client, [{ topic: topicKey}]);
consumer.on('message', function (message) {
handleMessage(message)
});
async function handleMessage(message) {
... decode the message
// Send to the Remote server using a REST call
//=> the task is suspended, waiting for the IO, so, meantime, the next message
// is processed, and I flood the remote server of POST requests.
await fetch(...);
}
谢谢。
【问题讨论】:
-
你用的是什么客户端?您能否提供一些代码来说明您的问题? stackoverflow.com/help/how-to-ask
-
嗨,克里斯。我正在使用 kafka 节点。
-
我只是在问题中添加代码。
-
也许你应该使用队列?如果你想要一个例子,我可以用它来回答
-
您的 REST API 是否提供将数据批量发送到端点的选项?如果是这种情况,我会在一个列表中批处理并定期发送该批处理。
标签: node.js apache-kafka kafka-consumer-api