我正在学习RabbitMQ的基础知识,将其安装在容器上。 检查容器时似乎没问题。
PS C:\Users\jvidin> docker port rabbitmq
25672/tcp -> 0.0.0.0:32776
4369/tcp -> 0.0.0.0:32779
5671/tcp -> 0.0.0.0:32778
5672/tcp -> 0.0.0.0:32777
PS C:\Users\jvidin> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d2fe1297c403 rabbitmq:latest "docker-entrypoint..." 22 minutes ago Up 22 minutes 0.0.0.0:32779->4369/tcp, 0.0.0.0:32778->5671/tcp, 0.0.0.0:32777->5672/tcp, 0.0.0.0:32776->25672/tcp rabbitmq
55253e21bb49 b38ce49eadce "docker-entrypoint..." 8 weeks ago Up 3 hours 0.0.0.0:5433->5432/tcp mypostgres9.6
但是,当尝试通过Pika Python lib进行连接时,它会失败并显示
pika.exceptions.ConnectionClosed: Connection to 127.0.0.1:5672 failed: timeout
代码
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='', routing_key='hello', body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()
以下关于正确连接信息的决议
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost', port=32777))
答案 0 :(得分:2)
您需要将python客户端配置为使用localhost:32777
。
只需检查docker port rabbitmq
的输出。它显示容器端口5672
映射到docker主机上的端口32777
。