首页 > 解决方案 > 如何限制并发 Azure Function 执行

问题描述

我已经看到这个问题表达了很多,但我还没有找到一个可行的解决方案。

简而言之,我定期有大量的处理操作要做。每个操作都由 Azure 函数处理。每个操作都会调用数据库。如果我有太多的并发函数同时运行,这会使数据库过载并且我得到超时错误。因此,我希望能够限制一次运行的并发 Azure 函数调用的数量。

我已将函数切换为队列触发,并根据我在网上看到的内容以多种方式调整了 batchSize / newBatchThreshold / maxDequeueCount host.json 设置。我还在函数应用程序设置中将 WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUT 应用程序设置设置为 1,以防止生成多个 VM。

然而,每次我填满那个队列时,都会不分青红皂白地产生多个函数,我的数据库就会崩溃。

如何限制并发操作的数量?

标签: azureazure-functionsazure-queues

解决方案


The problem ended up being a difference in formatting of host.json in V1 and V2 Functions. Below is the correct configuration (using Microsoft.Azure.WebJobs.Extensions.Storage at least 3.0.1). The following host.json configures a single function app to process queue messages sequentially.

{ 
  "version":"2.0",
  "extensions": { 
    "queues": { 
      "batchSize": 1,
      "newBatchThreshold": 0 
     }
   } 
}

Setting App Setting WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUT = 1 restricts a function app from dynamically scaling out beyond one instance.


推荐阅读