安装kafka集群
安装jdk
# wget https://download.oracle.com/java/18/latest/jdk-18_linux-x64_bin.rpm
# rpm -ivh jdk-18_linux-x64_bin.rpm
# java -version
下载kafka
# wget https://dlcdn.apache.org/kafka/3.2.0/kafka_2.13-3.2.0.tgz
将kafka传输到另外两台机器
# scp kafka_2.13-3.2.0.tgz 10.0.0.15:/root/
# scp kafka_2.13-3.2.0.tgz 10.0.0.12:/root/
解压
# tar -zxvf kafka_2.13-3.2.0.tgz -C /usr/local/
创建日志和数据目录
# mkdir -p /home/kafka/data
# mkdir -p /home/kafka/logs
修改kafka配置文件,注意:配置文件需要填写zk地址需要提前装好zk,其他两台需要修改broker.id
# vim server.properties
# cat /usr/local/kafka_2.13-3.2.0/config/server.properties | grep -v '#' | grep -v '^$'
broker.id=0
listeners=PLAINTEXT://10.0.0.13:9092
advertised.listeners=PLAINTEXT://10.0.0.13:9092
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/home/kafka/logs
num.partitions=1
num.recovery.threads.per.data.dir=1
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connect=10.0.0.13:2181,10.0.0.15:2181,10.0.0.12:2181
zookeeper.connection.timeout.ms=18000
group.initial.rebalance.delay.ms=0
启动kafka
/usr/local/kafka_2.13-3.2.0/bin/kafka-server-start.sh -daemon /usr/local/kafka_2.13-3.2.0/config/server.properties
测试
创建topic
[root@VM-0-12-centos kafka_2.13-3.2.0]# ./bin/kafka-topics.sh --create --bootstrap-server 10.0.0.13:9092,10.0.0.15:9092,10.0.0.12:9092 --replication-factor 3 --partitions 1 --topic test
Created topic test
[root@VM-0-12-centos kafka_2.13-3.2.0]# ./bin/kafka-topics.sh --create --bootstrap-server 10.0.0.13:9092,10.0.0.15:9092,10.0.0.12:9092 --replication-factor 3 --partitions 1 --topic test1
Created topic test1.
查看topic
[root@VM-0-12-centos kafka_2.13-3.2.0]# ./bin/kafka-topics.sh --list --bootstrap-server 10.0.0.13:9092,10.0.0.15:9092,10.0.0.12:9092
test
test1
删除topic
[root@VM-0-12-centos kafka_2.13-3.2.0]# ./bin/kafka-topics.sh --bootstrap-server 10.0.0.13:9092,10.0.0.15:9092,10.0.0.12:9092 --delete --topic test
查看topic
[root@VM-0-12-centos kafka_2.13-3.2.0]# ./bin/kafka-topics.sh --list --bootstrap-server 10.0.0.13:9092,10.0.0.15:9092,10.0.0.12:9092
test1
创建生产者
./bin/kafka-console-producer.sh --broker-list 10.0.0.13:9092,10.0.0.15:9092,10.0.0.12:9092 --topic test
创建消费者
./bin/kafka-console-consumer.sh --bootstrap-server 10.0.0.13:9092,10.0.0.15:9092,10.0.0.12:9092 --topic test --from-beginning --consumer.config config/consumer.properties
查看集群topic信息
./bin/kafka-topics.sh --describe --bootstrap-server 10.0.0.13:9092,10.0.0.15:9092,10.0.0.12:9092 --topic test