解决 RabbitMQ 集群 Channel shutdown: connection error 错误(HAProxy 负载均衡)

时间:2024-01-22 21:02:52

相关文章:搭建 RabbitMQ Server 高可用集群

具体错误信息:

2018-05-04 11:21:48.116 ERROR 60848 --- [.168.0.202:8001] o.s.a.r.c.CachingConnectionFactory       : Channel shutdown: connection error
2018-05-04 11:21:48.116 ERROR 60848 --- [.168.0.202:8001] o.s.a.r.c.CachingConnectionFactory       : Channel shutdown: connection error
2018-05-04 11:21:48.125  INFO 60848 --- [nge.consumer1-8] o.s.a.r.l.SimpleMessageListenerContainer : Restarting Consumer@5196e177: tags=[{amq.ctag-d_wIlZIGxM3f0fsxkmYQfA=my_test_exchange.consumer1}], channel=Cached Rabbit Channel: AMQChannel(amqp://admin@192.168.0.202:8001/,1), conn: Proxy@25a73de1 Shared Rabbit Connection: SimpleConnection@25fca927 [delegate=amqp://admin@192.168.0.202:8001/, localPort= 56258], acknowledgeMode=AUTO local queue size=0
2018-05-04 11:21:48.126  INFO 60848 --- [nge.consumer1-9] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [manager1:8001]
2018-05-04 11:21:48.393  INFO 60848 --- [nge.consumer1-9] o.s.a.r.c.CachingConnectionFactory       : Created new connection: rabbitConnectionFactory#2b8bd14b:12/SimpleConnection@3fb9795a [delegate=amqp://admin@192.168.0.202:8001/, localPort= 56260]
2018-05-04 11:21:49.059  INFO 60848 --- [nge.consumer1-8] o.s.a.r.l.SimpleMessageListenerContainer : Restarting Consumer@58b42519: tags=[{amq.ctag-T1HyrOd5Ykr_VQZDwxRslA=stream_exchange.consumer1}], channel=Cached Rabbit Channel: AMQChannel(amqp://admin@192.168.0.202:8001/,2), conn: Proxy@25a73de1 Shared Rabbit Connection: SimpleConnection@3fb9795a [delegate=amqp://admin@192.168.0.202:8001/, localPort= 56260], acknowledgeMode=AUTO local queue size=0

Spring Boot 配置 RabbitMQ(使用 HAProxy 负载均衡):

spring:
  application:
    name: stream-rabbitmq-producer
  rabbitmq:
    host: manager1
    port: 8001
    username: admin
    password: admin123456

最近使用 RabbitMQ 集群的时候(HAProxy 负载均衡),频繁的出现上面错误信息,但是消息可以正常被消费掉,如果只使用 RabbitMQ 的话(不使用 HAProxy),是没有任何错误的。

被这个问题困扰了很久,Google 找了很多资料,也没有找到解决方案,无意间找到一篇文章:RabbitMQ and HAProxy: a timeout issue

文章说,如果使用 HAProxy 配置 RabbitMQ 高可用集群的话,则会遇到客户端连接超时问题。

为什么会出现此问题呢?因为 HAProxy 配置了客户端连接超时参数(timeout client ms),如果客户端连接超过配置的此参数,那么 HAProxy 将会删除这个客户端连接。

RabbitMQ 客户端使用永久连接到代理,从不超时,那为什么还会出现问题?因为如果 RabbitMQ 在一段时间内处于非活动状态,那么 HAProxy 将自动关闭连接(有点坑呀