简体   繁体   中英

Spring boot rabbitmq connection retry configuration

I have spring boot cloud microservices with rabbitmq, I'm using docker-compose to start all containers. Rabbitmq need some time to start and before it finishes starting I see a lot of connecting attempts:

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

How I can configure retry timeout for rabbitmq connection?
I have not found any spring.rabbitmq.** property to do this.

Update

Have found a problem but still can't fix it.
I have a logging configuration that sends logs via rabbitmq appender, here is 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>

if I delete this config everything works well.
So, now the question is how can I configure connection factory recovery interval for AmqpAppender

It's not currently exposed as a Boot property, but you can set the recoveryInterval or recoveryBackOff on the rabbit listener container factory.

Simply get a reference to the container factory in some configuration class (or override Boot's default auto-configured factory) and set the property.

eg

@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.
}

Please open a Boot improvement GitHub issue to expose this as a property.

EDIT

Your original question made no mention that you are using RabbitMQ for logging only. In future, please be more clear in your questions, in order to get a more timely correct answer.

The above answer applies to @RabbitListener methods attempting to connect to the broker.

Since logging is a "publishing" event a connection attempt will be made each time you attempt to send a log (and for the retries) and hence you'll get that INFO log.

The only way to suppress those logs is to change the log level for org.springframework.amqp.rabbit.connection.CachingConnectionFactory to WARN.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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