application.yml spring: kafka: bootstrap-servers: ip:9092 consumer: enable-auto-commit: true group-id: kafka auto-offset-reset: earliest #启东时接收没有接收到的数据
pom.xml
<dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency>
Consumer
@Component public class TestConsumer { @KafkaListener(topics = "topic名字") public void listen (ConsumerRecord<?, ?> record) throws Exception { System.out.printf("topic = %s, offset = %d, value = %s \n", record.topic(), record.offset(), record.value()); } }
@Autowired private KafkaTemplate kafkaTemplate; @RequestMapping("send") public String send(String msg){ logger.info("生产者生产的消息:"+msg); kafkaTemplate.send("test", msg); return "success"; }