Kafka伪集群搭建
1、下载kafka:https://kafka.apache.org/downloads
tar -zxvf kafka_2.12-2.6.0.tgz
2、Zookeeper配置
启动Zookeeper:
bin/zookeeper-server-start.sh -daemon config/zookeeper.properties
3、kafka配置
1)、复制配置文件
cp server.properties server01.properties
cp server.properties server02.properties
cp server.properties server03.properties
2)、修改配置
分别修改配置文件(设置三个分片)
修改server01.properties文件:
broker.id=0
listeners=PLAINTEXT://192.168.201.133:9092
num.partitions=3
offsets.topic.replication.factor=3
transaction.state.log.replication.factor=3
transaction.state.log.min.isr=3
zookeeper.connect=localhost:2181,localhost:2182,localhost:2183
log.dirs=/tmp/kafka01-logs
修改server02.properties文件:
broker.id=1
listeners=PLAINTEXT://192.168.201.133:9093
num.partitions=3
offsets.topic.replication.factor=3
transaction.state.log.replication.factor=3
transaction.state.log.min.isr=3
zookeeper.connect=localhost:2181,localhost:2182,localhost:2183
log.dirs=/tmp/kafka02-logs
修改server03.properties文件:
broker.id=2
listeners=PLAINTEXT://192.168.201.133:9094
num.partitions=3
offsets.topic.replication.factor=3
transaction.state.log.replication.factor=3
transaction.state.log.min.isr=3
zookeeper.connect=localhost:2181,localhost:2182,localhost:2183
log.dirs=/tmp/kafka03-logs
3)、启动kafka
./kafka-server-start.sh -daemon ../config/server01.properties
./kafka-server-start.sh -daemon ../config/server02.properties
./kafka-server-start.sh -daemon ../config/server03.properties
4)、测试及使用
a. 创建topic
bin/kafka-topics.sh --create --zookeeper 192.168.201.133:2181 --replication-factor 3 --partitions 3 --topic test
b. 查看主题
bin/kafka-topics.sh --list --zookeeper 192.168.201.133:2181
c. 发送消息
bin/kafka-console-producer.sh --broker-list 192.168.201.133:9092 --topic test
d. 接收消息
bin/kafka-console-consumer.sh --bootstrap-server 192.168.201.133:9093 --topic test --from-beginning
e. 查看特定主题的详细信息
bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic test
f.删除主题
bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic test
g.查看消费组详细信息
./kafka-consumer-groups.sh --bootstrap-server 192.168.201.133:9092 --describe --group aaa3
4、安装可视化工具
下载安装:https://www.kafkatool.com/download.html
kafka消费后不会自动删除数据,通过消费的offset来判断消费的位置,可以设置消息保留的时间:
全局设置:修改server.properties文件
log.retention.hours=72 #默认7天 按生产时间计算
log.retention.bytes=1073741824 #按文件大小,超过文件大小的话被删除
单独设置某个topic过期时间
./kafka-configs.sh --zookeeper localhost:2181 --alter --entity-name mytopic --entity-type topics --add-config retention.ms=86400000
立即删除某个topic消息
./kafka-topics.sh --zookeeper localhost:2181 --alter --topic mytopic --config cleanup.policy=delete
消费: