pika.exceptions.ConnectionClosed:连接到 172.18.0.3:5672 失败:[Errno 111] 连接被拒绝

pika.exceptions.ConnectionClosed: Connection to 172.18.0.3:5672 failed: [Errno 111] Connection refused

我正在尝试将 Pika 客户端与 RabbitMQ 服务器连接起来。它们都在不同的 docker 图像上,并且它们在同一个 docker 网络上。 我将我的网络设置为

docker network create my_first_net

我运行将 RabbitMQ 图像 rabbitmq 设置为

docker run -d --network my_first_net --hostname rabbitmqhost  -p 35672:15672 -p 45672:5672 rabbitmq

我正在 运行 将 Pika 图片 ms2-1 设为

docker run --network my_first_net --hostname rabbitmq ms2-1

这是 pika-client 的代码 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 | 监听端口 5672 | 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

出现问题是因为您在 运行 鼠兔容器

时重新使用相同的主机名
docker run --network my_first_net --hostname rabbitmq ms2-1

您不应设置与 rabbitmq 容器名称相同的主机名。我 运行 相同的步骤,我能够连接

我遇到了同样的问题。 通过调整参数“socket_timeout”我能够摆脱这个问题。参数说明为here

这是将 socket_timeout 设置为 15 秒的代码片段:

connection=pika.BlockingConnection(pika.ConnectionParameters(host='xx.xx.xx.xx',socket_timeout=15,credentials=credentials))

xx.xx.xx.xx : 放置你的主机IP。