Azure Functions は、次の Dapr イベントを使用して、Dapr 入力バインドでトリガーできます。
Dapr 拡張機能のセットアップと構成について詳しくは、Dapr 拡張機能の概要に関する記事をご覧ください。
Example
A C# 関数は、次の C# モードのいずれかを使用して作成できます。
| Execution model | Description |
|---|---|
| 分離ワーカー モデル | 関数コードは、別の .NET ワーカー プロセスで実行されます。 .NET と .NET Framework のサポートされているバージョンで使います。 詳細については、 分離ワーカー モデルで C# Azure Functions を実行するためのガイドを参照してください。 |
| In-process model | 関数コードは、Functions ホスト プロセスと同じプロセスで実行されます。 .NET の長期サポート (LTS) バージョンのみをサポートします。 詳細については、「Azure Functions を使用する C# クラス ライブラリ関数を開発する」を参照してください。 |
[FunctionName("ConsumeMessageFromKafka")]
public static void Run(
// Note: the value of BindingName must match the binding name in components/kafka-bindings.yaml
[DaprBindingTrigger(BindingName = "%KafkaBindingName%")] JObject triggerData,
ILogger log)
{
log.LogInformation("Hello from Kafka!");
log.LogInformation($"Trigger data: {triggerData}");
}
Dapr 入力バインド トリガーの Java コードを次に示します。
@FunctionName("ConsumeMessageFromKafka")
public String run(
@DaprBindingTrigger(
bindingName = "%KafkaBindingName%")
)
app オブジェクトを使用して、daprBindingTriggerを登録します。
const { app, trigger } = require('@azure/functions');
app.generic('ConsumeMessageFromKafka', {
trigger: trigger.generic({
type: 'daprBindingTrigger',
bindingName: "%KafkaBindingName%",
name: "triggerData"
}),
handler: async (request, context) => {
context.log("Node function processed a ConsumeMessageFromKafka request from the Dapr Runtime.");
context.log(context.triggerMetadata.triggerData)
}
});
The following example shows Dapr triggers in a function.json file and PowerShell code that uses those bindings.
Here's the function.json file for daprBindingTrigger:
{
"bindings": [
{
"type": "daprBindingTrigger",
"bindingName": "%KafkaBindingName%",
"name": "triggerData",
"direction": "in"
}
]
}
For more information about function.json file properties, see the Configuration section.
In code:
using namespace System
using namespace Microsoft.Azure.WebJobs
using namespace Microsoft.Extensions.Logging
using namespace Microsoft.Azure.WebJobs.Extensions.Dapr
using namespace Newtonsoft.Json.Linq
param (
$triggerData
)
Write-Host "PowerShell function processed a ConsumeMessageFromKafka request from the Dapr Runtime."
$jsonString = $triggerData | ConvertTo-Json
Write-Host "Trigger data: $jsonString"
次の例は、 v2 Python プログラミング モデルを使用する Dapr 入力バインド トリガーを示しています。 Python 関数アプリ コードで daprBinding を使用するには:
import logging
import json
import azure.functions as func
app = func.FunctionApp()
@app.function_name(name="ConsumeMessageFromKafka")
@app.dapr_binding_trigger(arg_name="triggerData", binding_name="%KafkaBindingName%")
def main(triggerData: str) -> None:
logging.info('Python function processed a ConsumeMessageFromKafka request from the Dapr Runtime.')
logging.info('Trigger data: ' + triggerData)
Attributes
In the in-process model, use the DaprBindingTrigger to trigger a Dapr input binding, which supports the following properties.
| Parameter | Description |
|---|---|
| BindingName | Dapr トリガーの名前。 指定しない場合、関数の名前がトリガー名として使用されます。 |
Annotations
DaprBindingTrigger注釈を使用すると、作成したバインド コンポーネントによってトリガーされる関数を作成できます。
| Element | Description |
|---|---|
| bindingName | Dapr バインドの名前。 |
Configuration
次の表では、コードで設定するバインド構成プロパティについて説明します。
| Property | Description |
|---|---|
| bindingName | バインディングの名前。 |
次の表は、function.json ファイルで設定したバインド構成のプロパティを説明しています。
| function.json property | Description |
|---|---|
| bindingName | バインディングの名前。 |
See the Example section for complete examples.
Usage
Dapr 入力バインド トリガーを使用するには、まず Dapr 入力バインド コンポーネントを設定します。 使用するコンポーネントとその設定方法の詳細については、Dapr の公式ドキュメントを参照してください。
Python v2 で daprBindingTrigger を使用するには、正しい依存関係でプロジェクトを設定します。
requirements.textファイルに、次の行を追加します。azure-functions==1.18.0b3ターミナルで、Python ライブラリをインストールします。
pip install -r .\requirements.txt次の構成で
local.setting.jsonファイルを変更します。"PYTHON_ISOLATE_WORKER_DEPENDENCIES":1