You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Azure Functions Http Trigger Python发送消息至Azure Service Bus失败

Azure Functions Python Http Trigger 无法向Service Bus队列发送消息排查

我参考官方文档示例,通过Azure Functions Http Trigger Python向Azure Service Bus队列发送消息,但消息并未写入队列。以下是我的代码和配置:

init.py 代码

import logging

import azure.functions as func


def main(req: func.HttpRequest, msg: func.Out[str]) -> func.HttpResponse:

    input_msg = req.params.get('message')

    msg.set(input_msg)

    return "OK"

function.json 配置

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "admin",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    },
    {
      "type": "serviceBus",
      "direction": "out",
      "connection": "my connection string in serviceBus",
      "name": "msg",
      "queueName": "name of queau in serviceBus"
    }
  ]
}

我的预期是消息能成功写入队列。


排查与解决步骤

  • 修正连接字符串配置
    不要直接在connection字段写入明文连接字符串,应将连接字符串存储在Function App的应用设置中(比如创建名为ServiceBusConnection的配置项),然后在function.json中引用:"connection": "ServiceBusConnection"。同时确保该连接字符串对应的SAS密钥拥有发送消息的权限。

  • 修正队列名称拼写错误
    配置中queueName字段的queau是拼写错误,改为queue,并确保名称与Azure Portal中Service Bus队列的名称完全一致(大小写敏感)。

  • 处理空消息场景
    当请求中没有message参数时,input_msg会为None,此时调用msg.set(None)不会触发消息发送。添加参数校验:

    input_msg = req.params.get('message')
    if not input_msg:
        return func.HttpResponse("缺少message参数", status_code=400)
    msg.set(input_msg)
    return func.HttpResponse("OK")
    
  • 检查队列状态
    确认Azure Portal中目标Service Bus队列处于启用状态,且未达到消息容量上限。

  • 查看函数执行日志
    在Function App的监控->日志中查看执行记录,排查是否有权限错误、连接失败等具体报错信息。

  • 确认绑定扩展版本
    确保Function App已安装Service Bus绑定扩展,Python项目的requirements.txt中包含azure-functions依赖。

内容的提问来源于stack exchange,提问作者Ernesto Pérez Astoquillca

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 08:15:49