Spring Boot RabbitMQ连接重试配置

时间:2019-06-22 16:51:02

标签: spring spring-boot rabbitmq spring-amqp spring-rabbitmq

我有一些带有rabbitmq的spring boot cloud微服务,我正在使用docker-compose启动所有容器。 Rabbitmq需要一些时间才能开始,在完成启动之前,我看到了很多连接尝试:

config-service_1          | 2019-06-22 16:13:13.351  INFO 6 --- [pool-2-thread-2] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: rabbitmq:5672
config-service_1          | 2019-06-22 16:13:13.351  INFO 6 --- [pool-2-thread-2] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: rabbitmq:5672
config-service_1          | 2019-06-22 16:13:13.370  INFO 6 --- [pool-2-thread-2] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: rabbitmq:5672
config-service_1          | 2019-06-22 16:13:13.370  INFO 6 --- [pool-2-thread-2] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: rabbitmq:5672
config-service_1          | 2019-06-22 16:13:13.378  INFO 6 --- [pool-2-thread-2] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: rabbitmq:5672
config-service_1          | 2019-06-22 16:13:13.379  INFO 6 --- [pool-2-thread-2] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: rabbitmq:5672
config-service_1          | 2019-06-22 16:13:13.379  INFO 6 --- [pool-2-thread-2] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: rabbitmq:5672
config-service_1          | 2019-06-22 16:13:13.386  INFO 6 --- [pool-2-thread-2] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: rabbitmq:5672
config-service_1          | 2019-06-22 16:13:13.386  INFO 6 --- [pool-2-thread-2] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: rabbitmq:5672
config-service_1          | 2019-06-22 16:13:13.391  INFO 6 --- [pool-2-thread-2] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: rabbitmq:5672
config-service_1          | 2019-06-22 16:13:13.401  INFO 6 --- [pool-2-thread-2] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: rabbitmq:5672

如何为Rabbitmq连接配置重试超时?
我没有找到任何spring.rabbitmq.**属性来执行此操作。

更新

已发现问题,但仍无法解决。
我有一个日志记录配置,可通过Rabbitmq附加程序发送日志,这里是logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <springProperty scope="context" name="appName" source="spring.application.name"/>
    <springProperty scope="context" name="rabbitMqHost" source="spring.rabbitmq.host" defaultValue="localhost"/>
    <springProperty scope="context" name="rabbitMqPort" source="spring.rabbitmq.port" defaultValue="5672"/>
    <springProperty scope="context" name="rabbitMqUsername" source="spring.rabbitmq.username" defaultValue="guest"/>
    <springProperty scope="context" name="rabbitMqPassword" source="spring.rabbitmq.password" defaultValue="guest"/>
    <appender name="AMQP" class="org.springframework.amqp.rabbit.logback.AmqpAppender">
        <host>${rabbitMqHost}</host>
        <port>${rabbitMqPort}</port>
        <username>${rabbitMqUsername}</username>
        <password>${rabbitMqPassword}</password>
        <applicationId>${appName}</applicationId>
        <contentType>application/log</contentType>
        <exchangeName>gc.log</exchangeName>
        <declareExchange>true</declareExchange>
        <deliveryMode>NON_PERSISTENT</deliveryMode>
        <charset>UTF-8</charset>
        <layout>
            <pattern>
                ${appName}
                %date{"yyyy-MM-dd'T'HH:mm:ss.SSSXXX", UTC}
                %thread
                %level
                %logger{36}
                %message
            </pattern>
        </layout>
    </appender>
    <root level="INFO">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="AMQP"/>
    </root>
</configuration>

如果我删除此配置,则一切正常。
因此,现在的问题是如何为AmqpAppender

配置连接工厂恢复间隔

1 个答案:

答案 0 :(得分:1)

当前未作为Boot属性公开,但是您可以在Rabbit侦听器容器工厂上设置recoveryIntervalrecoveryBackOff

仅在某些配置类中获得对容器工厂的引用(或覆盖Boot的默认自动配置工厂)并设置属性。

例如

@Bean
public Object configure(SimpleRabbitListenerContainerFactory factory) {
    factory.setRecoveryInterval(10_000L);
    return null; // you can return null as long as you are on Boot 2.x.
}

请打开GitHub改进Boot问题,以将其公开为属性。

编辑

您的原始问题没有提到您使用RabbitMQ仅用于日志记录。 以后,请进一步澄清您的问题,以便及时获得正确的答案。

以上答案适用于尝试连接到代理的@RabbitListener方法。

由于日志记录是一个“发布”事件,因此每次您尝试发送日志(以及重试)时都会进行一次连接尝试,因此您将获得该INFO日志。

抑制这些日志的唯一方法是将org.springframework.amqp.rabbit.connection.CachingConnectionFactory的日志级别更改为WARN。