1、安装zookeeper
https://zookeeper.apache.org/releases.html
修改配置
启动
2、安装kafka
https://kafka.apache.org/downloads
修改配置
启动
.\bin\windows\kafka-server-start.bat .\config\server.properties
# 可以集群启动
#.\bin\windows\kafka-server-start.bat .\config\server2.properties
#.\bin\windows\kafka-server-start.bat .\config\serve3.properties
3、springboot整合kafka
依赖
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
配置
spring:
kafka:
bootstrap-servers: 127.0.0.1:9092
consumer:
group-id: test
生产者 & 消费者
package com.timi.t1.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
@Component
public class KafkaUtil {
public static final String TestQueueName = "TestQueueName";
private static KafkaTemplate<String, String> kafkaTemplate;
@Autowired
public void initKafka(KafkaTemplate<String, String> kafkaTemplate){
KafkaUtil.kafkaTemplate = kafkaTemplate;
}
public static void send(String msg){
System.out.println("send kafka msg");
kafkaTemplate.send(TestQueueName, msg);
}
@KafkaListener(topics = TestQueueName)
public static void receive(String msg){
System.out.println("receive kafka msg: " + msg);
}
}