Spring Boot整合Kafka
pom.xml
<dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency>
默认使用
配置
prop新增kafka配置 详细配置可参考org.springframework.boot.autoconfigure.kafka.KafkaProperties中属性 生产者,消费者默认序列化都是String格式message
#kafka默认消费者配置 spring.kafka.consumer.bootstrap-servers=10.111.7.124:9092 spring.kafka.consumer.enable-auto-commit=true spring.kafka.consumer.auto-offset-reset=earliest #kafka默认生产者配置 spring.kafka.producer.bootstrap-servers=10.111.7.124:9092 spring.kafka.producer.acks=-1 spring.kafka.client-id=kafka-producer spring.kafka.producer.batch-size=5
使用
//生产者 @Resource private KafkaTemplate kafkaTemplate; public void send() { HashMap<String, String> map = new HashMap<>(); map.put("sendType","send"); kafkaTemplate.send("test01", JSONUtil.toJsonStr(map)); }
//消费者 @Slf4j @Component public class KafkaConsumer { @KafkaListener(topics = "test01",groupId = "group01") public void listen(String message) { log.info("message:{}",message); } }
使用Json格式化
配置
自定义消费者工厂: 配置全部消费者参数,包含转换器,使用此项可删除转换器配置2
**转换器配置2:**仅配置json转换器,不需要配置全部消费者参数时可删除自定义消费者配置
addTrustedPackages("*"):默认Json转换仅信任java.util,java.lang包下类,增加后不进行类型检查
使用
vo
@Data public class TestVo { private String key; private String value; }
生产者
//注入自定义KafkaTemplate @Resource(name = "custiomKafkaTemplate") private KafkaTemplate custiomKafkaTemplate; public void customSend() { TestVo vo = new TestVo(); vo.setKey("sendType"); vo.setValue("send"); custiomKafkaTemplate.send("custom04", vo); }
消费者
containerFactory:指定自定义消费者工厂beanName
@Slf4j @Component public class KafkaConsumer { //自定义消费者 @KafkaListener(topics = "custom04",containerFactory = "customContainerFactory") public void customListen(TestVo message) { log.info("message:{}",message.toString()); } //仅自定义转换器 @KafkaListener(topics = "custom04",groupId = "custom04Group") public void customListen(TestVo message) { log.info("message:{}",message.toString()); } }
参考资料
https://github.com/spring-projects/spring-kafka/tree/master/samples
https://blog.csdn.net/u012045045/article/details/111034500
https://www.tutorialspoint.com/spring_boot/spring_boot_apache_kafka.htm