SpringBoot集成Kafka 发表于 2019-05-20 更新于 2023-03-01 分类于 SpringBoot 本文字数: 73 阅读时长 ≈ 1 分钟 pom.xml12345<!-- kafka --><dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId></dependency> application.yml12345spring: kafka: bootstrap-servers: 192.168.3.206:9002 consumer: group-id: mygroup 提供者123456789101112@RunWith(SpringRunner.class)@SpringBootTestpublic class KafkaTest { @Resource private KafkaTemplate kafkaTemplate; @Test public void providerTest() { kafkaTemplate.send("myTopic", "Test Message!"); }} 消费者1234567891011@Component@Slf4jpublic class KafkaConsumer { @KafkaListener(topics = "myTopic") public void listen(ConsumerRecord<?, String> record) { String value = record.value(); log.info(value); log.info(JSON.toJSONString(record)); }}