適用於 Azure Functions 的 Dapr 擴充功能
重要
適用於 Azure Functions 的 Dapr 擴充功能目前處於預覽狀態,且僅在 Azure Container Apps 環境中支援。
適用於 Azure Functions 的 Dapr 擴充功能是一組工具和服務,可讓開發人員輕鬆地整合 Azure Functions 與 分散式應用程式運行時間 (Dapr) 平臺。
Azure Functions 是事件驅動計算服務,可提供一組 觸發程式和系結 ,以輕鬆地與其他 Azure 服務連線。 Dapr 提供一組建置組塊和建置分散式應用程式的最佳做法,包括微服務、狀態管理、發佈/子傳訊等等。
透過 Dapr 與 Functions 之間的整合,您可以建置函式,以回應來自 Dapr 或外部系統的事件。
動作 | 方向 | 類型 |
---|---|---|
Dapr 輸入系結上的觸發程式 | N/A | daprBindingTrigger |
在 Dapr 服務調用上觸發 | N/A | daprServiceInvocationTrigger |
Dapr 主題訂用帳戶上的觸發程式 | N/A | daprTopicTrigger |
提取 Dapr 狀態以執行 | In | daprState |
提取 Dapr 秘密以執行 | In | daprSecret |
將值儲存至 Dapr 狀態 | 外 | daprState |
叫用另一個 Dapr 應用程式 | 外 | daprInvoke |
將訊息發佈至 Dapr 主題 | 外 | daprPublish |
將值傳送至 Dapr 輸出系結 | 外 | daprBinding |
安裝擴充功能
您安裝的延伸模組 NuGet 套件取決於您在函式應用程式中所使用的 C# 模式或隔離背景工作行程:
安裝套件組合
您可以在檔案 host.json
中新增或取代下列程式代碼,以新增預覽延伸模組:
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle.Preview",
"version": "[4.*, 5.0.0)"
}
}
Dapr 啟動
您可以使用各種 [自變數和註釋][dapr-args] 根據運行時間內容來設定 Dapr。 您可以透過兩個通道來設定 Azure Functions 的 Dapr:
- 基礎結構即程式碼 (IaC) 範本,如 Bicep 或 Azure Resource Manager(ARM) 範本所示
- Azure 入口網站
使用 IaC 範本時,請在容器應用程式資源定義的 區段中指定下列自變數 properties
。
DaprConfig: {
enabled: true
appId: '${envResourceNamePrefix}-funcapp'
appPort: 3001
httpReadBufferSize: ''
httpMaxRequestSize: ''
logLevel: ''
enableApiLogging: true
}
上述 Dapr 組態值會被視為應用程式範圍變更。 當您以多重修訂模式執行容器應用程式時,這些設定的變更將不會建立新的修訂。 相反地,所有現有的修訂都會重新啟動,以確保它們已設定為最新的值。
使用 Azure 入口網站 設定 Dapr 時,流覽至您的函式應用程式,然後從左側功能表中選取 [Dapr]:
Dapr 埠和接聽程式
當您從 Dapr 觸發函式時,擴充功能會自動公開埠 3001
以接聽來自 Dapr Sidecar 的傳入要求。
重要
只有在函式應用程式中定義 Dapr 觸發程式時,才會公開和接聽埠 3001
。 使用 Dapr 時,Sidecar 會先等候接收來自已定義埠的回應,再完成具現化。 除非您有觸發程式,dapr.io/port
否則--app-port
請勿定義批注。 這樣做可能會從 Dapr 側車鎖定您的應用程式。
如果您只使用輸入和輸出系結,則不需要公開或定義埠 3001
。
根據預設,當 Azure Functions 嘗試與 Dapr 通訊時,它會透過從環境變數 DAPR_HTTP_PORT
解析的埠呼叫 Dapr。 如果該變數為 null,它會預設為連接埠 3500
。
您可以在 中設定 DaprAddress
系結的 屬性 function.json
,以覆寫輸入和輸出系結所使用的 Dapr 位址(或 屬性)。 根據預設,它會使用 http://localhost:{DAPR_HTTP_PORT}
。
函式應用程式仍然會公開另一個埠和端點,例如 HTTP 觸發程式,其本機預設為 7071
,但在容器中,預設為 80
。
系結類型
.NET 支援的系結類型取決於延伸模組版本和 C# 執行模式,這可以是下列其中一項:
Dapr 擴充功能支援下表的參數類型。
繫結 | 參數型別 |
---|---|
Dapr 觸發程式 | daprBindingTrigger daprServiceInvocationTrigger daprTopicTrigger |
Dapr 輸入 | daprState daprSecret |
Dapr 輸出 | daprState daprInvoke daprPublish daprBinding |
如需使用這些類型的範例,請參閱 延伸模組的 GitHub 存放庫。
試用適用於 Azure Functions 的 Dapr 擴充功能
瞭解如何透過提供的範例使用適用於 Azure Functions 的 Dapr 擴充功能。
範例 | 描述 |
---|---|
快速入門 | 開始使用 Dapr Pub/sub 系結和 HttpTrigger 。 |
Dapr Kafka | 瞭解如何搭配 Kafka 系結 Dapr 元件使用 Azure Functions Dapr 擴充功能。 |
.NET 進程內 | 瞭解如何使用 Azure Functions 同進程模型與 .NET 中的多個 Dapr 元件整合,例如服務調用、Pub/sub、系結和狀態管理。 |
.NET 隔離 | 使用 Azure Functions out-of-proc (OOP) 執行模型,與 .NET 中的 Dapr 元件整合。 |
試用適用於 Azure Functions 的 Dapr 擴充功能
瞭解如何透過提供的範例使用適用於 Azure Functions 的 Dapr 擴充功能。
範例 | 描述 |
---|---|
Java 函式 | 瞭解如何使用 Java 使用 Azure Functions Dapr 擴充功能。 |
試用適用於 Azure Functions 的 Dapr 擴充功能
瞭解如何透過提供的範例使用適用於 Azure Functions 的 Dapr 擴充功能。
範例 | 描述 |
---|---|
快速入門 | 開始使用 Dapr Pub/sub 系結和 HttpTrigger 。 |
Dapr Kafka | 瞭解如何搭配 Kafka 系結 Dapr 元件使用 Azure Functions Dapr 擴充功能。 |
JavaScript | 執行 JavaScript Dapr 函式應用程式,並使用 Azure Functions 與 Dapr Service 調用、Pub/sub、Bindings 和 State Management 整合。 |
試用適用於 Azure Functions 的 Dapr 擴充功能
瞭解如何透過提供的範例使用適用於 Azure Functions 的 Dapr 擴充功能。
範例 | 描述 |
---|---|
PowerShell 函式 | 瞭解如何搭配 PowerShell 使用 Azure Functions Dapr 擴充功能。 |
試用適用於 Azure Functions 的 Dapr 擴充功能
瞭解如何透過提供的範例使用適用於 Azure Functions 的 Dapr 擴充功能。
範例 | 描述 |
---|---|
Dapr Kafka | 瞭解如何搭配 Kafka 系結 Dapr 元件使用 Azure Functions Dapr 擴充功能。 |
Python v1 | 執行 Dapr 化的 Python 應用程式,並使用 Azure Functions Python v1 程式設計模型來與 Dapr 元件整合。 |
Python v2 | 使用 Azure Functions Python v2 程式設計模型啟動 Dapr 應用程式,以與 Dapr 元件整合。 |
疑難排解
本節說明如何使用適用於 Azure Functions 的 Dapr 擴充功能,針對可能發生的問題進行疑難解答。
確定您的環境中已啟用 Dapr
如果您在 Azure Functions 中使用 Dapr 系結和觸發程式,且您的環境中未啟用 Dapr,您可能會收到錯誤訊息:若要在您的環境中啟用 Dapr: Dapr sidecar isn't present. Please see (https://aka.ms/azure-functions-dapr-sidecar-missing) for more information.
如果您的 Azure 函式部署在 Azure Container Apps 中,請參閱 適用於 Azure Functions 的 Dapr 擴充功能的 Dapr 啟用指示。
如果您的 Azure 函式部署在 Kubernetes 中,請確認部署 的 YAML 組態 具有下列批注:
annotations: ... dapr.io/enabled: "true" dapr.io/app-id: "functionapp" # You should only set app-port if you are using a Dapr trigger in your code. dapr.io/app-port: "<DAPR_APP_PORT>" ...
如果您在本機執行 Azure 函式,請執行下列命令,以確保您使用 Dapr 執行函式應用程式:
dapr run --app-id functionapp --app-port <DAPR_APP_PORT> --components-path <COMPONENTS_PATH> -- func host start
確認 Dapr 設定中的應用程式埠值
Azure Functions 的 Dapr 擴充功能預設會在埠 3001
上啟動 HTTP 伺服器。 您可以使用環境變數來DAPR_APP_PORT
設定此埠。
如果您在執行 Azure Functions 應用程式時提供不正確的應用程式連接埠值,您可能會收到錯誤訊息:若要解決此錯誤訊息: The Dapr sidecar is configured to listen on port {portInt}, but the app server is running on port {appPort}. This may cause unexpected behavior. For more information, visit [this link](https://aka.ms/azfunc-dapr-app-config-error).
在您的容器應用程式的 Dapr 設定中:
如果您在程式代碼中使用 Dapr 觸發程式,請確認應用程式埠已設定為 或 環境變數
3001
的值DAPR_APP_PORT
。如果您未在程式代碼中使用 Dapr 觸發程式,請確認未設定應用程式埠。 它應該是空的。
確認您在 Dapr 設定中提供正確的應用程式埠值。
如果您使用 Azure Container Apps,請在 Bicep 中指定應用程式連接埠:
DaprConfig: { ... appPort: <DAPR_APP_PORT> ... }
如果您使用 Kubernetes 環境,請設定
dapr.io/app-port
註釋:annotations: ... dapr.io/app-port: "<DAPR_APP_PORT>" ...
如果您要在本機開發,請使用 Dapr 執行函式應用程式時,確認您已設定
--app-port
:dapr run --app-id functionapp --app-port <DAPR_APP_PORT> --components-path <COMPONENTS_PATH> -- func host start