这里没有直接使用 spring封装的kafkaTemplate, 使用的是 原生的 Java API ,支持使用拉取型消费者 并使用 poll() 方法拉取消费消息.
1. 消费者具体常用配置项
pom.xml 文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>kafka-consumer-spring-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>kafka-consumer-spring-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.yml :
kafka:
bootstrap-servers: 127.0.0.1:9092 #指定kafka server的地址,集群配多个,中间,逗号隔开
producer:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
batch-size: 1000 # 每次批量发送消息的数量
consumer:
# 消费群组ID
group-id: test-fya-first
# 是否自动提交
enable-auto-commit: false
auto-commit-interval: 1000
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
# 每次最大拉取的消息数目
max-poll-records: 100
session-timeout: 30000
# 新消费组消费初始位置, 默认为 latest 最后点开始
auto-offset-reset: earliest
# 每次 poll 的允许最大阻塞时间, 单位 ms
pollTimeout: 1000
# 消费者个数, 建议设置不大于分区数目
consumerCount: 11
# Topic名称
topicName: topic.quick.initial
KafkaConsumer 配置类 KafkaConsumerConfig类:
@Configuration
public class KafkaConsumerConfig {
@Value("${kafka.bootstrap-servers}")
private String servers;
@Value("${kafka.consumer.enable-auto-commit}")
private boolean enableAutoCommit;
@Value("${kafka.consumer.session-timeout}")
private String sessionTimeout;
@Value("${kafka.consumer.auto-commit-interval}")
private String autoCommitInterval;
@Value("${kafka.consumer.group-id}")
private String groupId;
@Value("${kafka.consumer.auto-offset-reset}")
private String autoOffsetReset;
@Value("${kafka.consumer.max-poll-records}")
private int maxPollRecord;
@Value("${kafka.consumer.key-deserializer}")
private String keyDeserializer;
@Value("${kafka.consumer.value-deserializer}")
private String valueDeserializer;
@Value("${kafka.consumer.consumerCount}")
protected int consumerCount;
public Properties consumerConfig(){
Properties props = new Properties();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, servers);
props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, enableAutoCommit);
props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, autoCommitInterval);
props.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, sessionTimeout);
props.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,autoOffsetReset);
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, keyDeserializer);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, valueDeserializer);
props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, maxPollRecord);
return props;
}
}
2. 其他
具体消费使用, 可以参考阿里云的 Demo : 阿里云文档-kafka多消费者拉取Demo
如果有其他的kafka相关的问题, 推荐先参考阿里云的相关文档: 阿里云文档–kafka收发消息
3. 使用时的注意事项
-
当开启多个消费者时, 不需要消费时, 建议手动调用 consumer.close() 方法移除消费者, 避免卡顿
若未使用 consumer.close() 关闭消费者, 那么在下一次开启服务消费时可能会有较长时间的 rebalance ,这个卡顿时间大约是10秒左右. -
如果开启多个消费者并发拉取消息, 注意消费者的个数不能大于 分区 partition 的数目