-2
我试图用两个主机连接RabbitMQ & python pika。尝试连接到远程RabbitMQ服务器时出现AccessDeniedError
这里是工人:
#!/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')")
我检查了主机之间有iperf
为UDP和TCP二者在两个方向上的连接:
iperf -s -p 5672
iperf -p 5672 -c 192.168.0.2
所以交通去。
我很满意,可能会有什么问题?
是的,这仅仅是一个例子,当然有真正的ip而不是那些字符串,为了清晰起见,我会通过IP更改这个字符串 – Kirill
@Kirill然后这个问题就变成了关于网络而不是编程。检查防火墙设置,相关的端口转发等。 – DeepSpace
我检查了iperf与RabbitMQ端口的连接,一切正常。 – Kirill