使用 spring 引导时如何禁用 KafkaAdmin

How can I disable KafkaAdmin when using spring boot

目前我正在使用 Spring boot 2.4.0 和 spring-kafka。

我想使用消费者和生产者,但不想使用 kafka admin。

我试图通过将“bootstrap.servers”设置为空来覆盖 KafkaAdmin class,但是没有用。它仍然 'try' 连接以创建 KafkaAdmin 并记录错误。这不是致命的,但是有什么办法可以完全禁用 kafka admin?

你的担心不无道理。 KafkaAdmin 不会自行连接。那里的逻辑是这样的:

public final boolean initialize() {
        Collection<NewTopic> newTopics = this.applicationContext.getBeansOfType(NewTopic.class, false, false).values();
        if (newTopics.size() > 0) {
            AdminClient adminClient = null;
            try {
                Map<String, Object> configs2 = new HashMap<>(this.configs);
                checkBootstrap(configs2);
                adminClient = AdminClient.create(configs2);
            }
            catch (Exception e) {
                if (!this.initializingContext || this.fatalIfBrokerNotAvailable) {
                    throw new IllegalStateException("Could not create admin", e);
                }
                else {
                    LOGGER.error(e, "Could not create admin");
                }
            }

因此,如果应用程序上下文中没有任何 NewTopic bean,则 KafkaAdmin 什么都不做。如果你有一些,那是什么原因,如果不是在 producing/consuming 之前在代理上创建新主题。因此KafkaAdmin逻辑。