官方文档:   https://docs.spring.io/spring-kafka/reference/html/

@KafkaListener

The @KafkaListener annotation is used to designate a bean method as a listener for a listener container. The bean is wrapped in a MessagingMessageListenerAdapter configured with various features, such as converters to convert the data, if necessary, to match the method parameters.

If, say, six TopicPartition instances are provided and the concurrency is 3; each container gets two partitions. For five TopicPartition instances, two containers get two partitions, and the third gets one. If the concurrency is greater than the number of TopicPartitions, the concurrency is adjusted down such that each container gets one partition.

You can now configure a KafkaListenerErrorHandler to handle exceptions. See Handling Exceptions for more information.

By default, the @KafkaListener id property is now used as the group.id property, overriding the property configured in the consumer factory (if present). Further, you can explicitly configure the groupId on the annotation. Previously, you would have needed a separate container factory (and consumer factory) to use different group.id values for listeners. To restore the previous behavior of using the factory configured group.id, set the idIsGroup property on the annotation to false.

示例:

demo类:

public class Listener {

    @KafkaListener(id = "foo", topics = "myTopic", clientIdPrefix = "myClientId")
public void listen(String data) {
...
} }
配置类及注解:
@Configuration
@EnableKafka
public class KafkaConfig { @Bean
KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<Integer, String>>
kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<Integer, String> factory =
new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
factory.setConcurrency(3);
factory.getContainerProperties().setPollTimeout(3000);
return factory;
} @Bean
public ConsumerFactory<Integer, String> consumerFactory() {
return new DefaultKafkaConsumerFactory<>(consumerConfigs());
} @Bean
public Map<String, Object> consumerConfigs() {
Map<String, Object> props = new HashMap<>();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, embeddedKafka.getBrokersAsString());
...
return props;
}
}

Spring for Apache Kafka @KafkaListener使用及注意事项的更多相关文章

  1. Spring for Apache Kafka

    官方文档详见:http://docs.spring.io/spring-kafka/docs/1.0.2.RELEASE/reference/htmlsingle/ Authors Gary Russ ...

  2. Spring 对Apache Kafka的支持与集成

    1. 引言 Apache Kafka 是一个分布式的.容错的流处理系统.在本文中,我们将介绍Spring对Apache Kafka的支持,以及原生Kafka Java客户端Api 所提供的抽象级别. ...

  3. spring boot与kafka

    1.项目搭建 2.关键代码与配置 3.性能调优 注意,本项目基于spring boot 1,如果是spring boot 2有可能会报错.相应的包需要更新 1.项目搭建 kafka版本:kafka_2 ...

  4. Spring boot 集成Kafka

    搭建Kafka集群,参考: https://www.cnblogs.com/jonban/p/kafka.html 源码示例如下: 1.新建 Maven 项目 kafka 2.pom.xml < ...

  5. spring boot 2.x 系列 —— spring boot 整合 kafka

    文章目录 一.kafka的相关概念: 1.主题和分区 2.分区复制 3. 生产者 4. 消费者 5.broker和集群 二.项目说明 1.1 项目结构说明 1.2 主要依赖 二. 整合 kafka 2 ...

  6. Spring Boot 自定义kafka 消费者配置 ContainerFactory最佳实践

    Spring Boot 自定义kafka 消费者配置 ContainerFactory最佳实践 本篇博文主要提供一个在 SpringBoot 中自定义 kafka配置的实践,想象这样一个场景:你的系统 ...

  7. java企业架构 spring mvc +mybatis + KafKa+Flume+Zookeeper

    声明:该框架面向企业,是大型互联网分布式企业架构,后期会介绍linux上部署高可用集群项目. 项目基础功能截图(自提供了最小部分)      平台简介        Jeesz是一个分布式的框架,提供 ...

  8. Flafka: Apache Flume Meets Apache Kafka for Event Processing

    The new integration between Flume and Kafka offers sub-second-latency event processing without the n ...

  9. 【转载】Understanding When to use RabbitMQ or Apache Kafka

    https://content.pivotal.io/rabbitmq/understanding-when-to-use-rabbitmq-or-apache-kafka RabbitMQ: Erl ...

随机推荐

  1. 客户端JavaScript基础 网页上的输入输出

    创建: 2017/10/06 完成: 2017/10/07   更新: 2017/10/15 增加了通过事件监听器登陆事件的方法的链接   # TODO: 补充 基于Canvas的图象 客户端Java ...

  2. nodejs操作mysql

    var mysql = require('mysql');var pool = mysql.createPool({ host: 'localhost', user: 'root', password ...

  3. [Swift通天遁地]二、表格表单-(2)创建右侧带有索引的UITableView(表单视图)

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  4. aixcoder智能编程助手开发插件推荐

    1. aixcoder安装使用 1.1. 介绍 1.1.1. 功能 智能代码提示她用强大的深度学习引擎,能给出更加精确的代码提示: 代码风格检查她有代码风格智能检查能力,帮助开发者改善代码质量: 编程 ...

  5. 引水工程 Kruskal + Prim

    Kruskal题解 : 以案例输入为例 有五个缺水地区 , 这个个缺水地区之间建立联系的费用已经给出 并且之间水库的费用也已经给出 , 自己水库也已看为 是另一个 点 , 这样就有了 6 个点 , 这 ...

  6. BZOJ 4867 分块+神tm卡常

    思路: 注意到len<=10 按照权值max-min<=sqrt(n)*len 分块 记一下前缀和  每修改sqrt(n)次以后重新分块   修改的时候整块打标记  两边重构 (这题常数卡 ...

  7. UNIX环境高级编程--3

    文件IO 函数lseek: 每个打开文件都有一个与其相关联的“当前文件偏移量”,用来度量从文件开始处计算的字节数.除非指定O_APPEND选项,否则该偏移量被置为0.如果文件描述符指向的是一个管道.F ...

  8. SRM-697-DIV2

    Div2 Medium: DivisibleSetDiv2 Problem Statement      You are given a vector <int> b containing ...

  9. 【MySQL】RPM包安装

    操作系统:Red Hat Enterprise Linux Server release 6.5 Mysql安装包:MySQL-5.6.35-1.linux_glibc2.5.x86_64.rpm-b ...

  10. C#——简单工厂

    简单工厂的方法实现过程核心就是之前介绍的接口应用.所以直接上代码: public interface IPerson { void Say(); } public class Student : IP ...