本文介绍了应用启动时Kafka无法配置Topic,但稍后可以通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个使用 spring-kafka (2.2.5.RELEASE) 的 spring boot 应用程序,它在启动时总是出现这个错误:

We have a spring boot application using spring-kafka (2.2.5.RELEASE) that always gets this error when starting up:

Could not configure topics
org.springframework.kafka.KafkaException: Timed out waiting to get existing
topics; nested exception is java.util.concurrent.TimeoutException

但是,应用程序继续启动:

However, the application continues to startup:

org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1]
INFO  o.s.k.l.KafkaMessageListenerContainer - partitions revoked: []
INFO  o.s.k.l.KafkaMessageListenerContainer - partitions assigned: [my-reply-topic-1]
INFO  o.s.k.l.KafkaMessageListenerContainer - partitions assigned: [my-request-topic-0]
INFO  o.s.b.w.e.tomcat.TomcatWebServer -
Tomcat started on port(s): 8080 (http) with context path ''

此时,应用程序按预期与 Kafka 交互.

At this point, the application interacts with Kafka as expected.

我们喜欢保持我们的日志干净,所以我们想了解为什么会抛出这个异常.此外,这有点令人困惑,因为当我们移动到应用程序和 kafka 代理之间尚未建立网络的不同环境时,我们会收到相同的错误,但应用程序无法运行.在确实存在问题时出现相同的异常,并且在尝试解决连接问题时可以忽略它是令人厌烦的.

We like to keep our logs clean, so we would like to understand why this Exception is thrown. Also, it is a bit confusing, because when we move to a different environment where the networking has not been established between the application and the kafka broker(s), we get the same error, but the application does not function. Having the same Exception occur when there is truly a problem and when it can be ignored is irksome when trying to troubleshoot connectivity issues.

有没有办法在应用程序启动时确定是否与 Kafka 建立了连接,而不仅仅是等待超时消息(这可能是一个红鲱鱼)?

Is there a way, on application startup, to determine whether connectivity has been established with Kafka rather than just waiting for a timeout message (which may be a red herring anyway)?

推荐答案

如果主题已经存在,从应用程序上下文和 KafkaAdmin 删除任何 NewTopic bean> 根本不会尝试连接到代理.

If the topic(s) exist already, remove any NewTopic beans from the application context and the KafkaAdmin won't try to connect to the broker at all.

这篇关于应用启动时Kafka无法配置Topic,但稍后可以通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 07:32