SpringBoot Kafka Consumer: Failed to start bean internalKafkaListenerEndpointRegistry TimeoutException

Viewed 6754

I have a spring boot app that is using kafka, but I'm not able to start it because I just implemented a kafka consumer that is listening a server that is offline. WHen I start it I get some:

org.springframework.context.ApplicationContextException: Failed to start bean 'org.springframework.kafka.config.internalKafkaListenerEndpointRegistry'; nested exception is org.apache.kafka.common.errors.TimeoutException: Timeout expired while fetching topic metadata

because kafka is down.

How can I configure the spring boot application in a way that it starts even if kafka server is down?

bellow my Kafka Consumer config:

@EnableKafka
@Configuration
public class KafkaConsumerConfig {

    @Value(value = "${app.kafka.bootstrap-servers}")
    private String bootstrapServers;

    @Value("${HOSTNAME:NO_HOSTNAME}")
    private String groupId;

    @Value(value = "${spring.profiles.active}")
    private String activeSpringProfile;

    @Bean
    public ConsumerFactory<String, String> consumerFactory() {
        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ConsumerConfig.GROUP_ID_CONFIG,String.format("RANDOM_GROUP_ID_%s_%s", groupId, RandomUtils.nextInt()));
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
        return new DefaultKafkaConsumerFactory<>(props);
    }

    @Bean
    public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        return factory;
    }
}

Thank you!

1 Answers

With recent versions, the missingTopicsFatal container property is true, which is what causes this problem. You can turn it off...

@Component
class ContainerFactoryConfigurer {

    ContainerFactoryConfigurer(ConcurrentKafkaListenerContainerFactory<?, ?> factory) {
        factory.getContainerProperties().setMissingTopicsFatal(false);
    }

}
Related