Functions 指南
触发器与绑定
| 触发器 | 说明 |
|---|---|
| HTTP | REST 端点、Webhook |
| Timer | Cron 表达式定时触发 |
| Blob Storage | 新建/修改 Blob 时触发 |
| Service Bus | 队列或主题消息 |
| Event Hub | 高吞吐事件流 |
| Cosmos DB | 变更流触发 |
function.json 与代码示例
{
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": ["get", "post"]
},
{"type": "http", "direction": "out", "name": "res"},
{
"type": "queue",
"direction": "out",
"name": "outputQueue",
"queueName": "work-items",
"connection": "AzureWebJobsStorage"
}
]
}
Durable Functions
// 编排器函数(Node.js 示例)
const df = require('durable-functions');
module.exports = df.orchestrator(function*(context) {
// 顺序活动
const x = yield context.df.callActivity('ProcessOrder', context.df.getInput());
// 并行扇出
const tasks = [1, 2, 3].map(i =>
context.df.callActivity('ProcessItem', i)
);
const results = yield context.df.Task.all(tasks);
// 等待外部事件(人工审批)
const approval = yield context.df.waitForExternalEvent('Approval');
return results;
});
部署槽
# 创建 staging 槽
az functionapp deployment slot create \
--name my-function-app \
--resource-group myRG --slot staging
# 部署到 staging 槽
az functionapp deployment source config-zip \
--name my-function-app \
--resource-group myRG --slot staging \
--src function.zip
# 将 staging 切换到生产
az functionapp deployment slot swap \
--name my-function-app \
--resource-group myRG \
--slot staging --target-slot production
应用配置
# 设置应用配置
az functionapp config appsettings set \
--name my-function-app \
--resource-group myRG \
--settings DB_CONNECTION_STRING="Server=..."
# 引用 Key Vault Secret
az functionapp config appsettings set \
--name my-function-app \
--resource-group myRG \
--settings "[email protected](VaultName=myVault;SecretName=db-pass)"