尝试连接到RabbitMQ时出现IncompatibleProtocolError

时间:2016-03-03 13:40:16

标签: python rabbitmq pika

我遇到了使用pika从Python代码连接到dockerized RabbitMQ的问题。 我正在使用此代码连接到队列:

@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000, stop_max_attempt_number=2)
def rabbit_connect():
    connection_uri = cfg.get("System", "rabbit_uri", raw=True)
    queue = cfg.get("System", "queue")
    username = cfg.get("System", "username")
    password = cfg.get("System", "password")
    host = cfg.get("System", "rabbit_host")
    port = cfg.get("System", "rabbit_port")
    credentials = pika.PlainCredentials(username, password)
    log.info("Connecting  queue %s at %s:%s", queue, host, port)
    connection = None
    try:
        connection = pika.BlockingConnection(pika.ConnectionParameters(credentials=credentials, host=host, port=int(port)))
    except Exception, e:
        log.error("Can't connect to RabbitMQ")
        log.error(e.message)
        raise

这些是我的泊坞容器:

root@pc:~# docker ps
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                                                    NAMES
2063ad939823        rabbitmq:3-management   "/docker-entrypoint.s"   About an hour ago   Up About an hour    4369/tcp, 5671-5672/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:8080->15672/tcp   new-rabbitmg
94628f1fb33f        rabbitmq                "/docker-entrypoint.s"   About an hour ago   Up About an hour    4369/tcp, 5671-5672/tcp, 25672/tcp                                       new-rabbit

当我尝试使用任何可用凭据连接到localhost:8080时,pika会重试连接,直到出现错误:

Traceback (most recent call last):
  File "script.py", line 146, in worker
    connection = rabbit_connect()
  File "build/bdist.linux-x86_64/egg/retrying.py", line 49, in wrapped_f
    return Retrying(*dargs, **dkw).call(f, *args, **kw)
  File "build/bdist.linux-x86_64/egg/retrying.py", line 212, in call
    raise attempt.get()
  File "build/bdist.linux-x86_64/egg/retrying.py", line 247, in get
    six.reraise(self.value[0], self.value[1], self.value[2])
  File "build/bdist.linux-x86_64/egg/retrying.py", line 200, in call
    attempt = Attempt(fn(*args, **kwargs), attempt_number, False)
  File "script.py", line 175, in rabbit_connect
    connection = pika.BlockingConnection(pika.ConnectionParameters(credentials=credentials, host=host, port=int(port)))
  File "build/bdist.linux-x86_64/egg/pika/adapters/blocking_connection.py", line 339, in __init__
    self._process_io_for_connection_setup()
  File "build/bdist.linux-x86_64/egg/pika/adapters/blocking_connection.py", line 374, in _process_io_for_connection_setup
    self._open_error_result.is_ready)
  File "build/bdist.linux-x86_64/egg/pika/adapters/blocking_connection.py", line 410, in _flush_output
    self._impl.ioloop.poll()
  File "build/bdist.linux-x86_64/egg/pika/adapters/select_connection.py", line 602, in poll
    self._process_fd_events(fd_event_map, write_only)
  File "build/bdist.linux-x86_64/egg/pika/adapters/select_connection.py", line 443, in _process_fd_events
    handler(fileno, events, write_only=write_only)
  File "build/bdist.linux-x86_64/egg/pika/adapters/base_connection.py", line 364, in _handle_events
    self._handle_read()
  File "build/bdist.linux-x86_64/egg/pika/adapters/base_connection.py", line 412, in _handle_read
    return self._handle_disconnect()
  File "build/bdist.linux-x86_64/egg/pika/adapters/base_connection.py", line 288, in _handle_disconnect
    self._adapter_disconnect()
  File "build/bdist.linux-x86_64/egg/pika/adapters/select_connection.py", line 95, in _adapter_disconnect
    super(SelectConnection, self)._adapter_disconnect()
  File "build/bdist.linux-x86_64/egg/pika/adapters/base_connection.py", line 154, in _adapter_disconnect
    self._check_state_on_disconnect()
  File "build/bdist.linux-x86_64/egg/pika/adapters/base_connection.py", line 169, in _check_state_on_disconnect
    raise exceptions.IncompatibleProtocolError
IncompatibleProtocolError

是某种虫子吗?或者我做的事情不对吗?

1 个答案:

答案 0 :(得分:3)

您已将localhost:8080映射到docker container' s(new-rabbitmg)端口15672,这实际上是webui管理的端口。 amqp通信的端口是5672或5671。