本文主要介绍RabbitmqheartbeaBEA心跳检测机制的原理分析,通过示例代码进行了非常详细的介绍,对大家的学习或者工作有一定的参考价值。有需要的朋友可以参考一下。
前言
使用rabbitmq时,当你的客户端和rabbitmq服务器之间有一段时间没有流量时,服务器会断开与客户端的tcp连接。
您将在服务器上看到这个日志:
缺少来自客户端的心跳,超时:xxs
这个间隔就是心跳间隔。
Heartbeat通常用于检测通信的对等体是否活着(套接字连接异常关闭导致的异常崩溃)。其基本原理是检测相应socket连接上的数据是否在正常收发。如果一段时间内没有数据接收和发送,将向对等体发送心跳检测包。如果一段时间内没有响应,则认为心跳超时,即对等体可能出现异常崩溃。
Rabbitmq也不例外。在客户端和服务器之间使用heatbeat来检测对等体是否正常,即客户端和服务器之间的tcp链接是否正常。
关于rabbitmq心跳
1.心跳检测时间间隔可以通过在配置文件rabbitmq.config中添加配置项{heartbeat,Timeout}来配置,其中Timeout指定的是以秒为单位的时间间隔,客户端也可以配置心跳时间。
如果服务器未配置
默认代理心跳时间:
兔子MQ3.2.2:580秒
兔子MQ3.5.5:60秒
2.官方建议不要禁用心跳,建议心跳时间为60秒。
3.每隔心跳超时/2秒发送一次心跳。如果服务器两次接收失败,tcp连接将被断开。以前的连接将无效,客户端需要重新连接。
4.如果你使用Java。NET和Erlang客户端,服务器和客户端将协商心跳时间。
如果其中一个值为0,则使用其中较大的一个
否则,使用两者中较小的一个
两个值都是0,这意味着应该禁用heartbeat,因此服务器和客户端将保持这个tcp连接,不会断开连接。
注意:如果在python客户端直接设置为0,心跳将被禁用。
如何在python客户端中设置心跳禁用:
在py3:ConnectionParameters中设置heartbeat_interval=0。
在py2:ConnectionParameters中设置heartbeat=0。
5.连接上的任何流量(传输的有效数据、确认等。)将被计为有效心跳,当然也包括心跳帧。
6.在网上看到有人问这个问题:
为什么服务器停机了?在心跳检测机制下,服务器端断网,客户端检测不到tcp断网。我已经测试过了。客户端真的检测不到tcp断网,只有客户端在这个tcp上 *** 作才能检测到。当然,在断开的tcp连接上 *** 作会出错(比如发送消息)。
importpika importtime credit=pika.PlainCredentials(username='cloud',password='cloud') connection=pika.BlockingConnection(pika.ConnectionParameters( host='10.32.1.12',credentials=credit)) channel=connection.channel() whileTrue: connect_close=connection.is_closed connect_open=connection.is_open channel_close=channel.is_closed channel_open=channel.is_open print("connectionis_closed",connect_close) print("connectionis_open",connect_open) print("channelis_closed",channel_close) print("channelis_open",channel_open) print("") time.sleep(5)7.一些RabbitMQ客户端(Bunny,Java,。NET、Objective-C、Swift)提供了网络故障后自动恢复连接的机制,而pika只能在检测到连接异常后重新创建连接。
示例:通过检测异常连接来重新创建连接:
importpika whileTrue: try: connection=pika.BlockingConnection() channel=connection.channel() channel.basic_consume('test',on_message_callback) channel.start_consuming() #Don'trecoverifconnectionwasclosedbybroker exceptpika.exceptions.ConnectionClosedByBroker: break #Don'trecoveronchannelerrors exceptpika.exceptions.AMQPChannelError: break #Recoveronallotherconnectionerrors exceptpika.exceptions.AMQPConnectionError: continue也可以使用 *** 作重试库,比如retry。
fromretryimportretry @retry(pika.exceptions.AMQPConnectionError,delay=5,jitter=(1,3)) defconsume(): connection=pika.BlockingConnection() channel=connection.channel() channel.basic_consume('test',on_message_callback) try: channel.start_consuming() #Don'trecoverconnectionsclosedbyserver exceptpika.exceptions.ConnectionClosedByBroker: pass consume()心跳的实现
从客户端收到connection.tune-ok信令后,rabbitmq将启用心跳检测。rabbitmq将为每个用于心跳检测的tcp连接创建两个进程。一个进程会定期检测tcp连接上是否有数据传输(这里的传输是指rabbitmq向客户端发送数据)。如果在一段时间内没有数据发送到客户端,它将向客户端发送心跳包,然后循环进行下一次检测。另一个进程定期检测tcp连接上是否接收到数据。如果一段时间没有收到数据,则判断心跳超时,tcp连接最终会关闭。另外rabbitmq的流量控制机制可能会暂停心跳检测,这里不做描述。
涉及的源代码:
start(SupPid,Sock,SendTimeoutSec, SendFun,ReceiveTimeoutSec,ReceiveFun)-> %%数据发送检测进程 {ok,Sender}=start_heartbeater(SendTimeoutSec,SupPid,Sock, SendFun,heartbeat_sender, start_heartbeat_sender), %%数据接收检测进程 {ok,Receiver}=start_heartbeater(ReceiveTimeoutSec,SupPid, Sock,ReceiveFun, heartbeat_receiver, start_heartbeat_receiver), {Sender,Receiver}. start_heartbeat_sender(Sock,TimeoutSec,SendFun)-> %%the'div2'istheresothatwedon'tendupwaitingfor %%nearly2*TimeoutSecbeforesendingaheartbeatinthe %%boundarycase heartbeater({Sock,TimeoutSec*1000div2,send_oct,0, fun()->SendFun(),continueend}). start_heartbeat_receiver(Sock,TimeoutSec,ReceiveFun)-> %%wecheckforincomingdataeveryinterval,andtimeoutafter %%twocheckswithnochange.Asaresultwewilltimeout %%between2and3intervalsafterthelastdatahasbeen %%received heartbeater({Sock,TimeoutSec*1000,recv_oct,1, fun()->ReceiveFun(),stopend}). heartbeater({Sock,TimeoutMillisec, StatName,Threshold,Handler}=Params, Deb, {StatVal,SameCount}=State)-> Recurse=fun(State1)->heartbeater(Params,Deb,State1)end, receive ... %%定时检测 afterTimeoutMillisec-> caserabbit_net:getstat(Sock,[StatName])of {ok,[{StatName,NewStatVal}]}-> %%收发数据有变化 ifNewStatVal=/=StatVal-> %%重新开始检测 Recurse({NewStatVal,0}); %%未达到指定次数,发送为0,接收为1 SameCount<Threshold-> %%计数加1,再次检测 Recurse({NewStatVal,SameCount+1}); %%heartbeat超时 true-> %%对于发送检测超时,向客户端发送heartbeat包 %%对于接收检测超时,向父进程发送超时通知 %%由父进程触发tcp关闭等 *** 作 caseHandler()of %%接收检测超时 stop->ok; %%发送检测超时 continue->Recurse({NewStatVal,0}) end; ...inet模块的Getstat用于在检测过程中检查套接字的统计信息。
Recv_oct:检查套接字上接收的字节数
Send_oct:检查套接字上发送的字节数
点击这里查看inet的详细信息:http://www.erlang.org/doc/man/inet.html.
这就是本文的全部内容。希望对大家的学习有帮助,支持我们。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)