在 SpringBoot 中重试连接到 RabbitMQ 和 MongoDB

Retry Connection to RabbitMQ and MongoDB in SpringBoot

我正在使用 spring 并想重试获取与 RabbitMQ 的连接,如果失败 MongoDB。
我知道它对 sql 数据库是这样工作的:https://github.com/spring-projects/spring-boot/issues/4779#issuecomment-305482782

但是我不知道为 RabbitMQ 包装哪个 类 和 MongoDB。

我还尝试为 RabbitMQ 包装 ConnectionFactory,但 AbstractConnectionFactory 有一些最终方法我无法覆盖以将调用传递给委托工厂。

对于RabbitMQ,在消费者端,连接会根据监听器容器配置自动恢复(默认每5秒尝试重连)。

对于发布消息,您可以在 RabbitTemplate.

中配置适当配置的 RetryTemplate(来自 spring-重试)

对于 Mongo,您可以在代码中使用 RetryTemplate@Retryable 注释。