Pika BlockingConnection&RabbitMQ:连接已关闭

2024-06-11 07:18:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我在集群中有2个rabbitmq,由Azure内部负载平衡器进行负载平衡。客户端使用BlockingConnection连接到LB。

当客户交换信息时,一切正常。但当没有活动时,我的客户端似乎断开连接,无法再接收消息。

我想知道这个问题是否有解决办法?我假设负载平衡器或rabbitmq由于不活动而关闭连接。我想让pika触发rabbitmq的心跳(以便负载平衡器保持连接打开),但没有找到任何好的解决方案。你能给我个建议吗?

编辑1 似乎pika阻塞连接不支持心跳。Heart beat disables blocking connection

谢谢你。


Tags: 信息消息编辑客户端客户rabbitmq集群解决方案
1条回答
网友
1楼 · 发布于 2024-06-11 07:18:56

根据Pika文档http://pika.readthedocs.org/en/0.10.0/modules/parameters.html,Pika阻塞与^{}指定的heart_interval(如amqps://www-data:rabbit_pwd@rabbit1/web_messages?heartbeat_interval=30)的连接可以保持打开,但heart_interval的值不能大于建议的rabbit server的值。

Heartbeat Timeout Interval

The heartbeat timeout value defines after what period of time the peer TCP connection should be considered dead by RabbitMQ and client libraries. This value is negotiated between the client and RabbitMQ server at the time of connection. The client must be configured to request heartbeats. In RabbitMQ versions 3.0 and higher, the broker will attempt to negotiate heartbeats by default (although the client can still veto them). The timeout is in seconds, and default value is 60 (580 prior to release 3.5.5).

Heartbeat frames are sent about every timeout / 2 seconds. After two missed heartbeats, the peer is considered to be unreachable. Different clients manifest this differently but the TCP connection will be closed. When a client detects that RabbitMQ node is unreachable due to a heartbeat, it needs to re-connect.

Heartbeats can be disabled by setting the timeout interval to 0.

Pika文档中的示例代码:

import pika

parameters = pika.URLParameters('amqps://www-data:rabbit_pwd@rabbit1/web_messages?heartbeat_interval=30')
connection = pika.BlockingConnection(parameters)

相关问题 更多 >