1、windows下安装、启动kafka,这一步只是搭建环境
https://blog.51cto.com/u_15595167/6026035
2、安装kafka管理工具:kafkamanager,推荐linux下安装,windows下编译等很麻烦
https://dandelioncloud.cn/article/details/1513885729167065090
3、springboot接入kafka
3.1、引入pom
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
3.2、yml配置
spring:
kafka:
bootstrap-servers: 127.0.0.1:9092
producer:
# 很重要很重要,一定要去看这个配置的含义
acks: all
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
consumer:
# 可以自定义,在consumer中
# group-id: node1-group
enable-auto-commit: false
3.3、创建发送者(sender)
@Autowired
private KafkaTemplate<String, String> k;
@GetMapping("/send")
public void send(){
k.send("register", "发送一条消息");
}
3.4、创建订阅者(consumer)
@Configuration
public class Consumer {
@KafkaListener(groupId = "node1-group", topics = "register")
public void consumer(String message){
log.info("接收到请求-1111");
log.info(message);
}
@KafkaListener(groupId = "node2-group", topics = "register")
public void consumer1(String message){
log.info("接收到请求-2222");
log.info(message);
}
}
基础流程结束。