一段时间后,RabbitMQ因某种原因关闭连接。

3

我正在使用pika Python库连接到本地的RabbitMQ服务器。

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

兔子队列服务器日志
=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。

我不明白发生了什么,请给我解释一下。


在发布问题时提供一些平台信息、版本等是一个好习惯。 - pinepain
3个回答

3

相关日志行: {心跳超时,运行}。

某些原因阻止了BlockingConnection发送心跳,因此RabbitMQ认为您的客户端无法到达或已经死亡。您有以下3个选择:

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

2

实际问题是我停止了rabbitmq-server。而pika无法处理断开连接。


1
我有一些测试需要运行。当我在本地主机上运行时,一切正常。但是,当我在服务器上运行测试时,我遇到了与此相同的问题和情况。我检查了我的 pika 版本,它是 0.9.13。顺便说一下,我也在使用 BlockingConnection
首先,我尝试定期调用 process_data_events()。但是它没有起作用。 其次,我尝试每次发布消息或打开新队列时打开新的连接和通道。但是它也没有起作用。 第三,我尝试升级 pika0.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中的一个错误。我比较了版本:在本地主机上它是最新的(3.6.5),在我的服务器上它是2.8.4。为了验证这是实际问题,我运行了测试,但使用了一个更高版本的远程rabbitmq。一切都正常。因此,我升级了rabbitmq-server,问题消失了!
简而言之:
解决方案是升级你的rabbitmq-server

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接