事件中心发送速度快,但获取速度慢。我使用下一个代码:
subscribe(cb: (event: any) => Promise<void>) {
this.consumerClient.subscribe(
this.config.partitionId,
{
processEvents: async (events: any, context: any) => {
if (events.length === 0) {
return;
}
for (const event of events) {
await cb(event.body);
}
await context.updateCheckpoint(events[events.length - 1]);
},
processError: async (err, context: any) => {
console.error(`Event Hub Subscribe Error`, { err, context });
},
},
{ startPosition: earliestEventPosition },
);
}
获取 100 个事件大约需要 1 分钟。 await cb(event.body);
工作速度很快。
最佳答案
您可以在订阅选项中使用“maxBatchSize”和“maxWaitTimeInSeconds”:
subscribe(cb: (event: any) => Promise<void>) {
this.consumerClient.subscribe(
this.config.partitionId,
{
processEvents: async (events: any, context: any) => {
if (events.length === 0) {
return;
}
for (const event of events) {
await cb(event.body);
}
await context.updateCheckpoint(events[events.length - 1]);
},
processError: async (err, context: any) => {
console.error(`Event Hub Subscribe Error`, { err, context });
},
},
{
startPosition: earliestEventPosition,
maxBatchSize: 50,
maxWaitTimeInSeconds: 1
}
);
}
关于node.js - 为什么 Azure 事件中心获取事件的速度如此之慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72130753/