为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

潇潇雨雨 2023-09-27 10:16:35
我在 spring-boot(2.1.7.RELEASE) 应用程序服务器中遇到问题,该服务器使用 spring-boot-Kafka('spring-Kafka' -'2.2.7.RELEASE') 集成库来访问我的 Kafka 主题。当我的 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 failedorg.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)有没有办法绕过对 Kafka 代理的依赖?即使经纪人不可用/关闭,我也希望我的应用程序能够启动。一旦代理启动,应用程序服务器应该能够连接。
查看完整描述

2 回答

?
弑天下

TA贡献1818条经验 获得超8个赞

有一个容器属性missingTopicsFatal: https://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;

}


查看完整回答
反对 回复 2023-09-27
?
慕勒3428872

TA贡献1848条经验 获得超6个赞

这是新版本的spring的变化。


@Component

class ContainerFactoryConfigurer {


    ContainerFactoryConfigurer(ConcurrentKafkaListenerContainerFactory<?, ?> factory) {

        factory.getContainerProperties().setMissingTopicsFatal(false);

    }


}


查看完整回答
反对 回复 2023-09-27
  • 2 回答
  • 0 关注
  • 108 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信