【问题标题】:Connection closed in rabbitmq for some reason after some time一段时间后由于某种原因在rabbitmq中关闭了连接
【发布时间】:2014-03-05 08:59:35
【问题描述】:

我正在使用 pika python 库连接到本地主机上的 rabbitmq-server。

class BaseRabbitSender(MessageSender):
    __metaclass__ = ABCMeta

    def __init__(self, host):
        self.node = BaseMessagingNode(host)
        self.connection = pika.BlockingConnection(pika.ConnectionParameters(
            host=host))
        self.channel = self.connection.channel()

    @abstractmethod
    def send_message(self, message):
        pass

    def close_connection(self):
        self.connection.close()


class DirectRabbitSender(BaseRabbitSender):
    def __init__(self, host, queue_name):
        super(DirectRabbitSender, self).__init__(host)
        self.queue_name = queue_name
        self.channel.queue_declare(queue=queue_name, durable=True)

    def send_message(self, message):
        self.channel.basic_publish(exchange='',
                                   routing_key=self.queue_name,
                                   body=message,
                                   properties=pika.BasicProperties(
                                       delivery_mode=2,
                                   ))

    def close_connection(self):
        self.connection.close()

由于某种原因,在很长一段时间后(比如几天),我遇到了错误。

 File "build/bdist.linux-x86_64/egg/pika/adapters/blocking_connection.py", line 560, in basic_publish
    (properties, body), False)
  File "build/bdist.linux-x86_64/egg/pika/adapters/blocking_connection.py", line 1147, in _send_method
    self.connection.send_method(self.channel_number, method_frame, content)
  File "build/bdist.linux-x86_64/egg/pika/adapters/blocking_connection.py", line 267, in send_method
    self._send_method(channel_number, method_frame, content)
  File "build/bdist.linux-x86_64/egg/pika/connection.py", line 1504, in _send_method
    self._send_frame(frame.Header(channel_number, length, content[0]))
  File "build/bdist.linux-x86_64/egg/pika/adapters/blocking_connection.py", line 410, in _send_frame
    self.process_data_events()
  File "build/bdist.linux-x86_64/egg/pika/adapters/blocking_connection.py", line 236, in process_data_events
    raise exceptions.ConnectionClosed()
ConnectionClosed

Rabbitmq 服务器日志

=INFO REPORT==== 3-Mar-2014::15:11:03 ===
accepting AMQP connection <0.26625.0> (127.0.0.1:41846 -> 127.0.0.1:5672)

=ERROR REPORT==== 3-Mar-2014::15:38:12 ===
closing AMQP connection <0.326.0> (127.0.0.1:58580 -> 127.0.0.1:5672):
{heartbeat_timeout,running}

=WARNING REPORT==== 3-Mar-2014::16:11:04 ===
closing AMQP connection <0.26625.0> (127.0.0.1:41846 -> 127.0.0.1:5672):
connection_closed_abruptly

=INFO REPORT==== 3-Mar-2014::16:11:05 ===
accepting AMQP connection <0.27016.0> (127.0.0.1:37776 -> 127.0.0.1:5672)

=ERROR REPORT==== 3-Mar-2014::17:41:05 ===
closing AMQP connection <0.27016.0> (127.0.0.1:37776 -> 127.0.0.1:5672):
{heartbeat_timeout,running}

它在 ubuntu 13.10 上运行。 RabbitMQ 3.1.3

我不明白发生了什么。你能解释一下吗?

【问题讨论】:

  • 发布一些平台信息、版本等总是一个好主意。

标签: python rabbitmq pika


【解决方案1】:

相关日志行:{heartbeat_timeout,running}。

某些东西阻止了 BlockingConnection 发送心跳,因此 RabbitMQ 认为您的客户端无法访问或已死。您有 3 个选项:

  • 避免阻塞行为
  • 增加心跳间隔
  • 尝试其他连接实现,例如龙卷风。

【讨论】:

    【解决方案2】:

    嗯,实际的问题是我已经停止了rabbitmq-server。而且 pika 不处理断开连接。

    【讨论】:

      【解决方案3】:

      我有一些想要运行的测试。当我在本地主机中运行它时,一切都很好。但是,当我在服务器上运行测试时,我遇到了与这个完全相同的问题和情况。我检查了我的pika 版本,它是0.9.13。顺便说一句,我也在使用BlockingConnection。

      首先,我尝试定期致电process_data_events()。它没有用。
      其次,每当我发布消息或打开新队列时,我都会尝试打开新的连接和通道。它没有用。
      第三,我尝试将pika升级到0.9.14。没用。

      有人在这里讨论:https://github.com/pika/pika/issues/397 提到了socket 错误的可能性。因此,我检查了python版本,假设可能存在错误,并且在更高版本的python中已修复。在服务器中,python 版本是2.7.3,在我的本地主机中是2.7.12。为了测试python版本是否真的有问题,我安装了conda并创建了一个python版本为2.7.3的环境。我运行了测试并通过了(我无法重现该问题)。

      经过上述所有尝试后,我提出了另一个假设,即这可能是我服务器的rabbitmq-server 中的一个错误。我比较了版本:在 localhost 中是最新的 (3.6.5),在我的服务器中是 2.8.4。为了验证这是否是实际问题,我运行了测试,但我使用了带有更高版本的远程 rabbitmq。一切正常。所以,我升级了rabbitmq-server,结果问题消失了!

      TL;DR:
      解决方案是升级你的rabbitmq-server!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-02
        • 2021-11-18
        • 1970-01-01
        • 1970-01-01
        • 2020-03-04
        • 2022-01-01
        • 1970-01-01
        相关资源
        最近更新 更多