Fork me on GitHub

Kafka安装及入门

下载kafka,cmak

下载好后,分别解压到/opt目录下

这里如果是单机kafka,可以直接用kafka自带的,启动方式为

1
./$KAFKA_HOME/bin/zookeeper-server-start.sh -daemon config/zookeeper.properties

配置kafka

1
2
3
broker.id=0
log.dirs=/tmp/kafka-logs
zookeeper.connect=localhost:2181

大多数情况下,可能会改动的估计就是上述3个配置,由于kafka需要做持久化消息,故这里有一个log.dirs的配置项

启动kafka

1
$ ./bin/kafka-server-start.sh -daemon config/server.properties

不加-daemon则是前台启动,若是在docker中启动,就不用加-daemon

cmak(kafka manager)

依赖的组件说明

cmak 3.x需要依赖jdk 11+ 且zookeeper需要3.5.x版本,我直接用了es 7.x的自带jdk13

所以修改了cmak启动脚本get_java_cmd函数:

1
2
3
4
5
6
7
8
9
10
11
12
# Detect if we should use JAVA_HOME or just try PATH.
get_java_cmd() {
# High-priority override for Jlink images
#if [[ -n "$bundled_jvm" ]]; then
# echo "$bundled_jvm/bin/java"
#elif [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then
# echo "$JAVA_HOME/bin/java"
#else
# echo "java"
#fi
echo "/opt/elasticsearch-7.6.1/jdk/bin/java"
}

修改配置
修改conf/application.conf

1
2
3
4
kafka-manager.zkhosts="localhost:2181"
kafka-manager.zkhosts=${?ZK_HOSTS}
cmak.zkhosts="localhost:2181"
cmak.zkhosts=${?ZK_HOSTS}

当然也可以不修改application.conf文件,而是提供环境变量ZK_HOSTS

启动cmak

1
./bin/cmak > cmak.out 2>&1 &

添加集群

点添加集群(Add Cluster)
upload successful

填写集群名称与zookeeper地址
upload successful

添加完成查看集群信息,包括zookeepers连接地址,kafka版本,topic数量,broker数量
upload successful

查看broker信息

upload successful

查看、操作topic

之前已经创建过first-topic,topics列表就可以看到了
upload successful

给topic增加分区

upload successful

创建topic
upload successful

PS:更多cmak的功能有待实践

常见错误

1
This application is already running (Or delete /opt/kafka-cmak-3.0.0.4/RUNNING_PID file).

删掉RUNNING_PID重新启动

Kafka入门

创建Topic

1
2
root@ubuntu:/opt/kafka_2.12-2.4.0# ./bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic first-topic
Created topic first-topic.

参考资料

https://segmentfault.com/a/1190000012730949
https://blog.wolfogre.com/posts/kafka-manager-download/