如何禁用自动启动 KAFKA 消费者而无需更改任何代码,包括在 Spring 引导中设置 autoStartup = "{xyz}"?

How to disable KAFKA consumer being autostarted without having to make any code changes including setting the autoStartup = "{xyz}" in Spring Boot?

下面是我的 KAFKA 消费者

@Service
public class Consumer {

    private static final Logger LOGGER = Logger.getLogger(Consumer.class.getName());
    public static Queue<ProductKafka> consumeQueue = new LinkedList<>();

    @KafkaListener(topics = "#{'${spring.kafka.topics}'.split('\\ ')}", groupId = "#{'${spring.kafka.groupId}'}")
    public void consume(ProductKafka productKafka) throws IOException {
        consumeQueue.add(productKafka);
        LOGGER.info(String.format("#### -> Logger Consumed message -> %s", productKafka.toString()));
        System.out.printf("#### -> Consumed message -> %s", productKafka.toString());
    }
}

下面是我的“application.properties”文件

spring.kafka.topics=Product
spring.kafka.groupId=Product-Group

我的 KAFKA 消费者正在自动启动。

但是我想禁用自动启动的 KAFKA 消费者,而无需对现有代码进行任何更改,包括设置 autoStartup = "{xyz}" 在消费者中 class 由于要求。

我正在寻找一个现有的属性,它会禁用 KAFKA 消费者自动启动,就像这样

spring.kafka.consumer.enable=false

注意:我有多个KAFKA消费者,上面的属性应该禁用项目中的所有消费者。

我们是否有任何现有属性可以禁用 KAFKA 消费者自动启动而无需对现有代码进行任何更改?

没有开箱即用的标准属性;你必须提供你自己的。

autoStartup="${should.start:true}"

如果 属性 should.start 不存在,将启动容器。

编辑

只需在您的应用程序中添加类似的内容即可。

@Component
class Customizer {

    Customizer(AbstractKafkaListenerContainerFactory<?, ?, ?> factory,
            @Value("${start.containers:true}") boolean start) {

        factory.setAutoStartup(start);
    }

}
start:
  containers: false