目录
- Linux下Centos7系统部署搭建Kafka环境单机版本2.4.1
- 1.准备文件
- 2.安装jdk
- 3.安装zookeeper
- 3.1解压与生成配置文件
- 3.2 修改zookeeper中admin server的8080端口或停用内嵌的管理控制台(可选)
- 3.3 zookeeper设置-Xmx内存大小(可选)
- 3.4 zookeeper启动
- 4.kafka安装
- 4.1解压并修改配置文件
- 4.2 kafka启动停止命令
- 4.3kafka修改Jvm内存大小(可选)
- 5.kafka维护使用命令
- 6.其他问题
- 6.1kafka连Zk超时问题解决
此文档为Kafka单机版搭建文档,如果需要kafka集群搭建, 请点此查看
Linux下Centos7系统部署搭建Kafka环境单机版本2.4.1
1.准备文件
jdk-8u301-linux-x64.tar.gz 或其他Jdk8版本 —> JDK
kafka_2.12-2.4.1.tgz —> kafka安装包
apache-zookeeper-3.5.7-bin.tar.gz —> zookeeper安装包
jdk-8u301-linux-x64.tar.gz从oracle官网下载 https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
Kafka:https://archive.apache.org/dist/kafka/2.4.1/kafka_2.12-2.4.1.tgz
Zookeeper:http://archive.apache.org/dist/zookeeper/zookeeper-3.5.7/apache-zookeeper-3.5.7-bin.tar.gz
2.安装jdk
tar -xvf jdk-8u301-linux-x64.tar.gz -C /usr/local
cat >> /etc/profile << EOF
export JAVA_HOME=/usr/local/jdk1.8.0_301/
export PATH=\$JAVA_HOME/bin:\$PATH
export CLASSPATH=.:\$JAVA_HOME/lib/dt.jar:\$JAVA_HOME/lib/tools.jar
EOF
source /etc/profile
3.安装zookeeper
3.1解压与生成配置文件
#解压到目录/opt(你可以选择其他),并建立data目录
tar -xvf apache-zookeeper-3.5.7-bin.tar.gz -C /opt/
mkdir /opt/apache-zookeeper-3.5.7-bin/data
#配置文件生成
cat > /opt/apache-zookeeper-3.5.7-bin/conf/zoo.cfg << EOF
tickTime=2000
dataDir=/opt/apache-zookeeper-3.5.7-bin/data
dataLogDir=/opt/apache-zookeeper-3.5.7-bin/logs
clientPort=2181
initLimit=10
syncLimit=5
server.1=192.168.56.101:2888:3888
admin.serverPort=8080
EOF
#在data目录下生成myid文件取值为1,和server.1里面的1对应.
echo 1 > /opt/apache-zookeeper-3.5.7-bin/data/myid
3.2 修改zookeeper中admin server的8080端口或停用内嵌的管理控制台(可选)
zookeeper3.5.7版本中有个内嵌的管理控制台是通过jetty启动,会占用8080 端口,如上配置在zoo.cfg中的admin.serverPort,可以通过修改此配置来更换端口。
或者干脆直接停用这个服务通过在启动脚本中增加"-Dzookeeper.admin.enableServer=false"
,我是直接停止这个服务的,方法如下:
vi /opt/apache-zookeeper-3.5.7-bin/bin/zkServer.sh
找到-cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" > "$_ZOO_DAEMON_OUT" 2>&1 < /dev/null &这一行,大概在161行
在$JVMFLAGS后面增加"-Dzookeeper.admin.enableServer=false",如图:
3.3 zookeeper设置-Xmx内存大小(可选)
apache-zookeeper-3.5.7默认启动占用的-Xmx内存大小为1000m,如果资源不足或只是测试对zookeeper要求不高,可以将此-Xmx设置得小些,如256m
vi /opt/apache-zookeeper-3.5.7-bin/bin/zkEnv.sh
找到ZK_SERVER_HEAP="${ZK_SERVER_HEAP:-1000}"这一行
将上面的-1000修改成-256然后保存
或者直接命令进行:
sed -i 's/ZK_SERVER_HEAP="\${ZK_SERVER_HEAP:-1000}"/ZK_SERVER_HEAP="\${ZK_SERVER_HEAP:-256}"/' /opt/apache-zookeeper-3.5.7-bin/bin/zkEnv.sh
3.4 zookeeper启动
启动停止查看状态:
/opt/apache-zookeeper-3.5.7-bin/bin/zkServer.sh start
/opt/apache-zookeeper-3.5.7-bin/bin/zkServer.sh stop
/opt/apache-zookeeper-3.5.7-bin/bin/zkServer.sh status
4.kafka安装
4.1解压并修改配置文件
tar -xvf kafka_2.12-2.4.1.tgz -C /opt/
####修改/opt/kafka_2.12-2.4.1/config/server.properties
listeners=PLAINTEXT://192.168.56.101:9092
advertised.listeners=PLAINTEXT://192.168.56.101:9092
log.dirs=/opt/kafka_2.12-2.4.1/kafka-logs
log.retention.hours=24
zookeeper.connect=192.168.56.101:2181
zookeeper.connection.timeout.ms=60000
delete.topic.enable=true
auto.create.topics.enable=false
####如果需要修改kafka的jvm内存大小,修改kafka-server-start.sh(可选,默认是使用1G,一般不用修改)
export KAFKA_HEAP_OPTS="-Xmx256M -Xms128M"
参数说明:
log.dirs: kafka的数据存放目录
log.retention.hours: 数据过期时间,默认168小时
zookeeper.connect: zookeeper的连接地址
delete.topic.enable: 删除topic时是真正的删除还是先打好删除标记再处理
auto.create.topics.enable: 在topic不存在时是否能自动创建topic
附上直接执行的linux命令
,方便server.properties的操作修改
sed -i 's/^#listeners=PLAINTEXT:\/\/:9092/listeners=PLAINTEXT:\/\/192.168.56.101:9092/' /opt/kafka_2.12-2.4.1/config/server.properties
sed -i 's/^#advertised.listeners=PLAINTEXT:\/\/your.host.name:9092/advertised.listeners=PLAINTEXT:\/\/192.168.56.101:9092/' /opt/kafka_2.12-2.4.1/config/server.properties
sed -i 's/^log.dirs=\/tmp\/kafka-logs/log.dirs=\/opt\/kafka_2.12-2.4.1\/kafka-logs/' /opt/kafka_2.12-2.4.1/config/server.properties
sed -i 's/^log.retention.hours=168/log.retention.hours=24/' /opt/kafka_2.12-2.4.1/config/server.properties
sed -i 's/^zookeeper.connect=localhost:2181/zookeeper.connect=192.168.56.101:2181/' /opt/kafka_2.12-2.4.1/config/server.properties
sed -i 's/^zookeeper.connection.timeout.ms=6000/zookeeper.connection.timeout.ms=60000/' /opt/kafka_2.12-2.4.1/config/server.properties
cat >> /opt/kafka_2.12-2.4.1/config/server.properties << EOF
delete.topic.enable=true
auto.create.topics.enable=false
EOF
4.2 kafka启动停止命令
# 后台启动kafka
cd /opt/kafka_2.12-2.4.1
./bin/kafka-server-start.sh -daemon config/server.properties
# 停止:
./bin/kafka-server-stop.sh
4.3kafka修改Jvm内存大小(可选)
如果希望改kafka的Jvm内存,改以下参数即可,默认没有配置参数时为1G
vi /opt/kafka_2.12-2.4.1/bin/kafka-server-start.sh
找到export KAFKA_HEAP_OPTS="-Xmx1G -Xms1G"这一行,
修改成如export KAFKA_HEAP_OPTS="-Xmx2048M -Xms2048M"
5.kafka维护使用命令
先cd到kafka的bin目录
cd /opt/kafka_2.12-2.4.1/bin
##显示topic列表,2者都可--bootstrap-server高版本kafka才支持
./kafka-topics.sh --list --zookeeper 192.168.56.101:2181
./kafka-topics.sh --list --bootstrap-server 192.168.56.101:9092
##创建topic,2条命令都可,建议使用--bootstrap-server参数的命令
./kafka-topics.sh --create --zookeeper 192.168.56.101:2181 --replication-factor 1 --partitions 1 --topic topictest
./kafka-topics.sh --create --bootstrap-server 192.168.56.101:9092 --replication-factor 1 --partitions 1 --topic topictest
##删除topic,建议使用--bootstrap-server参数的命令
./kafka-topics.sh --delete --zookeeper 192.168.56.101:2181 --topic topictest
./kafka-topics.sh --delete --bootstrap-server 192.168.56.101:9092 --topic topictest
##启动命令行生产者进行测试
./kafka-console-producer.sh --broker-list 192.168.56.101:9092 --topic topictest
##启动命令行消费者进行测试
./kafka-console-consumer.sh --bootstrap-server 192.168.56.101:9092 --from-beginning --topic topictest
##查看topic的描述信息
./kafka-topics.sh --describe --bootstrap-server 192.168.56.101:9092 --topic topictest
##修改分区个数(只能增加)
./kafka-topics.sh --alter --bootstrap-server 192.168.56.101:9092 --topic topictest --partitions 3
注:其中修改分区个数只能增加分区不能减少,如修改后的分区个数比当前的要少,将报以下问题:
[root@localhost bin]# ./kafka-topics.sh --alter --bootstrap-server 192.168.56.101:9092 --topic topictest --partitions 1
Error while executing topic command : org.apache.kafka.common.errors.InvalidPartitionsException: Topic currently has 3 partitions, which is higher than the requested 1.
[2021-10-13 18:42:02,531] ERROR java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.InvalidPartitionsException: Topic currently has 3 partitions, which is higher than the requested 1.
at org.apache.kafka.common.internals.KafkaFutureImpl.wrapAndThrow(KafkaFutureImpl.java:45)
at org.apache.kafka.common.internals.KafkaFutureImpl.access$000(KafkaFutureImpl.java:32)
at org.apache.kafka.common.internals.KafkaFutureImpl$SingleWaiter.await(KafkaFutureImpl.java:89)
at org.apache.kafka.common.internals.KafkaFutureImpl.get(KafkaFutureImpl.java:260)
at kafka.admin.TopicCommand$AdminClientTopicService.alterTopic(TopicCommand.scala:267)
at kafka.admin.TopicCommand$.main(TopicCommand.scala:63)
at kafka.admin.TopicCommand.main(TopicCommand.scala)
Caused by: org.apache.kafka.common.errors.InvalidPartitionsException: Topic currently has 3 partitions, which is higher than the requested 1.
(kafka.admin.TopicCommand$)
6.其他问题
6.1kafka连Zk超时问题解决
在kafka的配置中(server.properties) 把连接zookeeper的超时时间改久些就可以了 如在server.properties 中加入以下配置 zookeeper.connection.timeout.ms=60000
如果已经存在这个配置 就直接修改值