Azure Functions는 다음 Dapr 이벤트를 사용하여 Dapr 토픽 구독에서 트리거할 수 있습니다.
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("TransferEventBetweenTopics")]
public static void Run(
[DaprTopicTrigger("%PubSubName%", Topic = "A")] CloudEvent subEvent,
[DaprPublish(PubSubName = "%PubSubName%", Topic = "B")] out DaprPubSubEvent pubEvent,
ILogger log)
{
log.LogInformation("C# function processed a TransferEventBetweenTopics request from the Dapr Runtime.");
pubEvent = new DaprPubSubEvent("Transfer from Topic A: " + subEvent.Data);
}
다음은 Dapr 토픽 트리거를 사용하여 토픽을 구독하기 위한 Java 코드입니다.
@FunctionName("PrintTopicMessage")
public String run(
@DaprTopicTrigger(
pubSubName = "%PubSubName%",
topic = "B")
String payload,
final ExecutionContext context) throws JsonProcessingException {
Logger logger = context.getLogger();
logger.info("Java function processed a PrintTopicMessage request from the Dapr Runtime.");
개체를 사용하여 다음을 app 등록합니다 daprTopicTrigger.
const { app, trigger } = require('@azure/functions');
app.generic('TransferEventBetweenTopics', {
trigger: trigger.generic({
type: 'daprTopicTrigger',
name: "subEvent",
pubsubname: "%PubSubName%",
topic: "A"
}),
return: daprPublishOutput,
handler: async (request, context) => {
context.log("Node function processed a TransferEventBetweenTopics request from the Dapr Runtime.");
context.log(context.triggerMetadata.subEvent.data);
return { payload: context.triggerMetadata.subEvent.data };
}
});
The following examples show Dapr triggers in a function.json file and PowerShell code that uses those bindings.
Here's the function.json file for daprTopicTrigger:
{
"bindings": [
{
"type": "daprTopicTrigger",
"pubsubname": "%PubSubName%",
"topic": "B",
"name": "subEvent",
"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 (
$subEvent
)
Write-Host "PowerShell function processed a PrintTopicMessage request from the Dapr Runtime."
# Convert the object to a JSON-formatted string with ConvertTo-Json
$jsonString = $subEvent["data"] | ConvertTo-Json -Compress
Write-Host "Topic B received a message: $jsonString"
다음 예제에서는 v2 Python 프로그래밍 모델을 사용하는 Dapr Topic 트리거를 보여줍니다. Python 함수 앱 코드에서 사용하려면 다음을 daprTopicTrigger 수행합니다.
import logging
import json
import azure.functions as func
app = func.FunctionApp()
@app.function_name(name="PrintTopicMessage")
@app.dapr_topic_trigger(arg_name="subEvent", pub_sub_name="%PubSubName%", topic="B", route="B")
def main(subEvent) -> None:
logging.info('Python function processed a PrintTopicMessage request from the Dapr Runtime.')
subEvent_json = json.loads(subEvent)
logging.info("Topic B received a message: " + subEvent_json["data"])
Attributes
In the in-process model, use the DaprTopicTrigger to trigger a Dapr pub/sub binding, which supports the following properties.
| Parameter | Description |
|---|---|
| PubSubName | Dapr pub/sub의 이름입니다. |
| Topic | Dapr 토픽의 이름입니다. |
Annotations
DaprTopicTrigger 주석을 사용하면 토픽을 받을 때 실행되는 함수를 만들 수 있습니다.
| Element | Description |
|---|---|
| pubSubName | Dapr pub/sub의 이름입니다. |
| topic | Dapr 토픽의 이름입니다. |
Configuration
다음 표에서는 코드에 설정한 바인딩 구성 속성을 설명합니다.
| Property | Description |
|---|---|
| pubsubname | Dapr pub/sub 구성 요소 유형의 이름입니다. |
| topic | 항목의 이름입니다. |
The following table explains the binding configuration properties that you set in the function.json file.
| function.json property | Description |
|---|---|
| pubsubname | Dapr pub/sub 구성 요소 유형의 이름입니다. |
| topic | 항목의 이름입니다. |
See the Example section for complete examples.
Usage
Dapr Topic 트리거를 사용하려면 먼저 Dapr pub/sub 구성 요소를 설정합니다. 공식 Dapr 문서에서 사용할 구성 요소와 설정 방법에 대해 자세히 알아볼 수 있습니다.
Python v2에서 daprTopicTrigger를 사용하려면 올바른 종속성으로 프로젝트를 설정합니다.
requirements.text파일에서 다음 줄을 추가합니다.azure-functions==1.18.0b3터미널에서 Python 라이브러리를 설치합니다.
pip install -r .\requirements.txt다음 구성으로
local.setting.json파일을 수정합니다."PYTHON_ISOLATE_WORKER_DEPENDENCIES":1