2017-10-12 208 views
-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 

所以交通去。

我很滿意,可能會有什麼問題?

回答

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)。

+0

是的,這僅僅是一個例子,當然有真正的ip而不是那些字符串,爲了清晰起見,我會通過IP更改這個字符串 – Kirill

+0

@Kirill然後這個問題就變成了關於網絡而不是編程。檢查防火牆設置,相關的端口轉發等。 – DeepSpace

+0

我檢查了iperf與RabbitMQ端口的連接,一切正常。 – Kirill

相關問題