当前位置: 首页 > 科技观察

通过错误,理解 HAProxy 负载均衡下的 RabbitMQ

时间:2023-03-18 23:30:26 科技观察

通过通过,理解haproxy负载的的的的:2018-05-0411:21:48.116error60848---[。168.0.202:8001]:48.116ERROR60848---[.168.0.202:8001]o.s.a.r.c.CachingConnectionFactory:Channelshutdown:connectionerror2018-05-0411:21:48.125INFO60848---[nge.consumer1-8]o.s.a.r.l.SimpleMessageListenerConsumer@7{RestartingListenerConsumer1:7amq.ctag-d_wIlZIGxM3f0fsxkmYQfA=my_test_exchange.consumer1}],channel=CachedRabbitChannel:AMQChannel(amqp://admin@192.168.0.202:8001/,1),conn:Proxy@25a73de1SharedRabbitConnection:SimpleConnection@25fca927[委托=amqpadmin@192.168.0.202:8001/,localPort=56258],acknowledgeMode=AUTOlocalqueuesize=02018-05-0411:21:48.126INFO60848---[nge.consumer1-9]o.s.a.r.c.CachingConnectionFactory:Attemptingtoconnectto:[manager1:8180-1]205-0411:21:48.393INFO60848---[nge.consumer1-9]o.s.a.r.c.CachingConnectionFactory:Creatednewconnection:rabbitConnectionFactory#2b8bd14b:12/SimpleConnection@3fb9795a[delegate=amqp://admin@192.168.0.202:8001/,localPort=56260]2018-05-0411:21:49.059INFO60848---[nge.consumer1-8]o.s.a.r.l.SimpleMessageListenerContainer:RestartingConsumer@58b42519:标签=[{amq.ctag-T1HyrOd5Ykr_VQZDwxRslA=stream_exchange.consumer1}],channel=CachedRabbitChannel:AMQChannel(amqp://admin@192.168.0.202:8001/2ion@SimpleConnection@253fb9795a[delegate=am/admin@192.168.0.202:8001/,localPort=56260],acknowledgeMode=AUTOlocalqueuesize=0SpringBoot配置RabbitMQ(使用HAProxy负载均衡):spring:application:name:stream-rabbitmq-producerrabbitmq:host:manager1port:8001username:adminpassword:admin123456最近在使用RabbitMQ集群(HAProxy负载均衡)时,经常出现上面的错误信息,但是可以正常消费消息,如果只使用单机版的RabbitMQ(不使用HAProxy),没有任何这个问题困扰很久了,google了很多资料化,但没有找到解决方案。无意中找到一篇文章:RabbitMQandHAProxy:atimeoutissue文章说如果你使用HAProxy配置RabbitMQ高可用集群,那么你会遇到客户端连接超时的问题。为什么会出现这个问题?因为HAProxy配置了客户端连接超时参数(timeoutclientms),如果客户端连接超过配置的参数,那么HAProxy会删除客户端连接。RabbitMQ客户端使用***连接broker,从不超时,为什么还是有问题?因为如果RabbitMQ一段时间不活跃,那么HAProxy会自动关闭连接(有点坑??)。那么如何解决这个问题呢?我们看到HAProxy提供了一个clitcpka参数配置,可以从客户端发送TCPkeepalive包。我们使用了,但是发现配置之后,还是出现了上面的问题。为什么?[...]cpkeep-alive的确切行为由底层操作系统/内核配置决定[...]这是什么意思?这意味着TCPkeepalive数据包的发送取决于操作系统/内核配置。我们可以使用命令查看(HAProxy所在服务器中的tcp_keepalive_time配置):[root@manager1~]#cat/proc/sys/net/ipv4/tcp_keepalive_time7200tcp_keepalive_time默认配置时间为2小时,表示间隔发送TCPkeepalive数据包的间隔为2小时,或者每2小时发送一次TCPkeepalive数据包。这就很清楚了。虽然我们在HAProxy中配置了clitcpka参数,但是系统发送TCPkeepalive包的时间间隔太长,远远超过了HAProxy中的timeoutclienttimeout(默认好像是2秒),因此无情的删除了client连接每2秒由HAProxy执行一次,然后不断重建。说了这么多,我们如何解决这个问题呢?两种解决方案:修改系统的tcp_keepalive_time配置,间隔时间低于HAProxy配置的timeout客户端超时时间(因为可能影响其他系统服务,不推荐)。修改HAProxy中的timeoutclienttimeout,配置成大于系统的tcp_keepalive_time间隔(推荐)因为系统tcp_keepalive_time每隔2小时发送一次TCPkeepalive包,所以我们将HAProxy中的timeoutclienttimeout设置为3小时:timeoutclient3htimeoutserver3h完整示例配置:[root@manager1~]#cat/etc/haproxy/haproxy.cfggloballog127.0.0.1local0infogloballog127.0.0.1local1noticedaemonglobalmaxconn4096defaultslogglobalmodetcpoptiontcplogoptiondontlognullretries3optionabortonclosemaxconn4096timeoutconnect5000mstimeoutclient3000msglobaltimeoutserver3000msbalanceroundrobinlistenprivate_monitoringbind0.0.0.0:8000modehttpoptionhttplogstatsrefresh5sstatsuri/statsstatsrealmHaproxystatsauthadmin:adminlistenrabbitmq_adminbind0.0.0.0:8002servermanager1manager1:15672servermanager2manager2:15672servermanager3manager3:15672listenrabbitmq_clusterbind0.0.0.0:8001modetcpoptiontcplogbalancoroundrobintimeoutclient3htimeoutserver3hservermanager1manager1:5672checkinter5000rise2fall3servermanager2manager2:5672checkinter5000rimanage2fall3r3manager3:5672checkinter5000rise2fall3重新运行HAProxy,然后RabbitMQ测试成功: