Pika BlockingConnection &RabbitMQ:连接关闭
问题描述
我在集群中有 2 个 rabbitmq,由 Azure 内部负载均衡器进行负载均衡.客户端使用BlockingConnection
连接到LB.
I have 2 rabbitmq in cluster load balanced by an Azure Internal Load Balancer. The client connect to the LB with BlockingConnection
.
当客户端交换消息时,一切正常.但是当没有活动时,我的客户似乎已断开连接并且无法再接收消息.
When there a are message exchanged by the clients, everything works fine. But when there is no activity, it seems my clients are disconnected and and not able to receive messages anymore.
我想知道这个问题是否有解决方案?我假设负载均衡器或 rabbitmq 由于不活动而关闭连接.我想让 pika 触发 rabbitmq 的一些心跳(以便负载均衡器保持连接打开),但没有找到任何好的解决方案.能给个建议吗?
I am wondering if there is a solution to this problem ? I assume the load balancer or rabbitmq is closing the connection due to inactivity. I wanted to let pika trigger some heartbeat to rabbitmq (so that the load balancer keep the connection opened) but didn't find any good solution. Could you advice ?
编辑 1看来 pika BlockingConnections 不支持心跳.心跳禁用阻塞连接
EDIT 1 It seems pika BlockingConnections does not support heartbeat. Heart beat disables blocking connection
谢谢.
解决方案
根据 Pika 文档 http://pika.readthedocs.org/en/0.10.0/modules/parameters.html,看来 Pika BlockingConnentions 与 URLParameters
指定heart_interval
(如amqps://www-data:rabbit_pwd@rabbit1/web_messages?heartbeat_interval=30
) 可以保持连接打开,但是heart_interval的值不能大于rabbit server建议的值.
According to the Pika doc http://pika.readthedocs.org/en/0.10.0/modules/parameters.html, it seems Pika BlockingConnentions with URLParameters
specified heart_interval
(such as amqps://www-data:rabbit_pwd@rabbit1/web_messages?heartbeat_interval=30
) that can keep connection opened, but the value of heart_interval can not larger than the value of rabbit server suggested.
心跳超时间隔
心跳超时值定义了对等 TCP 连接应在多长时间后被 RabbitMQ 和客户端库视为死亡.该值在连接时在客户端和 RabbitMQ 服务器之间协商.客户端必须配置为请求心跳.在 RabbitMQ 3.0 及更高版本中,代理将默认尝试协商心跳(尽管客户端仍然可以否决它们).超时以秒为单位,默认值为 60(3.5.5 版本之前为 580).
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).
心跳帧大约每超时/2 秒发送一次.在两次错过的心跳之后,对等点被认为是不可达的.不同的客户端会以不同的方式显示这一点,但 TCP 连接将被关闭.当客户端检测到 RabbitMQ 节点由于心跳而无法访问时,需要重新连接.
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.
可以通过将超时间隔设置为 0 来禁用心跳.
Heartbeats can be disabled by setting the timeout interval to 0.
来自 Pika 文档的示例代码:
Sample code from Pika doc:
import pika
parameters = pika.URLParameters('amqps://www-data:rabbit_pwd@rabbit1/web_messages?heartbeat_interval=30')
connection = pika.BlockingConnection(parameters)
相关文章