我試圖連接一個Pika客戶端與RabbitMQ服務器。他們都在不同的碼頭圖像上,他們在同一個碼頭網絡上。 設置我的網絡pika.exceptions.ConnectionClosed:連接到172.18.0.3:5672失敗:[Errno 111]連接被拒絕
docker network create my_first_net
我運行的是RabbitMQ的圖像rabbitmq
作爲
docker run -d --network my_first_net --hostname rabbitmqhost -p 35672:15672 -p 45672:5672 rabbitmq
我運行鼠害的圖像ms2-1
作爲
docker run --network my_first_net --hostname rabbitmq ms2-1
這裏是高原鼠兔的代碼 - 客戶ms2.py
:
import pika
exchange = 'gateway_exchange'
myName = 'microservice2'
myKey = '#.ms2.#'
gwKey = 'gw'
credentials = pika.PlainCredentials('guest', 'guest')
parameters = pika.ConnectionParameters('rabbitmq', 5672, '/', credentials)
def send(key, message):
send_conn = pika.BlockingConnection(parameters)
send_ch = send_conn.channel()
send_ch.exchange_declare(exchange=exchange,
exchange_type='topic')
send_ch.basic_publish(exchange=exchange,
routing_key=key,
body=message)
print(" [x] Sent %r:%r" % (key, message))
send_conn.close()
def receive():
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
channel.exchange_declare(exchange=exchange,
exchange_type='topic')
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue
binding_key = myKey
channel.queue_bind(exchange=exchange,
queue=queue_name,
routing_key=binding_key)
print(' [*] Waiting for messages. To exit press CTRL+C')
def callback(ch, method, properties, body):
print(" [x] Received %r:%r" % (method.routing_key, body))
send('Response-from-' + myName + '-to-.' + gwKey, body)
channel.basic_consume(callback,
queue=queue_name,
no_ack=True)
channel.start_consuming()
if __name__ == "__main__":
receive()
我收到以下錯誤,當我嘗試運行鼠兔形象
Traceback (most recent call last):
File "ms2.py", line 61, in <module>
receive()
File "ms2.py", line 36, in receive
connection = pika.BlockingConnection(parameters)
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 374, in __init__
self._process_io_for_connection_setup()
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 414, in _process_io_for_connection_setup
self._open_error_result.is_ready)
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 468, in _flush_output
raise exceptions.ConnectionClosed(maybe_exception)
pika.exceptions.ConnectionClosed: Connection to 172.18.0.3:5672 failed: [Errno 111] Connection refused
的RabbitMQ的容器的IP地址是172.18.0.2
當我做docker inspect
,它是68.50.13.82
當我curl ifconfig.me
容器內。 我確認容器正在使用`netstat -ap tcp | grep -i「listen」
我認爲Errno 111與身份驗證錯誤有關,但我無法連接到來自我的計算機的任何IP地址。
我的下一步應該怎樣解決這個問題?
編輯:我意識到,在運行的RabbitMQ服務器雖然我沒有添加--name
標誌,所以我刪除容器,並開始一個新的爲:
docker run -d --network my_first_net --hostname rabbitmqhost --name rabbitmq -p 15672:15672 -p 5672:5672 rabbitmq
但現在我得到的錯誤:
Traceback (most recent call last):
File "ms2.py", line 61, in <module>
receive()
File "ms2.py", line 36, in receive
connection = pika.BlockingConnection(parameters)
File "/usr/local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 374, in __init__
self._process_io_for_connection_setup()
File "/usr/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 "/usr/local/lib/python2.7/site-packages/pika/adapters/blocking_connection.py", line 466, in _flush_output
raise maybe_exception
socket.gaierror: [Errno 8] nodename nor servname provided, or not known