简体   繁体   English

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

[英]Automatic retry connection to broker by spring-rabbitmq

I have read this fragment of docs:我已经阅读了这个文档片段:

RabbitMQ Automatic Connection/Topology recovery RabbitMQ 自动连接/拓扑恢复

Since the first version of Spring AMQP, the framework has provided its own connection and channel recovery in the event of a broker failure.从 Spring AMQP 的第一个版本开始,该框架提供了自己的连接和通道在代理失败的情况下恢复。 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.此外,如第 3.1.10 节“配置代理”中所述,RabbitAdmin 将在重新建立连接时重新声明任何基础设施 bean(队列等)。 It therefore does not rely on the Auto Recovery that is now provided by the amqp-client library.因此,它不依赖于 amqp-client 库现在提供的自动恢复。 Spring AMQP now uses the 4.0.x version of amqp-client, which has auto recovery enabled by default. Spring AMQP 现在使用 4.0.x 版本的 amqp-client,它默认启用自动恢复。 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).如果您愿意,Spring AMQP 仍然可以使用自己的恢复机制,在客户端禁用它(通过将底层 RabbitMQ connectionFactory 上的 automaticRecoveryEnabled 属性设置为 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.这意味着您在代码中创建的任何消费者(可能通过 RabbitTemplate.execute())都可以自动恢复。

I am not sure If I correctly understand.我不确定我是否正确理解。 In my application.properties I have defined port and host.在我的application.properties我定义了端口和主机。 During starting my spring-boot app it successfully established connection and all necessary beans to communicate with queue.在启动我的 spring-boot 应用程序期间,它成功建立了连接和所有必要的 bean 以与队列通信。

However, what in case when during start my app broker is shutdown and it will be launched five minutes after starting of app ?但是,如果在启动期间我的应用程序代理关闭并且它将在应用程序启动五分钟后启动怎么办? Does spring-rabbitmq manage to reconnect and define all beans ? spring-rabbitmq是否设法重新连接并定义所有 bean?

That's correct.没错。 Spring AMQP manages the re-connection and recovery automatically. Spring AMQP 自动管理重新连接和恢复。

This subject isn't related to bean definitions.本主题与 bean 定义无关。 If you talk about Broker entities declaration, then yes, that are processed really on the connection establishing.如果您谈论 Broker 实体声明,那么是的,它们确实在建立连接时进行了处理。

I've had a similar issue, you just have to put a property on the connection factory configuration.我遇到了类似的问题,您只需要在连接工厂配置上放置一个属性即可。

As per the article here set factory.setAutomaticRecoveryEnabled(true);根据这里的文章 set factory.setAutomaticRecoveryEnabled(true); and factory.setNetworkRecoveryInterval(10000);factory.setNetworkRecoveryInterval(10000); on the factory and the rabbit client will try to reconnect when the rabbit server is down or the connection is lost.在工厂中,当兔子服务器关闭或连接丢失时,兔子客户端将尝试重新连接。

Because you are using spring configuration for the connection factory your connection factory will be like the following因为您正在为连接工厂使用弹簧配置,所以您的连接工厂将如下所示

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

Connection factory reference here连接工厂参考这里

Yes, the connections will be recreated when the broker is back on line.是的,当代理重新上线时将重新创建连接。 The default recovery interval is 5 seconds.默认恢复间隔为 5 秒。 You can change the recovery interval by setting container.setRecoveryInterval(30000);您可以通过设置container.setRecoveryInterval(30000);来更改恢复间隔container.setRecoveryInterval(30000); where container is a SimpleMessageListenerContainer .其中containerSimpleMessageListenerContainer Setting recovery interval in the underlying connection factory cachingConnectionFactory.getRabbitConnectionFactory().setNetworkRecoveryInterval(int) seems not reflecting.在底层连接工厂cachingConnectionFactory.getRabbitConnectionFactory().setNetworkRecoveryInterval(int)设置恢复间隔似乎没有反映。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM