问题描述
我见过一些例子,我们有一个 java 配置类,我们定义了多个 KafkaListenerContainer
并将所需的 containerType
传递给 @kafkaListener
.但我正在探索是否有任何方法可以通过 appication.yml/properties 使用 Spring Boot 自动 Kafka 配置来实现相同的目标.
I have seen examples where we have a java configuration class and we define multiple KafkaListenerContainer
and pass the required containerType
to @kafkaListener
. But i am exploring if there are any ways to achieve the same using Spring Boot auto Kafka configuration via appication.yml/properties.
推荐答案
否;Boot 只会自动配置一组基础设施;如果需要多个,则需要将它们定义为bean.
No; Boot will only auto-configure one set of infrastructure; if you need multiple, you need to define them as beans.
但是,对于最近的版本(自 2.3.4 起),您可以向工厂添加侦听器容器定制器,以便您可以自定义每个侦听器容器,即使它们是由同一个工厂创建的;也可以在 @KafkaListener
注释本身上覆盖某些属性.
However, with recent versions (since 2.3.4), you can add a listener container customizer to the factory so you can customize each listener container, even though they are created by the same factory; some properties can also be overridden on the @KafkaListener
annotation itself.
示例:
@Component
class Customizer {
public Customizer(ConcurrentKafkaListenerContainerFactory<?, ?> factory) {
factory.setContainerCustomizer(container -> {
if (container.getContainerProperties().getGroupId().equals("slowGroup")) {
container.getContainerProperties().setIdleBetweenPolls(60_000);
}
});
}
}
这篇关于使用 appication.yml/properties 配置具有不同属性的 Spring Boot Kafka 多个消费者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!