通过 spring-rabbitmq 自动重试与代理的连接

Automatic retry connection to broker by spring-rabbitmq

我已阅读此文档片段:

RabbitMQ Automatic Connection/Topology recovery

Since the first version of Spring AMQP, the framework has provided its own connection and channel recovery in the event of a broker failure. Also, as discussed in Section 3.1.10, “Configuring the broker”, the RabbitAdmin will re-declare any infrastructure beans (queues etc) when the connection is re-established. It therefore does not rely on the Auto Recovery that is now provided by the amqp-client library. Spring AMQP now uses the 4.0.x version of amqp-client, which has auto recovery enabled by default. Spring AMQP can still use its own recovery mechanisms if you wish, disabling it in the client, (by setting the automaticRecoveryEnabled property on the underlying RabbitMQ connectionFactory to false). However, the framework is completely compatible with auto recovery being enabled. This means any consumers you create within your code (perhaps via RabbitTemplate.execute()) can be recovered automatically.

我不确定我是否理解正确。在我的 application.properties 中,我定义了端口和主机。在启动我的 spring-boot 应用程序期间,它成功建立了连接和所有必要的 bean 以与队列通信。

但是,如果在启动期间我的应用程序代理关闭并且它将在应用程序启动五分钟后启动,该怎么办? spring-rabbitmq 是否设法重新连接并定义所有 bean?

没错。 Spring AMQP 自动管理重新连接和恢复。

本主题与 bean 定义无关。如果您谈论 Broker 实体声明,那么是的,它们实际上是在建立连接时处理的。

我遇到过类似的问题,您只需在连接工厂配置上添加 属性 即可。

根据文章 here 在工厂设置 factory.setAutomaticRecoveryEnabled(true);factory.setNetworkRecoveryInterval(10000);,当兔子服务器关闭或连接丢失时,兔子客户端将尝试重新连接。

因为您正在为连接工厂使用 spring 配置,所以您的连接工厂将如下所示

<bean id="connectionFactory"
      class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
    <constructor-arg value="somehost"/>
    <property name="username" value="guest"/>
    <property name="password" value="guest"/>
    <property name="automaticRecoveryEnabled" value="true"/>
    <property name="networkRecoveryInterval" value="100000"/>
</bean>

连接工厂参考 here

是的,当代理重新联机时将重新创建连接。默认恢复间隔为 5 秒。您可以通过设置 container.setRecoveryInterval(30000); 来更改恢复间隔,其中 containerSimpleMessageListenerContainer。在底层连接工厂中设置恢复间隔 cachingConnectionFactory.getRabbitConnectionFactory().setNetworkRecoveryInterval(int) 似乎没有反映。