原文鏈接:https://blog.csdn.net/tflasd1157/java/article/details/81985722
安裝命令
brew install kafka
kafka的安裝目錄:/usr/local/Cellar/kafka
kafka的配置文件目錄:/usr/local/etc/kafka
kafka服務(wù)的配置文件:/usr/local/etc/kafka/server.properties
zookeeper配置文件: /usr/local/etc/kafka/zookeeper.properties
# server.properties中的重要配置
broker.id=0
listeners=PLAINTEXT://:9092
advertised.listeners=PLAINTEXT://127.0.0.1:9092
log.dirs=/usr/local/var/lib/kafka-logs
# zookeeper.properties
dataDir=/usr/local/var/lib/zookeeper
clientPort=2181
maxClientCnxns=0
啟動zookeeper
# 新起一個終端啟動zookeeper
cd /usr/local/Cellar/kafka/1.0.0
./bin/zookeeper-server-start /usr/local/etc/kafka/zookeeper.properties
啟動kafka
# 新起一個終端啟動zookeeper,注意啟動kafka之前先啟動zookeeper
cd /usr/local/Cellar/kafka/1.0.0
./bin/kafka-server-start /usr/local/etc/kafka/server.properties
創(chuàng)建topic
# 新起一個終端來創(chuàng)建主題
cd /usr/local/Cellar/kafka/1.0.0
## 創(chuàng)建一個名為“test”的主題,該主題有1個分區(qū)
./bin/kafka-topics --create
--zookeeper localhost:2181
--partitions 1
--topic test
查看topic
// 創(chuàng)建成功可以通過 list 列舉所有的主題
./bin/kafka-topics --list --zookeeper localhost:2181
// 查看某個主題的信息
./bin/kafka-topics --describe --zookeeper localhost:2181 --topic <name>
發(fā)送消息
# 新起一個終端稠茂,作為生產(chǎn)者,用于發(fā)送消息蔼啦,每一行算一條消息,將消息發(fā)送到kafka服務(wù)器
> ./bin/kafka-console-producer --broker-list localhost:9092 --topic test
This is a message
This is another message
消費消息(接收消息)
# 新起一個終端作為消費者,接收消息
cd /usr/local/Cellar/kafka/1.0.0
> ./bin/kafka-console-consumer --bootstrap-server localhost:9092 --topic test --from-beginning
This is a message
This is another message
設(shè)置全局topic數(shù)據(jù)過期時間
log.retention.hours=72
log.cleanup.policy=delete