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# 클래스 라이브러리 함수 개발을 참조하세요. |
다음 예제에서는 Dapr 서비스 호출 트리거 및 Dapr 출력 바인딩을 사용하여 바인딩 요청을 읽고 처리하는 방법을 보여 줍니다.
[FunctionName("SendMessageToKafka")]
public static async Task Run(
[DaprServiceInvocationTrigger] JObject payload,
[DaprBinding(BindingName = "%KafkaBindingName%", Operation = "create")] IAsyncCollector<object> messages,
ILogger log)
{
log.LogInformation("C# function processed a SendMessageToKafka request.");
await messages.AddAsync(payload);
}
다음 예제에서는 "SendMessageToKafka"와 DaprBindingOutput 바인딩을 사용하여 DaprServiceInvocationTrigger 함수를 만듭니다.
@FunctionName("SendMessageToKafka")
public String run(
@DaprServiceInvocationTrigger(
methodName = "SendMessageToKafka")
String payload,
@DaprBindingOutput(
bindingName = "%KafkaBindingName%",
operation = "create")
OutputBinding<String> product,
final ExecutionContext context) {
context.getLogger().info("Java function processed a SendMessageToKafka request.");
product.setValue(payload);
return payload;
}
다음 예제에서 Dapr 출력 바인딩은 app 개체에 의해 등록된 Dapr 호출 출력 트리거와 쌍을 이룹니다.
const { app, trigger } = require('@azure/functions');
app.generic('SendMessageToKafka', {
trigger: trigger.generic({
type: 'daprServiceInvocationTrigger',
name: "payload"
}),
return: daprBindingOutput,
handler: async (request, context) => {
context.log("Node function processed a SendMessageToKafka request from the Dapr Runtime.");
context.log(context.triggerMetadata.payload)
return { "data": context.triggerMetadata.payload };
}
});
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 daprBinding:
{
"bindings":
{
"type": "daprBinding",
"direction": "out",
"bindingName": "%KafkaBindingName%",
"operation": "create",
"name": "messages"
}
}
For more information about function.json file properties, see the Configuration section.
In code:
using namespace System.Net
# Input bindings are passed in via param block.
param($req, $TriggerMetadata)
Write-Host "Powershell SendMessageToKafka processed a request."
$invoke_output_binding_req_body = @{
"data" = $req
}
# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name messages -Value $invoke_output_binding_req_body
다음 예제에서는 v2 Python 프로그래밍 모델을 사용하는 Dapr Binding 출력 바인딩을 보여줍니다. Python 함수 앱 코드에서 @dapp.dapr_binding_output을 사용하려면 다음을 수행합니다.
import logging
import json
import azure.functions as func
app = func.FunctionApp()
@app.function_name(name="SendMessageToKafka")
@app.dapr_service_invocation_trigger(arg_name="payload", method_name="SendMessageToKafka")
@app.dapr_binding_output(arg_name="messages", binding_name="%KafkaBindingName%", operation="create")
def main(payload: str, messages: func.Out[bytes]) -> None:
logging.info('Python processed a SendMessageToKafka request from the Dapr Runtime.')
messages.set(json.dumps({"data": payload}).encode('utf-8'))
Attributes
In the in-process model, use the DaprBinding to define a Dapr binding output binding, which supports these parameters:
| Parameter | Description | 특성을 통해 전송 가능 | RequestBody를 통해 보낼 수 있음 |
|---|---|---|---|
| BindingName | Dapr 바인딩의 이름입니다. | ✔️ | ✔️ |
| Operation | 구성된 바인딩 작업입니다. | ✔️ | ✔️ |
| Metadata | 메타데이터 네임스페이스입니다. | ❌ | ✔️ |
| Data | Required. 바인딩 작업의 데이터입니다. | ❌ | ✔️ |
Annotations
DaprBindingOutput 주석을 사용하면 출력 바인딩을 보내는 함수를 만들 수 있습니다.
| Element | Description | 특성을 통해 전송 가능 | RequestBody를 통해 보낼 수 있음 |
|---|---|---|---|
| bindingName | Dapr 바인딩의 이름입니다. | ✔️ | ✔️ |
| output | 구성된 바인딩 작업입니다. | ✔️ | ✔️ |
| metadata | 메타데이터 네임스페이스입니다. | ❌ | ✔️ |
| data | Required. 바인딩 작업의 데이터입니다. | ❌ | ✔️ |
Configuration
다음 표에서는 코드에 설정한 바인딩 구성 속성을 설명합니다.
| Property | Description | 특성을 통해 전송 가능 | RequestBody를 통해 보낼 수 있음 |
|---|---|---|---|
| bindingName | 바인딩 이름입니다. | ✔️ | ✔️ |
| operation | 바인딩 작업입니다. | ✔️ | ✔️ |
| metadata | 메타데이터 네임스페이스입니다. | ❌ | ✔️ |
| data | Required. 바인딩 작업의 데이터입니다. | ❌ | ✔️ |
다음 표에서는 function.json 파일에 설정된 바인딩 구성 속성을 설명합니다.
| function.json property | Description | 특성을 통해 전송 가능 | RequestBody를 통해 보낼 수 있음 |
|---|---|---|---|
| bindingName | 바인딩 이름입니다. | ✔️ | ✔️ |
| operation | 바인딩 작업입니다. | ✔️ | ✔️ |
| metadata | 메타데이터 네임스페이스입니다. | ❌ | ✔️ |
| data | Required. 바인딩 작업의 데이터입니다. | ❌ | ✔️ |
속성이 특성과 RequestBody 둘 다에 정의된 경우 RequestBody에 제공된 데이터에 우선 순위가 부여됩니다.
See the Example section for complete examples.
Usage
Dapr 출력 바인딩을 사용하려면 먼저 Dapr 출력 바인딩 구성 요소를 설정합니다. 공식 Dapr 문서에서 사용할 구성 요소와 설정 방법에 대해 자세히 알아볼 수 있습니다.
Python v2에서 daprBinding를 사용하려면 올바른 종속성으로 프로젝트를 설정합니다.
requirements.text파일에서 다음 줄을 추가합니다.azure-functions==1.18.0b3터미널에서 Python 라이브러리를 설치합니다.
pip install -r .\requirements.txt다음 구성으로
local.setting.json파일을 수정합니다."PYTHON_ISOLATE_WORKER_DEPENDENCIES":1