1、安装java环境
1 2 3 4 5 6 7 |
[root@imzcy ~]# tar zxvf jdk-8u112-linux-x64.tar.gz -C /usr/local/ [root@imzcy ~]# ln -s /usr/local/jdk1.8.0_112/ /usr/local/java [root@imzcy ~]# vi /etc/profile export JAVA_HOME=/usr/local/java export CLASSPATH=${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar export PATH=${PATH}:${JAVA_HOME}/bin [root@imzcy ~]# source /etc/profile |
2、解压kafka压缩包,并修改两个配置文件
2.2.1 解压程序包到/opt/目录下,并做好符号链接,在其下创建zkdata数据目录
1 2 3 4 |
[root@imzcy ~]# tar zxvf kafka_2.12-1.1.0.tgz -C /opt/ [root@imzcy ~]# ln -s /opt/kafka_2.12-1.1.0/ /opt/kafka [root@imzcy ~]# cd /opt/kafka [root@imzcy ~]# mkdir zkdata |
2.2.2 修改kafka配置文件server.properties中第32行和125行配置项,添加本机IP
1 2 3 4 |
[root@imzcy kafka]# vim config/server.properties 32 listeners=PLAINTEXT://192.168.1.13:9092 125 zookeeper.connect=192.168.1.13:2181 [root@imzcy kafka]# |
2.2.3 修改zookeeper配置文件zookeeper.properties,16行data目录修改为刚才创建的zkdata的绝对路径,确认18行端口信息并在下面新增19行内容
1 2 3 4 5 |
[root@imzcy kafka]# vim config/zookeeper.properties 16 dataDir=/opt/kafka/zkdata 18 clientPort=2181 19 zookeeper.connect=192.168.1.13:2181 [root@imzcy kafka]# |
3、启动zookeeper和kafka程序
1 2 |
[root@imzcy kafka]# bin/zookeeper-server-start.sh config/zookeeper.properties [root@imzcy kafka]# bin/kafka-server-start.sh config/server.properties |
4、其他常用命令
4.1 jps命令查看所有java进程pid
QuorumPeerMain是ZooKeeper守护进程,kafka是kafka的守护进程
1 2 3 4 5 |
[root@imzcy kafka]# jps 11546 Jps 10396 QuorumPeerMain 10685 Kafka [root@imzcy kafka]# |
4.2 创建topic
1 2 3 4 |
//创建了一个名为zcy-test的topic,其中包含一个分区和一个副本因子。 [root@imzcy kafka]# bin/kafka-topics.sh --create --zookeeper 192.168.1.13:2181 --replication-factor 1 --partitions 1 --topic zcy-test Created topic "zcy-test". [root@imzcy kafka]# |
4.3 查看所有topic列表
1 2 3 |
[root@imzcy kafka]# bin/kafka-topics.sh --list --zookeeper imzcy:2181 zcy-test [root@imzcy kafka]# |
4.4 删除topic
1 |
[root@imzcy kafka]# bin/kafka-topics.sh --zookeeper 192.168.1.13:2181 --delete --topic zcy-test |
4.5 启动生产者以发送消息
1 2 3 4 5 |
[root@imzcy kafka]# bin/kafka-console-producer.sh --broker-list 192.168.1.13:9092 --topic zcy-test >123 >456 >888 > |
4.6 启动消费者以接收消息
使用ctrl + c中止运行后,会提示处理了几条消息
1 2 3 4 5 6 |
[root@imzcy kafka]# bin/kafka-console-consumer.sh --zookeeper 192.168.1.13:2181 -topic zcy-test --from-beginning Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper]. 123 456 888 Processed a total of 3 messages |
学习自w3cschool
完!