0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Azure Functions の Queue トリガーを .Net 6 と Azure CLI でやってみた

Posted at

Azure Functions で Azure Storage の Queue をトリガーに並列処理する、イベント駆動型のスケーラブルなアーキテクチャを考えてみます。そもそも Azure Function の Queue トリガーは、キューに入ったメッセージをデフォルトの状態で並列処理してくれるのでしょうか?実際に試してみました。

Azure Functions 検証環境を作成

bash
prefix=mnrfunc
region=japaneast

az group create \
  --name ${prefix}-rg \
  --location $region

az storage account create \
  --name ${prefix}stor \
  --resource-group ${prefix}-rg \
  --sku Standard_LRS

az storage queue create \
  --name myqueue-items \
  --account-name ${prefix}stor

az monitor log-analytics workspace create \
  --workspace-name ${prefix}-log \
  --resource-group ${prefix}-rg

az monitor app-insights component create \
  --app ${prefix}-ai \
  --location $region \
  --resource-group ${prefix}-rg \
  --workspace ${prefix}-log

az functionapp create \
  --name ${prefix} \
  --resource-group ${prefix}-rg \
  --consumption-plan-location $region \
  --runtime dotnet \
  --functions-version 4 \
  --storage-account ${prefix}stor \
  --app-insights ${prefix}-ai \
  --https-only \
  --os-type Linux

検証用アプリを作成

bash
func init $prefix --dotnet

cd $prefix

func new --name queue --template "Queue trigger"

アプリにスリープと終了ログを追加

queue.cs
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using System.Threading;

namespace mnrfunc
{
    public class queue
    {
        [FunctionName("queue")]
        public void Run([QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")]string myQueueItem, ILogger log)
        {
            log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
            Thread.Sleep(10000);
            log.LogInformation($"C# Queue trigger function finished: {myQueueItem}");
        }
    }
}

Azure Functions にデプロイ

bash
func azure functionapp publish $prefix

Azure Storage のキューにメッセージを追加

bash
az storage message put \
  --queue-name myqueue-items \
  --content $(echo test | base64) \
  --account-name ${prefix}stor

for i in {1..10}
do
    az storage message put \
    --queue-name myqueue-items \
    --content $(echo test$i | base64) \
    --account-name ${prefix}stor
done

スクショを取り忘れましたが、無事並列処理している事を確認できました。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?