简体   繁体   English

作为 Kafka Producer 急切地连接到主题

[英]Eagerly connect to topic as a Kafka Producer

I'm implementing a service which sends messages to a downstream service via a Kafka topic.我正在实现一个通过 Kafka 主题向下游服务发送消息的服务。 This will only happen when my service's API is called, which is likely to be infrequently, at least at first.这只会在我的服务的 API 被调用时发生,这可能很少发生,至少一开始是这样。

I've found that the reactive Kafka producer API connects to Kafka lazily, which I'm sure is great for most use-cases, but I'd like to know that my Kafka connection configuration is correct at the point I deploy my service.我发现反应式 Kafka 生产者 API 懒惰地连接到 Kafka,我确信这对大多数用例来说都很棒,但我想知道我的 Kafka 连接配置在我部署我的服务时是正确的。 I don't want to have to wait for the first API call only to find out that something somewhere is wrong.我不想等到第一个 API 调用才发现某个地方出了问题。

The solution I've got for this at the moment is simply to send an initialisation message to the topic at start-up, but this feels clunky.我目前对此的解决方案只是在启动时向主题发送初始化消息,但这感觉很笨拙。 Is there anything better I can be doing to force an initial connection to the topic, or at least to validate the connection configuration?有什么更好的方法可以强制与主题建立初始连接,或者至少验证连接配置?

val response = kafkaSender.send(Mono.just(initialise))
        .next()
        .block();

if (response == null) {
    throw new RuntimeException("empty Mono from Kafka initialisation");
}

if (response.exception() != null) {
    throw propagate(response.exception());
}

如果您不想通过生产者发送心跳,您可以使用 AdminClient API 来描述您将要使用的集群或主题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM