背景
spring对Kafka有原生的支持,有对应的KafkaTemplate
kafka官网:http://kafka.apache.org/
对应kafka的安装,本篇就不再叙述。
demo源码:https://github.com/jc0803kevin/Queue/tree/master/springboot-kafka
依赖
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.2.8.RELEASE</version>
</dependency>
项目结构:
生产者
@RestController
@RequestMapping("/kafka")
public class TestKafkaProducerController {
@Autowired(required = false )
private KafkaTemplate<String, String> kafkaTemplate;
@RequestMapping("/send")
public String send(String message){
String temp = String.valueOf(System.currentTimeMillis());
message = message + temp;
kafkaTemplate.send("test_topic", message);
kafkaTemplate.send("test_topic2", message);
System.out.println("producer send message-->" + message);
return message;
}
}
消费者
消费者只需要实现监听,并且指定主题,若有多个customer不同group的需要,只需要配置不同的连接工厂即可
参考:https://blog.csdn.net/caijiapeng0102/article/details/80765923
@Component
public class RawDataConsumerListener {
@KafkaListener(topics = {"test_topic", "test_topic2"})
public void onMessage(String message){
System.out.println("test_topic consumer thread name->"+ Thread.currentThread().getName());
System.out.println("test_topic consumer receive message->"+ message);
}
@KafkaListener(topics = {"test_topic2"})
public void listence(ConsumerRecord<String, String> context){
//ConsumerRecord(topic = test_topic2, partition = 0, offset = 0, CreateTime = 1569395997850, serialized key size = -1,
// serialized value size = 18, headers = RecordHeaders(headers = [], isReadOnly = false), key = null, value = kevin1569395997499)
System.out.println("test_topic2 consumer thread name->"+ Thread.currentThread().getName());
System.out.println("test_topic2 consumer topic name->"+ context.topic());
System.out.println("test_topic2 consumer receive message->"+ context);
}
/**
* @Author kevin
* @Description 同一个主题 配置多个消费者 只会消费一次
* @Date Created on 2019/9/25 15:39
* @param
* @return
*/
@KafkaListener(topics = {"test_topic2"})
public void receive(ConsumerRecord<String, String> context){
System.out.println("receive test_topic2 consumer thread name->"+ Thread.currentThread().getName());
System.out.println("receive test_topic2 consumer topic name->"+ context.topic());
System.out.println("receive test_topic2 consumer receive message->"+ context);
}
}
配置文件
server.port=8082
spring.kafka.consumer.group-id=test
#kafka 集群服务地址 host:post 形式 逗号分隔
spring.kafka.consumer.bootstrap-servers=127.0.0.1:9092,127.0.0.1:9093
有了spring的支持,项目集成Kafka还是比较简单的,只需要对应配置一下。