尝试连接到远程RabbitMQ服务器时出现AccessDeniedError

时间:2017-10-12 23:03:17

标签: python rabbitmq rpc pika

我正在尝试用RabbitMQ连接两台主机&蟒蛇皮卡。

这是工人:

#!/usr/bin/env python
import pika, time
NEW_TASK_HOST_IP = '192.168.0.2'
credentials = pika.PlainCredentials('login-to-remote', 'pass')
connection = pika.BlockingConnection(
                    pika.ConnectionParameters(host=NEW_TASK_HOST_IP))
channel = connection.channel()

channel.queue_declare(queue='task_queue', durable=True)
print(' [*] Waiting for messages. To exit press CTRL+C')

def callback(ch, method, properties, body):
    ch.basic_ack(delivery_tag = method.delivery_tag)

channel.basic_qos(prefetch_count=1)
channel.basic_consume(callback,
                      queue='task_queue')

这是新任务:

#!/usr/bin/env python
import pika, sys
WORKER_IP = '192.168.0.3'
credentials = pika.PlainCredentials('login-to-remote', 'pass')
connection = pika.BlockingConnection(pika.ConnectionParameters(
        host=WORKER_IP, socket_timeout=300, credentials=credentials))
channel = connection.channel()

channel.queue_declare(queue='task_queue', durable=True)

message = ' '.join(sys.argv[1:]) or "Hello World!"
channel.basic_publish(exchange='',
                      routing_key='task_queue',
                      body=message,
                      properties=pika.BasicProperties(
                         delivery_mode = 2, # make message persistent
                      ))
print(" [x] Sent %r" % message)
connection.close()

我使用命令在两台主机上创建了两个用户:

sudo rabbitmqctl add_user login-to-remote pass

当我试图运行任何我得到的东西时:

Traceback (most recent call last):
  File "worker.py", line 5, in <module>
    connection = pika.BlockingConnection(pika.ConnectionParameters(host=NEW_TASK_HOST_IP, socket_timeout=300, credentials=credentials))
  File "/home/anna/.local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 374, in __init__
    self._process_io_for_connection_setup()
  File "/home/anna/.local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 414, in _process_io_for_connection_setup
    self._open_error_result.is_ready)
  File "/home/anna/.local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 466, in _flush_output
    raise maybe_exception
pika.exceptions.ProbableAccessDeniedError: (-1, "error(104, 'Connection reset by peer')")

我已经检查了两个方向上udp和tcp的主机与iperf之间的连接:

iperf -s -p 5672
iperf -p 5672 -c 192.168.0.2

所以流量就这样了。

我有点堆叠,可能有什么问题?

1 个答案:

答案 0 :(得分:1)

connection = pika.BlockingConnection(
                    pika.ConnectionParameters(host='new-task-host-ip'))

.
.
.

connection = pika.BlockingConnection(pika.ConnectionParameters(
    host='worker-ip', socket_timeout=300, credentials=credentials))

'new-task-host-ip''worker-ip'是无效的IP地址。您需要将这些替换为主机的实际IP地址(可能是'localhost''127.0.0.1)。