如何在Spring Cloud Stream Kafka绑定中写一个订阅主题的方法?

问题描述 投票:0回答:1

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

我找到了一个答案,它建议这样做------。

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

现在,在注释中,"topics "是被订阅的主题的名称,但 "id "字段是什么?但是'id'字段是什么?或者有什么更好的方法吗?

感谢你的帮助。

java apache-kafka spring-cloud-stream spring-cloud-stream-binder-kafka
1个回答
2
投票

@KafkaListener 与Spring Cloud Stream无关;它在Spring for Apache Kafka项目中(Spring Cloud Stream使用它的Kafka binder)。

请参阅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 也用于从监听器容器的 KafkaListenerEndpointRegistry 豆子,所以你可以 stop()start() 它。

如果你想使用spring-cloud-stream,请阅读它的文档。

© www.soinside.com 2019 - 2024. All rights reserved.