1、安装单机版的kafka
1.1、下载kafka压缩包
下载地址在:https://mirror-hk.koddos.net/apache/kafka/2.7.0/kafka_2.12-2.7.0.tgz
解压(注意:我这里在windows下使用,用git base客户端进行操作)
tar -xzf kafka_2.12-2.7.0.tgz
1.2、启动kafka
1.2.1、启动zookeeper
进入对应的目录,然后执行:bin/zookeeper-server-start.sh config/zookeeper.properties
打印的日志信息:
[2021-01-09 17:12:28,498] INFO Reading configuration from: config/zookeeper.properties (org.apache.zookeeper.server.quorum.QuorumPeerConfig)
[2021-01-09 17:12:28,499] WARN config\zookeeper.properties is relative. Prepend .\ to indicate that you're sure! (org.apache.zookeeper.server.quorum.QuorumPeerConfig)
[2021-01-09 17:12:28,500] WARN \tmp\zookeeper is relative. Prepend .\ to indicate that you're sure! (org.apache.zookeeper.server.quorum.QuorumPeerConfig)
[2021-01-09 17:12:28,518] INFO clientPortAddress is 0.0.0.0:2181 (org.apache.zookeeper.server.quorum.QuorumPeerConfig)
1.2.2、启动kafka
执行指令: bin/kafka-server-start.sh config/server.properties
打印的日志:
[2021-01-09 17:18:30,404] INFO Registered kafka:type=kafka.Log4jController MBean (kafka.utils.Log4jControllerRegistration$)
[2021-01-09 17:18:30,858] INFO Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation (org.apache.zookeeper.common.X509Util)
[2021-01-09 17:18:30,895] INFO starting (kafka.server.KafkaServer)
[2021-01-09 17:18:30,896] INFO Connecting to zookeeper on localhost:2181 (kafka.server.KafkaServer)
1.2.3、创建一个topic
执行指令:bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic jeffchan0
打印的日志:
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Created topic jeffchan0.
我在windows的环境下,这里有个异常:
log4j:ERROR Could not read configuration file from URL [file:/d/jeffchan/other-meterial/kafka_2.12-2.7.0/bin/…/config/tools-log4j.properties].
java.io.FileNotFoundException: \d\jeffchan\other-meterial\kafka_2.12-2.7.0\bin…\config\tools-log4j.properties (???)
解决方法:直接将config目录下的tools-log4j.properties文件的绝对路路径(每个人的绝对路径一般不同,你复制你对应的绝对路径就可以了):
D:\jeffchan\other-meterial\kafka_2.12-2.7.0\config\tools-log4j.properties
打开:kafka-run-class.sh文件,将:
$base_dir/config/tools-log4j.properties 替换成绝对路经 D:\jeffchan\other-meterial\kafka_2.12-2.7.0\config\tools-log4j.properties
然后重新启动kafka
查看上述创建的topic:
bin/kafka-topics.sh --list --zookeeper localhost:2181
1.2.4、启动生产者
执行命令(往topic jeffchan0中发送消息):
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic jeffchan0
启动之后,可以往主题 jeffchan0中发送消息
这里发送了hello jeffchan的消息
1.2.5、启动消费者
执行命令:注意–from-beginning表示从头开始读取
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic jeffchan0 --from-beginning
可以发现,上述生产者发布的消息已经被消费者消费了。
参考资料: https://kafka.apachecn.org/quickstart.html