部署
准备三台服务器,每台机器都需要安装 java 环境 、 zookeeper 、kafka。
- java 环境
Your local environment must have Java 8+
installed.
yum install java-1.8.0-openjdk-devel -y
java -version
- 下载 kafka
wget https://downloads.apache.org/kafka/3.7.1/kafka_2.13-3.7.1.tgz
tar -xzf kafka_2.13-3.7.1.tgz
cd kafka_2.13-3.7.1
- 配置
config/zookeeper.properties
dataDir=/home/zookeeper/data
dataLogDir=/home/zookeeper/log
clientPort=2181
tickTime=2000
initLimit=10
syncLimit=5
# 3台服务器IP
server.1=172.16.20.152:2888:3888
server.2=172.16.20.153:2888:3888
server.3=172.16.20.154:2888:3888
在 dataDir
目录下创建 myid
文件,3 台机器分别写 1
, 2
, 3
.
[root@data9 kafka_2.13-3.7.1]
1
- 启动zookeeper
bin/zookeeper-server-start.sh config/zookeeper.properties
bin/zookeeper-server-start.sh -daemon config/zookeeper.properties
- 配置
config/server.properties
# 3 台机器分别 0 1 2
broker.id=0
listeners=PLAINTEXT://:9092
# 填本机IP
advertised.listeners=PLAINTEXT://172.16.20.154:9092
# 修改日志目录
log.dirs=/home/kafka/kafka-logs
# 修改存储时间,默认7天
log.retention.hours=24
# 修改 zookeeper 地址
zookeeper.connect=172.16.20.152:2181,172.16.20.153:2181,172.16.20.154:2181
- 启动kafka
bin/kafka-server-start.sh config/server.properties
bin/kafka-server-start.sh -daemon config/server.properties
- 测试kafka
bin/kafka-topics.sh --create --topic quickstart-events --bootstrap-server 172.16.20.152:9092
bin/kafka-topics.sh --describe --topic quickstart-events --bootstrap-server 172.16.20.152:9092
bin/kafka-console-consumer.sh --topic quickstart-events --from-beginning --bootstrap-server 172.16.20.152:9092
bin/kafka-console-producer.sh --topic quickstart-events --bootstrap-server 172.16.20.154:9092
- 修改kafka内存使用上限
vim bin/kafka-server-start.sh
export KAFKA_HEAP_OPTS="-Xmx1G -Xms1G"
bin/kafka-server-stop.sh
bin/kafka-server-start.sh -daemon config/server.properties