Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Azure Functions dapat dipicu pada langganan topik Dapr menggunakan peristiwa Dapr berikut.
Untuk informasi tentang penyiapan dan detail konfigurasi ekstensi Dapr, lihat gambaran umum ekstensi Dapr.
Example
Fungsi C# dapat dibuat menggunakan salah satu mode C# berikut:
| Execution model | Description |
|---|---|
| Model pekerja terisolasi | Kode fungsi Anda berjalan dalam proses kerja .NET yang terpisah. Gunakan dengan versi .NET dan .NET Framework yang didukung. Untuk mempelajari selengkapnya, lihat Panduan untuk menjalankan C# Azure Functions dalam model pekerja yang terisolasi. |
| In-process model | Kode fungsi Anda berjalan dalam proses yang sama dengan proses host Functions. Hanya mendukung versi Dukungan Jangka Panjang (LTS) .NET. Untuk mempelajari lebih lanjut, lihat Mengembangkan fungsi pustaka kelas C# dengan menggunakan Azure Functions. |
[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);
}
Berikut adalah kode Java untuk berlangganan topik menggunakan pemicu Topik Dapr:
@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 Gunakan objek untuk mendaftarkan 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"
Contoh berikut menunjukkan pemicu Topik Dapr, yang menggunakan model pemrograman Python v2. Untuk menggunakan daprTopicTrigger dalam kode aplikasi fungsi Python Anda:
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 | Nama dapr pub/sub. |
| Topic | Nama topik Dapr. |
Annotations
Anotasi DaprTopicTrigger memungkinkan Anda membuat fungsi yang berjalan saat topik diterima.
| Element | Description |
|---|---|
| pubSubName | Nama dapr pub/sub. |
| topic | Nama topik Dapr. |
Configuration
Tabel berikut menjelaskan properti konfigurasi pengikatan yang Anda tetapkan dalam kode.
| Property | Description |
|---|---|
| pubsubname | Nama jenis komponen dapr pub/sub. |
| topic | Nama topik. |
The following table explains the binding configuration properties that you set in the function.json file.
| function.json property | Description |
|---|---|
| pubsubname | Nama jenis komponen dapr pub/sub. |
| topic | Nama topik. |
Tabel berikut menjelaskan properti konfigurasi pengikatan untuk @dapp.dapr_topic_trigger yang Anda tetapkan dalam kode Python Anda.
| Property | Description | Dapat dikirim melalui Atribut | Dapat dikirim melalui RequestBody |
|---|---|---|---|
| pub_sub_name | Nama jenis komponen langganan Dapr. | ✔️ | ❌ |
| topic | Topik langganan. | ✔️ | ❌ |
See the Example section for complete examples.
Usage
Untuk menggunakan pemicu Topik Dapr, mulailah dengan menyiapkan komponen pub/sub Dapr. Anda dapat mempelajari selengkapnya tentang komponen mana yang akan digunakan dan cara menyiapkannya dalam dokumentasi Resmi Dapr.
Untuk menggunakan daprTopicTrigger di Python v2, siapkan proyek Anda dengan dependensi yang benar.
Membuat dan mengaktifkan lingkungan virtual.
Di file Anda
requirements.text, tambahkan baris berikut:azure-functions==1.18.0b3Di terminal, instal pustaka Python.
pip install -r .\requirements.txtUbah file Anda
local.setting.jsondengan konfigurasi berikut:"PYTHON_ISOLATE_WORKER_DEPENDENCIES":1
Next steps
Pelajari selengkapnya tentang Dapr menerbitkan dan berlangganan.