简体   繁体   English

Spring Boot RabbitMQ连接重试配置

[英]Spring boot rabbitmq connection retry configuration

I have spring boot cloud microservices with rabbitmq, I'm using docker-compose to start all containers. 我有与rabbitmq的spring boot cloud微服务,我使用docker-compose启动所有容器。 Rabbitmq need some time to start and before it finishes starting I see a lot of connecting attempts: 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

How I can configure retry timeout for rabbitmq connection? 如何为Rabbitmq连接配置重试超时?
I have not found any spring.rabbitmq.** property to do this. 我没有找到任何spring.rabbitmq.**属性来执行此操作。

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 : 我有一个日志记录配置,可通过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>

if I delete this config everything works well. 如果我删除此配置,一切正常。
So, now the question is how can I configure connection factory recovery interval for AmqpAppender 因此,现在的问题是如何为AmqpAppender配置连接工厂恢复间隔

It's not currently exposed as a Boot property, but you can set the recoveryInterval or recoveryBackOff on the rabbit listener container factory. 它当前没有显示为Boot属性,但是您可以在Rabbit侦听器容器工厂上设置recoveryIntervalrecoveryBackOff

Simply get a reference to the container factory in some configuration class (or override Boot's default auto-configured factory) and set the property. 只需在某些配置类中获得对容器工厂的引用(或覆盖Boot的默认自动配置工厂)并设置属性即可。

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. 请打开“启动改进GitHub”问题以将其公开为属性。

EDIT 编辑

Your original question made no mention that you are using RabbitMQ for logging only. 您最初的问题没有提到您使用RabbitMQ仅用于日志记录。 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. 上面的答案适用于尝试连接到代理的@RabbitListener方法。

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. 由于日志记录是“发布”事件,因此,每次您尝试发送日志(并进行重试)时,都会进行一次连接尝试,因此您将获得该INFO日志。

The only way to suppress those logs is to change the log level for org.springframework.amqp.rabbit.connection.CachingConnectionFactory to WARN. 抑制这些日志的唯一方法是将org.springframework.amqp.rabbit.connection.CachingConnectionFactory的日志级别更改为WARN。

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

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