0

我想编写一个使用 KafkaListener 订阅主题的消费者方法。

我找到了一个建议这个答案 -

@KafkaListener(id = "foo", topics = "dead-out")
public void dlq(Message<?> in) {
    System.out.println("DLQ:" + in);
}

现在,注释中的“主题”是订阅主题的名称。但什么是“id”字段?或者,还有更好的方法?

感谢您的帮助。

4

1 回答 1

2

@KafkaListener与 Spring Cloud Stream 无关;它位于 Spring for Apache Kafka 项目中(Spring Cloud Stream 将其用于其 Kafka 活页夹)。

请参阅 javadocs

    /**
     * The unique identifier of the container managing for this endpoint.
     * <p>If none is specified an auto-generated one is provided.
     * <p>Note: When provided, this value will override the group id property
     * in the consumer factory configuration, unless {@link #idIsGroup()}
     * is set to false.
     * <p>SpEL {@code #{...}} and property place holders {@code ${...}} are supported.
     * @return the {@code id} for the container managing for this endpoint.
     * @see org.springframework.kafka.config.KafkaListenerEndpointRegistry#getListenerContainer(String)
     */
    String id() default "";

id也用于从KafkaListenerEndpointRegistrybean 中获取侦听器容器,以便您可以使用stop()start()

如果您想改用 spring-cloud-stream ;阅读其文档。

于 2020-05-13T13:55:03.657 回答