Azure 函数节点.js 扩展 - 服务总线
该项目为使用Node.js在Azure函数中与Azure Service Bus合作提供扩展. 它简化了与Service Bus的集成和互动,使开发者能够构建可扩展而高效的无服务器应用程序.
特征
- 服务巴士触发器: 当消息到达队列或订阅时自动触发 Azure 函数.
- 服务总线输入绑定: 阅读服务 Bus 消息作为您函数的输入 .
- 服务总线输出绑定: 向服务巴士队列和话题发送消息.
- 缓存客户端管理: 优化客户端再利用智能缓存
- 资源管理: Azure 资源客户的适当处置模式
- 性能优化: 减少通信业务的间接费用
先决条件
- 节点.js (LTS版本推荐)
- Azure 函数核心工具
- 活动 Azure 订阅
安装
要安装所需的依赖, 运行 :
npm install @azure/functions-extensions-servicebus
使用量
- 设置您的 Azure 服务总线命名空间数字 :
- 在 Azure 门户网站中创建服务巴士命名空间 .
- 根据需要创建队列、主题和订阅 .
- 配置您的函数数字 :
- 更新
function.json文件包含服务总线的适当绑定.
- 导入并初始化扩展 :
import '@azure/functions-extensions-servicebus'; // Ensures at the top of the file
用于 Azure 函数
import "@azure/functions-extensions-servicebus";
import { ServiceBusMessageContext } from "@azure/functions-extensions-servicebus"
import { app, InvocationContext } from "@azure/functions";
export async function serviceBusTrigger1(
serviceBusMessageContext: ServiceBusMessageContext,
context: InvocationContext
): Promise<void> {
try {
// Log trigger metadata
context.log("triggerMetadata: ", context.triggerMetadata);
// Process the first message (messages is always an array)
const message = serviceBusMessageContext.messages[0];
context.log('Completing the message', message);
// Use serviceBusMessageActions to action on the messages
await serviceBusMessageContext.actions.complete(message);
context.log('Completing the body', message.body);
} catch (error) {
context.log('Error processing Service Bus message:', error);
}
}
app.serviceBusQueue("serviceBusTrigger", {
connection: "AzureWebJobsServiceBus",
queueName: "testqueue",
cardinality: "many",
sdkBinding: true, //Ensure this is set to true
autoCompleteMessages: false, //Exposing this so that customer can take action on the messages
handler: serviceBusTrigger1,
});
// https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus?tabs=isolated-process%2Cextensionv5&pivots=programming-language-javascript
// autoCompleteMessages
- 本地运行数字 :
- 使用 Azure 函数核心工具在本地运行函数 :
func start
- 部署到阿苏尔数字 :
- 使用以下命令向 Azure 部署功能 :
func azure functionapp publish <YourFunctionAppName>
捐款
欢迎捐款! 请跟随 捐款准则 在提交问题或撤回请求时.
许可证
本项目根据以下规定获得许可证: MIT 许可证。 。 。 .