SpringBoot接入kafka

引入依赖

1
2
3
4
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>

消费者模式

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#kafka消费服务地址
spring.kafka.consumer.bootstrap-servers=ip1:port,ip2:port
#是否自动提交
spring.kafka.consumer.enable-auto-commit=true
#提交间隔时间
spring.kafka.consumer.auto-commit-interval=100ms
#key反序列化
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
#value反序列化
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
#心跳超时时间
spring.kafka.consumer.properties.session.timeout.ms=15000
#消费者groupID
spring.kafka.consumer.group-id=test-group-id

代码

1