本文分享自華為雲社群《手拉手入門springboot+kafka》,作者:QGS。
安裝kafka
啟動Kafka本地環境需Java 8+以上
Kafka是一種高吞吐量的分散式釋出訂閱訊息系統,它可以處理消費者在網站中的所有動作流資料。
Kafka啟動方式有Zookeeper和Kraft,兩種方式只能選擇其中一種啟動,不能同時使用。
Kafka下載https://downloads.apache.org/kafka/3.7.0/kafka_2.13-3.7.0.tgz
解壓tar -xzf kafka_2.13-3.7.0.tgz
一、Zookeeper啟動Kafka(kafka內建zookeeper)
Kafka依賴Zookeeper
1、啟動Zookeeper 2、啟動Kafka
使用kafka自帶Zookeeper啟動
./zookeeper-server-start.sh ../config/zookeeper.properties &
./zookeeper-server-stop.sh ../config/zookeeper.properties
./kafka-server-start.sh ../config/server.properties &
./kafka-server-stop.sh ../config/server.properties
二、Zookeeper伺服器啟動Kafka
Zookeeper伺服器安裝
https://zookeeper.apache.org/
https://dlcdn.apache.org/zookeeper/zookeeper-3.9.2/apache-zookeeper-3.9.2-bin.tar.gz
tar zxvf apache-zookeeper-3.9.2-bin.tar.gz
配置Zookeeper伺服器
cp zoo_sample.cfg zoo.cfg
啟動Zookeeper伺服器
./zkServer.sh start
修改Zookeeper埠
Zoo.cfg新增內容
admin.serverPort=8099
apache-zookeeper-3.9.2-bin/bin目錄下重啟Zookeeper
Zookeeper伺服器啟動kafka
/opt/kafka_2.13-3.7.0/bin目錄下
./kafka-server-start.sh ../config/server.properties &
Kafka配置檔案server.properties
三、使用KRaft啟動Kafka
UUID通用唯一識別碼(Universally Unique Identifier)
1、生成Cluster UUID(叢集UUID):./kafka-storage.sh random-uuid
2.格式化kafka日誌目錄:./kafka-storage.sh format -t 3pMJGNJcT0uLIBsZhbucjQ -c ../config/kraft/server.properties
3.啟動kafka:./kafka-server-start.sh ../config/kraft/server.properties &
springboot整合kafka
建立topic時,若不指定topic的分割槽(partition)數量使,則預設為1個分割槽(partition)
修改server.properties檔案
vim server.properties
listeners=PLAINTEXT://0.0.0.0:9092
advertised.listeners=PLAINTEXT://192.168.68.133:9092
springboot加入依賴kafka
<dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency>
加入spring-kafka依賴後,springboot自動裝配好kafkaTemplate的Bean
application.yml配置連線kafka
spring:
kafka:
bootstrap-servers: 192.168.68.133:9092
生產者
傳送訊息
@Resource
private KafkaTemplate<String,String> kafkaTemplate;
@Test
void kafkaSendTest(){
kafkaTemplate.send("kafkamsg01","hello kafka");
}
消費者
接收訊息
@Component
public class KafkaConsumer {
@KafkaListener(topics = {"kafkamsg01","test"},groupId = "123")
public void consume(String message){
System.out.println("接收到訊息:"+message);
}
}
若沒有配置groupid
Failed to start bean 'org.springframework.kafka.config.internalKafkaListenerEndpointRegistry'; nested exception is java.lang.IllegalStateException: No group.id found in consumer config, container properties, or @KafkaListener annotation; a group.id is required when group management is used.
@Component
public class KafkaConsumer {
@KafkaListener(topics = {"kafkamsg01","test"},groupId = "123")
public void consume(String message){
System.out.println("接收到訊息:"+message);
}
}
點選關注,第一時間瞭解華為雲新鮮技術~