SpringBoot Disable rabbitTemplate retry policy for

2019-04-16 08:42发布

问题:

My SpringBoot configuration contains very strong retry policy for rabbitTemplate retries

spring:
  rabbitmq:
    template:
      retry:
        enabled: true
        initial-interval: 500
        max-attempts: 10
        multiplier: 5
        max-interval: 60000

The problem with this configuration is when health endpoint is called and rabbitMQ is down, the connections hangs for really long time.

Adding properties like

spring.rabbitmq.connection-timeout=500 or
spring.rabbitmq.template.receive-timeout=500 or
spring.rabbitmq.template.reply-timeout=500 or 
spring.rabbitmq.requested-heartbeat=1

does not help, since the retry.multiplier=5, so it will take a lot of time anyway.

If we disregard whether the retry policy is good or not, is there a way to disable rabbitTemplate retries for health check endpoint or at least give it some timeout?

回答1:

You can override the default health indicator bean to use a template without retry enabled...

@Configuration
public class MyRabbitHealthIndicatorOverride
        extends CompositeHealthIndicatorConfiguration<RabbitHealthIndicator, RabbitTemplate> {

    @Bean
    public HealthIndicator rabbitHealthIndicator(ConnectionFactory connectionFactory) {
        return createHealthIndicator(new RabbitTemplate(connectionFactory));
    }

}