启动springboot服务器时如何绕过kafka代理失败?

时间:2019-10-05 21:16:37

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

我在使用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经纪人的依赖?我希望即使代理不可用/关闭,我的应用程序也可以启动。代理启动后,App Server应该能够连接。

2 个答案:

答案 0 :(得分:2)

有一个容器属性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;
}

答案 1 :(得分:0)

这是spring的较新版本中的更改。

@Component
class ContainerFactoryConfigurer {

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

}