官方文档
https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#boot-features-kafka
POM添加jar支持
<dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency>
消费者
package com.jachs.kafka.config; import org.springframework.kafka.annotation.KafkaListener; import org.springframework.stereotype.Component; @Component public class Process { @KafkaListener(topics = "test1") public void processMessage(String content) { System.out.println(content); } }
生产者
package com.jachs.kafka.controller; import java.util.Date; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class SendMessAge { @Autowired private KafkaTemplate kafkaTemplate; @RequestMapping("/send") public String send() { kafkaTemplate.send("test1",new Date().getTime()+""); return ""; } }
KafkaTemplate 自动注入的对象
spring.kafka.bootstrap-servers=127.0.0.1:9092 spring.kafka.consumer.group-id=myGroup