有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在Spring Cloud Stream Kafka binding中编写订阅主题的方法?

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

我找到了一个答案,表明了这一点-

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

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

谢谢你的帮助


共 (1) 个答案

  1. # 1 楼答案

    @KafkaListener与春云流无关;这是ApacheKafka项目的春天(SpringCloudStream将其用于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;阅读它的文档