一.准备ZooKeeper
从官网下载最新稳定版本:3.5.6。下载后解压到本地,在 conf
目录下找到 zoo_sample.cfg
,重命名为 zoo.cfg
修改里面的数据存放目录:
#dataDir=/tmp/zookeeper
dataDir=D:/hecg/apache-zookeeper-3.5.6-bin/data
使用 git
命令行客户端启动:
## 测试启动,正常
$ ./zkServer.sh start
/c/ProgramData/Oracle/Java/javapath/java
ZooKeeper JMX enabled by default
Using config: D:\hecg\apache-zookeeper-3.5.6-bin\conf\zoo.cfg
Starting zookeeper ... STARTED
## 测试停止,正常
$ ./zkServer.sh stop
/c/ProgramData/Oracle/Java/javapath/java
ZooKeeper JMX enabled by default
Using config: D:\hecg\apache-zookeeper-3.5.6-bin\conf\zoo.cfg
二.准备Kafka
从官网下载的版本为:kafka_2.12-2.4.0
2.1 修改配置文件
-
修改
config
目录下的 kafka 配置文件,修改日志存放目录:#log.dirs=/tmp/kafka-logs log.dirs=D:/Program/kafka_2.12-2.4.0/data/kafka-logs
-
修改
config
目录下的 zookeeper 配置文件,修改数据存放目录:#dataDir=/tmp/zookeeper dataDir=D:/hecg/kafka_2.12-2.4.0/data/zookeeper
2.2 启动ZooKeeper:
$ bin/zookeeper-server-start.sh config/zookeeper.properties
[2020-01-20 14:46:01,282] INFO Reading configuration from: config/zookeeper.properties (org.apache.zookeeper.server.quorum.QuorumPeerConfig)
[2020-01-20 14:46:01,283] WARN config\zookeeper.properties is relative. Prepend.\ to indicate that you're sure! (org.apache.zookeeper.server.quorum.QuorumPeerConfig)
[2020-01-20 14:46:01,284] INFO clientPortAddress is 0.0.0.0/0.0.0.0:2181 (org.apache.zookeeper.server.quorum.QuorumPeerConfig)
...
2.3 启动Kafka Server:
$ bin/kafka-server-start.sh config/server.properties
[2020-01-20 14:50:57,961] INFO Registered kafka:type=kafka.Log4jController MBean (kafka.utils.Log4jControllerRegistration$)
[2020-01-20 14:50:58,388] INFO starting (kafka.server.KafkaServer)
[2020-01-20 14:50:58,389] INFO Connecting to zookeeper on localhost:2181 (kafka.server.KafkaServer)
[2020-01-20 14:50:58,407] INFO [ZooKeeperClient Kafka server] Initializing a new session to localhost:2181. (kafka.zookeeper.ZooKeeperClient)
[2020-01-20 14:50:58,412] INFO Client environment:zookeeper.version=3.5.6-c11b7e26bc554b8523dc929761dd28808913f091, built on 10/08/2019 20:18 GMT (org.apache.zookeeper.ZooKeeper)
...
2.4 创建Topic
-
创建Topic:
$ bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test
-
列出所有的Topic:
$ bin/kafka-topics.sh --list --bootstrap-server localhost:9092 test
期间出现了小插曲:加载不到log4j的配置文件:写死配置文件后好了
$ bin/kafka-topics.sh --list --bootstrap-server localhost:9092
log4j:ERROR Could not read configuration file from URL [file:/d/hecg/kafka_2.12-2.4.0/bin/../config/tools-log4j.properties].
java.io.FileNotFoundException: \d\hecg\kafka_2.12-2.4.0\bin\..\config\tools-log4j.properties
...
## 修改 kafka-run-class.sh line 194
#LOG4J_DIR="$base_dir/config/tools-log4j.properties"
LOG4J_DIR="D:/hecg/kafka_2.12-2.4.0/config/tools-log4j.properties"
2.5 发送和接收消息测试
-
指定Topic发送消息:
$ bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test >This is a message >This is another message >hello hecg >
-
指定Topic接收消息:
$ bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning This is a message This is another message hello hecg