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

使用Dapr+RabbitMQ+Python发布消息失败求助

Dapr+RabbitMQ+Python 消息发布失败排查与修复

问题背景

已完成Dapr安装、Docker RabbitMQ镜像部署及Dapr RabbitMQ组件配置,但Python客户端运行失败。RabbitMQ本身可用:通过pika能正常连接,管理界面可访问。运行sidecar连接RabbitMQ时出现健康检查失败及超时错误。

错误信息

Health check on http://127.0.0.1:3500/v1.0/healthz/outbound failed: [WinError 10061] No connection could be made because the target machine actively refused it

随后抛出超时异常:

Traceback (most recent call last):
File "C:\source\dapr_scratch\daprpubsub.py", line 13, in 
with DaprClient() as dapr_client:
^^^^^^^^^^^^
File "C:\source\dapr_scratch\env\Lib\site-packages\dapr\clients\__init__.py", line 83, in __init__
super().__init__(address, interceptors, max_grpc_message_length, retry_policy)
File "C:\source\dapr_scratch\env\Lib\site-packages\dapr\clients\grpc\client.py", line 139, in __init__
DaprHealth.wait_until_ready()
File "C:\source\dapr_scratch\env\Lib\site-packages\dapr\clients\health.py", line 47, in wait_until_ready
raise TimeoutError(f'Dapr health check timed out, after {timeout}.')
TimeoutError: Dapr health check timed out, after 60.0.

当前配置

RabbitMQ组件配置(rabbitmq.yaml)

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: rabbitmq-pubsub
  namespace: default
spec:
  type: pubsub.rabbitmq
  version: v1
  metadata:
  - name: connectionString
    value: "amqp://localhost:5672"
  - name: username
    value: guest
  - name: password
    value: guest

Python脚本(原错误版本)

import json
import pika
from dapr.clients import DaprClient

try:
    pika.BlockingConnection(pika.ConnectionParameters('localhost'))
except Exception as e:
    print(e)
print("connected to RabbitMQ")

# create dapr client and pub/sub a messages
data = {"message": "Hello World"}
with DaprClient() as dapr_client:
    resp = dapr_client.publish_event(pubsub_name='=rabbitmq-pubsub', topic_name='TOPIC_A', data=json.dumps(data))

修复步骤

  1. 修正脚本中的组件名错误
    原脚本里pubsub_name='=rabbitmq-pubsub'多了一个无效的=号,正确应为pubsub_name='rabbitmq-pubsub'。

  2. 通过Dapr CLI启动Sidecar
    不能单独运行Python脚本,必须用Dapr CLI启动sidecar并关联脚本。执行命令:

    dapr run --app-id python-pubsub --components-path ./components python daprpubsub.py
    
    • --components-path需指向存放rabbitmq.yaml的目录,若文件在当前目录,改为--components-path .。
    • --app-id可自定义,只要保证唯一即可。
  3. 验证组件配置路径
    确保rabbitmq.yaml放在Dapr能识别的组件目录下(默认约定为./components),否则sidecar无法加载RabbitMQ组件配置。

  4. 确认Sidecar健康状态
    启动后访问http://127.0.0.1:3500/v1.0/healthz,返回OK则说明sidecar正常运行。

修复后的Python脚本

import json
from dapr.clients import DaprClient

# 创建Dapr客户端并发布消息
data = {"message": "Hello World"}
with DaprClient() as dapr_client:
    resp = dapr_client.publish_event(
        pubsub_name='rabbitmq-pubsub', 
        topic_name='TOPIC_A', 
        data=json.dumps(data),
        data_content_type='application/json'
    )
print("消息发布成功")

内容的提问来源于stack exchange,提问作者weekendPython

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 16:48:30