Spring Kafka Listner |阅读相同的消息

时间:2017-09-19 19:14:55

标签: spring spring-boot apache-kafka spring-kafka

我有一个主题名称" user"在哪里删除userid,我想从这个主题中读取并处理下面的功能

1. process user leave data
2. process user salary data

我希望有两个侦听器指向同一主题并读取相同的用户ID并并行启动处理。

@KafkaListener(topics = "${kafka.topic.user}",group="abc"))
            public void receive(String message) {

                    userService.processLeave(message);
                }

@KafkaListener(topics = "${kafka.topic.user}",group="abc1"))
            public void receive1(String message) {

                    userService.processPayRoll(message);
                }

但是我一直看到 - processPayRoll 一直被调用。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

好吧,看起来你使用旧的Spring Kafka版本。

不幸的是group与消费者的group.id无关。 这是生命周期管理的containerGroup

您应该考虑根据不同的消费者配置配置不同的KafkaMessageListenerContainer。在那里,您已经配置了不同的ConsumerConfig.GROUP_ID_CONFIG

最新版本具有@KafkaListener结构,如:

/**
 * If provided, the listener container for this listener will be added to a bean
 * with this value as its name, of type {@code Collection<MessageListenerContainer>}.
 * This allows, for example, iteration over the collection to start/stop a subset
 * of containers.
 * @return the bean name for the group.
 */
String containerGroup() default "";

/**
 * Override the {@code group.id} property for the consumer factory with this value
 * for this listener only.
 * @return the group id.
 * @since 1.3
 */
String groupId() default "";

/**
 * When {@link #groupId() groupId} is not provided, use the {@link #id() id} (if
 * provided) as the {@code group.id} property for the consumer. Set to false, to use
 * the {@code group.id} from the consumer factory.
 * @return false to disable.
 * @since 1.3
 */
boolean idIsGroup() default true;