kafka教程-springboot消费者@KafkaListener注解

属性解释

  • id:监听器的id,不能重复。如果指定id,搭配 idIsGroup ,会影响 消费者组

  • idIsGroup:默认为 true

    • true:如果指定了 id,表示 id 就是 消费者组,此时配置文件中的 消费者组无效

    • false:配置文件中的 消费者组 才生效
      spring.kafka.consumer.properties.group.id=test-consumer-group

  • clientIdPrefix:消费者ID的前缀;

  • groupId:消费组ID,如果指定,其优先级最高

  • topics:监听的topic,可监听多个。
    不能与 topicPartitions 同时使用

  • topicPartitions:可配置更加详细的监听信息,可指定topic、parition、offset监听。
    不能与 topics 同时使用

  • concurrency :并发数量,启动多少个线程
    当配置 topicPartitions 后,开启的线程数与 topicPartitions 有关

  • errorHandler :指定错误处理类对象
    该类要实现接口 KafkaListenerErrorHandler,而且 errorHandler指定的名字,是实例在spring容器中的Name

源码

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@MessageMapping
@Documented
@Repeatable(KafkaListeners.class)
public @interface KafkaListener {
    String id() default "";

    String containerFactory() default "";

    String[] topics() default {};

    String topicPattern() default "";

    TopicPartition[] topicPartitions() default {};

    String containerGroup() default "";

    String errorHandler() default "";

    String groupId() default "";

    boolean idIsGroup() default true;

    String clientIdPrefix() default "";

    String beanRef() default "__listener";

    String concurrency() default "";

    String autoStartup() default "";

    String[] properties() default {};
}

原文出处:/show_1IXNJRiTIoo.html