我有一个由队列消息触发的Azure函数。该函数向第三方API发出请求.不幸的是,这个API有限制-10个事务每秒,但我可能有超过10条消息每秒服务总线队列。如何限制Azure函数的调用次数以满足第三方API的限制?
发布于 2018-03-17 19:04:54
不幸的是,在这方面没有内置的选择。
限制并发执行的唯一可靠方法是在固定的(而不是消费计划)上运行,只需要始终运行一个实例。你得为此付出代价。
然后在host.json文件中设置该选项:
"serviceBus": {
// The maximum number of concurrent calls to the callback the message
// pump should initiate. The default is 16.
"maxConcurrentCalls": 10
}最后,确保您的函数执行(或其他最小持续时间,并相应地调整并发调用)。
正如@SeanFeldman所建议的,请参阅this answer中的其他一些想法。它是关于存储队列的,但也适用于服务总线。
发布于 2018-03-17 18:50:44
您可以尝试编写一些自定义逻辑,即在Azure函数中实现自己的内存中队列,以排队处理请求,并限制对第三方API的调用。无论如何,在调用第三方API成功之前,您不需要确认队列中的消息。这样,可靠性也得到了保持。
发布于 2018-03-17 19:07:57
维护系统完整性的最佳方法是抑制服务总线消息的消耗。您可以控制QueueClient处理消息的方式,请参阅:https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-get-started-with-queues#4-receive-messages-from-the-queue
查看“最大并发调用”
static void RegisterOnMessageHandlerAndReceiveMessages()
{
// Configure the message handler options in terms of exception handling, number of concurrent messages to deliver, etc.
var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
{
// Maximum number of concurrent calls to the callback ProcessMessagesAsync(), set to 1 for simplicity.
// Set it according to how many messages the application wants to process in parallel.
MaxConcurrentCalls = 1,
// Indicates whether the message pump should automatically complete the messages after returning from user callback.
// False below indicates the complete operation is handled by the user callback as in ProcessMessagesAsync().
AutoComplete = false
};
// Register the function that processes messages.
queueClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
}https://stackoverflow.com/questions/49340233
复制相似问题