【问题标题】:How do I bypass kafka broker failure when starting springboot server?启动springboot服务器时如何绕过kafka代理失败?
【发布时间】:2019-10-05 21:16:37
【问题描述】:

我在使用 spring-boot-Kafka('spring-Kafka' -'2.2.7.RELEASE') 集成库访问我的 Kafka 的 spring-boot(2.1.7.RELEASE) 应用服务器中遇到问题话题。

当我的 Kafka 代理关闭时,我的应用程序无法启动。

这是我得到的:

2019-10-06 02:41:02.764  WARN -- [main           ] org.apache.kafka.clients.NetworkClient                       [] : [Consumer clientId=consumer-1, groupId=caas] Connection to node -1 could not be established. Broker may not be available.

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-10-06 02:41:31.777 ERROR -- [main           ] org.springframework.boot.SpringApplication                   [] : Application run failed

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
    at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:185)
    at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:53)
    at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:360)
    at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:158)
    at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:122)
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:893)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:162)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:552)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:743)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:390)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1214)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1203)
    at com.zinka.fmsclients.FmsClientsApplication.main(FmsClientsApplication.java:14)
Caused by: org.apache.kafka.common.errors.TimeoutException: Timeout expired while fetching topic metadata

有没有办法绕过对 Kafka 代理的这种依赖?即使代理不可用/关闭,我也希望我的应用程序启动。代理启动后,应用服务器应该能够连接。

【问题讨论】:

    标签: java spring-boot web-applications apache-kafka


    【解决方案1】:

    有一个容器属性missingTopicsFatalhttps://docs.spring.io/spring-kafka/api/org/springframework/kafka/listener/ContainerProperties.html#isMissingTopicsFatal-- 即使主题和/或代理不可用,也允许应用程序启动。

    @Bean(name = "kafkaListenerContainerFactory")
    public ConcurrentKafkaListenerContainerFactory<?, ?> kafkaListenerContainerFactory(
        ConsumerFactory<Object, Object> kafkaConsumerFactor,
        ConcurrentKafkaListenerContainerFactoryConfigurer configurer) {
    
      ConcurrentKafkaListenerContainerFactory<Object, Object> factory =
          new ConcurrentKafkaListenerContainerFactory<>();
      configurer.configure(factory, kafkaConsumerFactor);
    
      ContainerProperties containerProperties = factory.getContainerProperties();
      containerProperties.setMissingTopicsFatal(false);
    
      ...
      return factory;
    }
    

    【讨论】:

    • 主题不可用和经纪人不可用是两个不同的东西!!有什么办法可以绕过在启动 spring boot 应用程序时检查 kafka 服务器状态?
    • 当代理不可用时,应用程序也会启动,至少在我的 kubernetes 集群上工作。你测试设置了吗?
    • 是的,我使用嵌入式和外部 tomcat 服务器对其进行了测试。它没有用。到目前为止,我们还没有使用 Kubernetes!总之谢谢
    • 嗨,我重新测试并验证这仍然适用于 Spring Boot 2.3。应用程序启动并开始搜索 kafka 代理,如果代理可用,则开始使用消息,如果代理消失,则应用程序继续搜索代理。这是一个工作示例应用程序:github.com/AndreasKl/stackoverflow-58252409
    • 谢谢!!我现在用 spring boot 2.1.6 进行了测试,它可以工作。我最初在开始使用这些属性时进行了测试,它可能由于其他原因而失败。
    【解决方案2】:

    这是新版spring的变化。

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

    【讨论】:

      猜你喜欢
      • 2020-05-23
      • 1970-01-01
      • 2019-03-21
      • 1970-01-01
      • 2017-02-10
      • 1970-01-01
      • 2018-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多