Kafka设置是否启动消费者监听器

目前在做一个项目,需要从第三方通过消息队列拿数据,之前使用的是kinesis,现在需要改成kafka,于是在前期的时候,希望增加一个开关控制是否开启kafka的消息监听。

但是在使用kafka的时候,springboot集成 kafka就是直接使用@KafkaListener注解,启用监听就可以了,但是设置监听不监听,还真不知道从哪下手。

但是之前也对kafka的消费者做过个性化配置,于是顺着这条线看看可能找到解决方案,结果皇天不负有心人,ConcurrentKafkaListenerContainerFactory这个类就可以直接配置,下面是我kafka详细的配置:

@Configuration
@Slf4j
@EnableConfigurationProperties({KafkaProducerProperties.class, KafkaConsumerProperties.class, KafkaLocalConsumerProperties.class})
public class KafkaConfig {
    private final KafkaProducerProperties producerProperties;
    private final KafkaConsumerProperties defaultConsumerProperties;
    private final KafkaLocalConsumerProperties localConsumerProperties;

    public KafkaConfig(KafkaProducerProperties kafkaProperties, KafkaConsumerProperties kafkaConsumerProperties, KafkaLocalConsumerProperties kafkaLocalConsumerProperties) {
        this.producerProperties = kafkaProperties;
        this.defaultConsumerProperties = kafkaConsumerProperties;
        this.localConsumerProperties = kafkaLocalConsumerProperties;
    }


    //==================================================Consumer Config==============================================================================================

    @Bean("defaultConsumerFactory")
    @Primary
    public ConsumerFactory<Object, Object> consumerFactory2() {
        return createConsumerFactory(defaultConsumerProperties);
    }

    @Bean("localConsumerFactory")
    public ConsumerFactory<Object, Object> consumerFactory() {
        return createConsumerFactory(localConsumerProperties);
    }

    private DefaultKafkaConsumerFactory<Object, Object> createConsumerFactory(KafkaBaseConsumerProperties kafkaConsumerProperties) {
        Map<String, Object> configs = new HashMap<>();
        configs.put(ConsumerConfig.GROUP_ID_CONFIG, kafkaConsumerProperties.getGroupId());
        configs.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaConsumerProperties.getBootstrapServers());
        configs.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, kafkaConsumerProperties.getAutoCommitInterval());
        configs.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, kafkaConsumerProperties.isEnableAutoCommit());
        configs.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        configs.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        configs.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 50);
        return new DefaultKafkaConsumerFactory<>(configs);
    }


   
    @Bean("kafkaListenerContainerFactory")
    @Primary
    public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory(
            ConsumerFactory<Object, Object> consumerFactory) {
        ConcurrentKafkaListenerContainerFactory<String, String> listenerContainerFactory = createListenerContainerFactory(consumerFactory);
        listenerContainerFactory.setAutoStartup(defaultConsumerProperties.isEnableAutoStartup());
        return listenerContainerFactory;
    }

    @Bean("localConsumer")
    public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory2(
            @Qualifier("localConsumerFactory") ConsumerFactory<Object, Object> consumerFactory) {
        ConcurrentKafkaListenerContainerFactory<String, String> listenerContainerFactory = createListenerContainerFactory(consumerFactory);
        listenerContainerFactory.setAutoStartup(localConsumerProperties.isEnableAutoStartup());
        return listenerContainerFactory;
    }

    private ConcurrentKafkaListenerContainerFactory<String, String> createListenerContainerFactory(ConsumerFactory<Object, Object> consumerFactory) {
        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConcurrency(3);
        factory.setConsumerFactory(consumerFactory);
        return factory;
    }
}

重点就是最下面这段里,listenerContainerFactory.setAutoStartup()这个方法了,传入一个boolean类型的值就OK啦,是不是超级简单?

 

©️2020 CSDN 皮肤主题: 点我我会动 设计师:上身试试 返回首页