rabbitmq 的心跳机制&应用

时间:2022-10-06 17:28:21

官方文档说:

If a consumer dies (its channel is closed, connection is closed, or TCP connection is lost) without sending an ack, RabbitMQ will understand that a message wasn't processed fully and will re-queue it

即: 如果消费者进程挂掉了(channel关闭, connection关闭,或者tcp连接丢失), 没有发回确认信息, RMQ将认为消息没有被处理完, 将重新排队等待分配。

but how?

答案是:通过心跳来监控。

官方文档在这里:https://www.rabbitmq.com/heartbeats.html

摘要如下:

=====================================================================

Detecting Dead TCP Connections with Heartbeats

用心跳监控tcp连接是否丢失

Introduction

介绍

Network can fail in many ways, sometimes pretty subtle (e.g. high ratio packet loss). Disrupted TCP connections take a moderately long time (about 11 minutes with default configuration on Linux, for example) to be detected by the operating system. AMQP 0-9-1 offers a heartbeat feature to ensure that the application layer promptly finds out about disrupted connections (and also completely unresponsive peers). Heartbeats also defend against certain network equipment which may terminate "idle" TCP connections.

网络故障很多种,有时很微妙(比如,丢包比率和高)。 分布式的tcp连接采取适中的时间(比如Linux默认配置大约11分钟),方便操作系统检测。AMQP 0-9-1提供heartbeat(心跳)特性来确保应用服务层及时发现已崩溃的连接(和完全无相应的peers)。 心跳机制也能保证进程不被某些网络设备给杀掉。

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).

心跳超时值决定了tcp相互连接的最大时间, 超过了这个时间, 该连接即被RMQ和客户端视为丢失(dead)。 这个值在客户端和服务器建立连接的时候协商确定。客户端需配才能发心跳包。 RMQ3.0及以上版本, RMQ将试着将beatheart协调为默认值(客户端可以否决这个值)。 超时时间单位为秒,默认值为60( 3.5.5发布版之前是580)。

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.

心跳包每半个超时时间发送一次。 丢失了两个心跳包, 连接被认为不可抵达。 不同的客户端有不同的提示, 但tcp连接都会被关闭。 当客户端检测到RMQ节点不可抵达(根据心跳判定), 它需要重新连接(到服务器)。

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

心跳机制可以被禁用:设定超时间隔为0。

转载请注明本文源头:http://www.cnblogs.com/Tommy-Yu/p/5775852.html

谢谢!