前置条件
必须安装zookeeper。Linux安装zookeeper
kafka官方源
下载kafka安装包
# 版本:kafka_2.11-2.3.0.tgz
wget http://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.3.0/kafka_2.11-2.3.0.tgz
解压安装包
tar -zxvf kafka_2.11-2.3.0.tgz
cp kafka_2.11-2.3.0 /usr/local/kafka
启动kafka服务
cd /usr/local/kafka/bin
# 启动命令:守护进程进行启动
./kafka-server-start.sh -daemon ../config/server.properties
备注:这里有可能报错,因为默认情况下,kafka默认的内存要1G,如果你这里没有这么多内存可用,那么就会启动失败。报错如下:
Java Hotspot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c5330000, 986513408, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (malloc) failed to allocate 986513408 bytes for committing reserved memory.
# An error report file with more information is saved as:
# hs_err_pid5535.log
解决方案:
修改启动脚本:kafka-server-start.sh
vim kafka-server-start.sh
设置内容根据自己情况进行设置。
创建一个topic
# 名为test的chart,只有一个副本,一个分区
kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic chart
# 查看kafka的topic
kafka-topics.sh -list -zookeeper localhost:2181
生产、消费测试
# 启动生产端
./kafka-console-producer.sh --broker-list localhost:9092 --topic chart
#启动消费端
./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic chart --from-beginning
来源:51CTO
作者:刺激乐天派
链接:https://blog.51cto.com/4837471/2466638