【问题标题】:Python RabbitMQ sender.py remote queuePython RabbitMQ sender.py 远程队列
【发布时间】:2014-11-10 17:08:21
【问题描述】:

我正在尝试向远程机器中的 rabbit mq 队列发送消息。我正在关注this 教程。

连接到 localhost 的程序运行良好。但是连接到远程队列的程序不起作用。错误一定是在创建连接时出现的,因为我看不到“连接已创建”的日志消息。

我已经验证我可以从我的机器访问远程主机、端口并且凭据是正确的。我可以访问

http://remote-host:15672/#/queues

我是否遗漏了什么明显的东西?

本地

#!/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()

远程

#!/usr/bin/env python
import pika

# this queue is the destination queue
credentials = pika.PlainCredentials('xxxx', 'xxxx')
parameters = pika.ConnectionParameters('remote-host', 15672, '/', credentials)
connection = pika.BlockingConnection(parameters)
print " connection created"

channel = connection.channel()
channel.queue_declare(queue='hello')

channel.basic_publish(exchange='helloEx', routing_key='', body='Hello World!')
print " [x] Sent 'Hello World!'"
connection.close()

更新 这是我在尝试连接时遇到的错误。

ERROR:pika.adapters.base_connection:Socket Error on fd 3: 54 Traceback (most recent call last): File "remote-sender.py", line 10, in connection = pika.BlockingConnection(parameters) File "/Library/Python/2.7/site-packages/pika/adapters/base_connection.py", line 61, in init super(BaseConnection, self).init(parameters, on_open_callback) File "/Library/Python/2.7/site-packages/pika/connection.py", line 513, in init self._connect() File "/Library/Python/2.7/site-packages/pika/connection.py", line 804, in _connect self._adapter_connect() File "/Library/Python/2.7/site-packages/pika/adapters/blocking_connection.py", line 146, in _adapter_connect self.process_data_events() File "/Library/Python/2.7/site-packages/pika/adapters/blocking_connection.py", line 88, in process_data_events if self._handle_read(): File "/Library/Python/2.7/site-packages/pika/adapters/blocking_connection.py", line 184, in _handle_read super(BlockingConnection, self)._handle_read() File "/Library/Python/2.7/site-packages/pika/adapters/base_connection.py", line 300, in _handle_read return self._handle_error(error) File "/Library/Python/2.7/site-packages/pika/adapters/base_connection.py", line 264, in _handle_error self._handle_disconnect() File "/Library/Python/2.7/site-packages/pika/adapters/blocking_connection.py", line 181, in _handle_disconnect self._on_connection_closed(None, True) File "/Library/Python/2.7/site-packages/pika/adapters/blocking_connection.py", line 235, in _on_connection_closed raise exceptions.AMQPConnectionError(*self.closing) pika.exceptions.AMQPConnectionError: (0, '')

【问题讨论】:

标签: python rabbitmq pika


【解决方案1】:

使用 5672 端口而不是 15672 ! 它应该工作!

【讨论】:

  • 我做到了。但我得到了同样的结果。我确认我可以访问该端口。用我得到的错误更新了问题。知道可能是什么问题吗?谢谢!
  • @ravindrab,对不起,我修改了端口,正确的是 5672!
  • 我尝试使用正确的端口 5672。用我看到的错误更新了答案。
  • 好吧,如果不是防火墙问题,你应该检查rabbitmq日志,看看连接是否到达RabbitMQ。我建议阅读这篇文章:github.com/pika/pika/issues/266
  • 问题是我没有指定虚拟主机。 parameters = pika.ConnectionParameters('remote-host', 5672, 'product', credentials) 成功了。
【解决方案2】:

需要正确指定虚拟主机。就我而言,我的队列属于虚拟主机。但我试图连接到'/'。指定虚拟主机后(product 下面)我能够成功连接。

发现这个link 很有帮助。

#!/usr/bin/env python
import pika

# this queue is the destination queue
credentials = pika.PlainCredentials('xxxx', 'xxxx')
parameters = pika.ConnectionParameters('remote-host', 5672, 'product', credentials)
connection = pika.BlockingConnection(parameters)
print " connection created"

channel = connection.channel()
channel.queue_declare(queue='hello')

channel.basic_publish(exchange='helloEx', routing_key='', body='Hello World!')
print " [x] Sent 'Hello World!'"
connection.close()

【讨论】:

  • 我正在尝试在我的本地主机上使用 RabbitMQ,但使用起来很困难,你能给我任何关于此的指示
  • 您的具体问题是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-17
  • 1970-01-01
  • 2021-06-14
  • 2018-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多