您可以使用下列 Dapr 事件,在 Dapr 輸入系結上觸發 Azure Functions。
如需 Dapr 延伸模組的安裝和設定詳細資料,請參閱 Dapr 延伸模組概觀。
Example
您可以使用下列其中一種 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 輸入系結觸發程式。
daprBinding若要在 Python 函式應用程式程式代碼中使用 :
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 文件中深入了解要使用哪個元件,以及如何進行設定。