【问题标题】:How to set groupId to null in @KafkaListeners如何在@KafkaListeners 中将 groupId 设置为 null
【发布时间】:2019-05-13 14:18:51
【问题描述】:

关于:this question

我正在尝试通过@KafkaListener 阅读压缩主题。 我希望每个消费者每次都阅读整个主题。

我无法为每个消费者生成一个唯一的 groupId。所以我想使用一个空 groupid。

我已尝试配置容器和消费者以将 groupId 设置为 null,但均未成功。

这是我的容器配置:

 ConcurrentKafkaListenerContainerFactory<Object, Object> factory = new ConcurrentKafkaListenerContainerFactory<>();
        configurer.configure(factory, kafkaConsumerFactory);
        // Set ackMode to manual and never commit, we are reading from the beginning each time
        factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL);
        factory.getContainerProperties().setAckOnError(false);
        // Remove groupId, we are consuming all partitions here
        factory.getContainerProperties().setGroupId(null);
        // Enable idle event in order to detect when init phase is over
        factory.getContainerProperties().setIdleEventInterval(1000L);

还尝试强制消费者配置:

Map<String, Object> consumerProperties = sprinfKafkaProperties.buildConsumerProperties();
        // Override group id property to force "null"
        consumerProperties.put(ConsumerConfig.GROUP_ID_CONFIG, null);
        ConsumerFactory<Object, Object> kafkaConsumerFactory = new DefaultKafkaConsumerFactory<>(consumerProperties);

当我将容器 groupId 设置为 null 时,使用带有侦听器 ID 的默认值。

当我强制消费者使用 null groupId 属性时,出现错误: 在消费者配置、容器属性或@KafkaListener 注释中找不到 group.id;使用组管理时需要 group.id。

【问题讨论】:

    标签: apache-kafka spring-kafka


    【解决方案1】:

    您不能使用 null group.id

    来自kafka documentation

    group.id

    标识此消费者所属的消费者组的唯一字符串。如果消费者通过 subscribe(topic) 或基于 Kafka 的偏移管理策略使用组管理功能,则此属性是必需的

    如果您想每次都从头开始阅读,您可以在容器工厂中添加ConsumerAwareRebalanceListener,或者让您的监听器实现ConsumerSeekAware

    在任何一种情况下,当调用onPartitionsAssigned 时,将每个主题/分区查找到开头。

    我无法为每个消费者生成一个唯一的 groupId。

    您可以使用 SpEL 表达式来生成 UUID。

    编辑

    您可以手动分配主题/分区,然后 group.id 可以为空。

    @SpringBootApplication
    public class So56114299Application {
    
        public static void main(String[] args) {
            SpringApplication.run(So56114299Application.class, args);
        }
    
        @Bean
        public NewTopic topic() {
            return new NewTopic("so56114299", 10, (short) 0);
        }
    
        @KafkaListener(topicPartitions = @TopicPartition(topic = "so56114299",
                              partitions = "#{@finder.partitions('so56114299')}"))
        public void listen(@Header(KafkaHeaders.RECEIVED_MESSAGE_KEY) String key, String payload) {
            System.out.println(key + ":" + payload);
        }
    
        @Bean
        public PartitionFinder finder(ConsumerFactory<String, String> consumerFactory) {
            return new PartitionFinder(consumerFactory);
        }
    
        public static class PartitionFinder {
    
            public PartitionFinder(ConsumerFactory<String, String> consumerFactory) {
                this.consumerFactory = consumerFactory;
            }
    
            private final ConsumerFactory<String, String> consumerFactory;
    
            public String[] partitions(String topic) {
                try (Consumer<String, String> consumer = consumerFactory.createConsumer()) {
                    return consumer.partitionsFor(topic).stream()
                        .map(pi -> "" + pi.partition())
                        .toArray(String[]::new);
                }
            }
    
        }
    
    }
    
    
    spring.kafka.consumer.enable-auto-commit=false
    spring.kafka.consumer.auto-offset-reset=earliest
    spring.kafka.listener.ack-mode=manual
    

    【讨论】:

    • 您还可以在groupId 属性中使用SpEL 表达式来生成UUID。
    • 由于代理上的安全性和 ACL,我无法生成唯一的 groupId。我只能使用有限数量的 groupId,我必须明确列出所有名称。
    • 是否可以不使用“群组管理功能”?
    • 您可以手动将主题/分区分配给侦听器(使用 topicPartitions 注释属性)。如果您有很多分区,这很笨拙,但如果您只有几个分区,则可以管理。
    • 好的,我想我可以使用 groupId null。如果我使用“consumer.partitionsFor()”和“consumer.assign()”创建自己的 kafkaConsumer 分配给所有 TopicPartitions 怎么办?
    猜你喜欢
    • 2014-10-01
    • 2013-10-31
    • 2013-01-28
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2012-03-08
    • 1970-01-01
    相关资源
    最近更新 更多