赞
踩
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka-test</artifactId> <scope>test</scope> </dependency> </dependencies>
spring: application: name: sringboot-kafka-lemon kafka: # 指定kafka地址 bootstrap-servers: 172.17.10.110:9092 # 配置指定的key和value的序列化器 producer: key-serializer: org.apache.kafka.common.serialization.StringSerializer value-serializer: org.apache.kafka.common.serialization.StringSerializer # 配置指定的key和value的反序列化器 consumer: key-deserializer: org.apache.kafka.common.serialization.StringDeserializer value-deserializer: org.apache.kafka.common.serialization.StringDeserializer # 指定消费者组 group-id: lemon
@RestController
public class ProducerController {
@Autowired
KafkaTemplate<String,String> kafkaTemplate;
/**
* 发送到指定的topic
*/
@RequestMapping("/send")
public String send(String msg){
kafkaTemplate.send("firstSend", msg);
return "发送成功";
}
}
@Component
public class KafkaConsumer {
@KafkaListener(topics = "firstSend")
public void consumerTopics(String msg){
System.out.println("receive:"+msg);
}
}
localhost:8080/send/msg="hello,kafka“
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。