RabbitMq连接到127.0.0.1:5672失败

vwoqyblh  于 2023-04-20  发布在  RabbitMQ
关注(0)|答案(4)|浏览(743)

我目前正在学习rabbitmq教程,遇到了一个问题。无论我多么仔细地学习教程,当我试图运行www.example.com和www.example.com时,我总是得到这个错误send.pyreceive.py:

pika.exceptions.ConnectionClosed: Connection to 127.0.0.1:5672 failed: [Errno 61] Connection refused

这是send.py:

#!/usr/bin/env python
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()

这是receive.py:

#!/usr/bin/env python
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='hello')

def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)

channel.basic_consume(callback,
                      queue='hello',
                      no_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()

我一辈子都搞不清楚我到底做错了什么。我看过这里的其他帖子,也问过类似的问题,但还是没有答案。

afdcj2ne

afdcj2ne1#

我使用了相同的教程,他们没有安装和运行rabbitmq的依赖
在执行brew install rabbitmqbrew services start rabbitmq之后,连接到Pika上的localhost就可以工作了

nbnkbykc

nbnkbykc2#

如果你使用docker来运行rabbitmq,并按照教程和docker页面(https://github.com/docker-library/docs/tree/master/rabbitmq)中的说明操作,你可能会遇到这个问题。当你运行容器时没有指定端口Map选项(“-p”),端口绑定将只在容器内有效。你可以通过在容器中执行“docker exec”然后运行netstat来验证。
所以你需要做的是重新启动rabbitmq容器并指定一个端口Map。例如:docker run -d --hostname my-rabbit --name some-rabbit -p 5672:5672 rabbitmq:latest

dfddblmv

dfddblmv3#

您是否使用docker运行rabbitmq?如果是,建议您仔细检查端口绑定。例如:- 第5672页:5672

wwodge7n

wwodge7n4#

以下步骤为我修复了它:
1.在您的终端中,运行brew info rabbitmq。它应该显示是否安装了所有依赖项(安装缺少的任何依赖项):
1.确保你的usr目录中有你的***.zshrc***文件。如果没有,在终端中运行touch ~/.zshrc
1.在.zshrc文件中,确保有以下两行之一:
(Mac Intel)export PATH=$PATH:/usr/local/sbin
(MAC硅)export PATH=$PATH:/opt/homebrew/sbin
1.最后,在您的终端运行brew services restart rabbitmq并再次测试

相关问题