CentOS7安装kafka

安装准备

基于Centos7.5 1804 minimal

安装JDK1.8

1
yum -y install java-1.8.0-openjdk*

安装kafka

下载kafka

1
wget http://mirrors.tuna.tsinghua.edu.cn/apache/kafka/2.3.0/kafka_2.12-2.3.0.tgz

解压

1
tar -zxvf kafka_2.12-2.3.0.tgz

修改配置,使kafka远程访问

1
2
3
4
5
vi config/server.properties

# listeners=PLAINTEXT://:9092
->
listeners=PLAINTEXT://KAFKA_IP:9092

启动kafka

进入kafka目录

cd kafka_2.12-2.3.0

启动zookeeper

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

检查zookeeper端口2181是否正常监听

1
2
netstat -an|grep 2181
tcp6 0 0 :::2181 :::* LISTEN

检查kafka默认的JVM参数配置是否需要修改

1
Kafka默认设置1G,即”-Xmx1G -Xms1G”。

如果你的测试机内存较低,需要修改才能成功启动。
参数配置位于:bin/kafka-server-start.sh

启动Kafka

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

如果一切顺利,就会看到如下启动成功的日志:

1
2
3
4
5
[2019-11-18 21:42:37,067] INFO [KafkaServer id=0] started (kafka.server.KafkaServer)
检查Kafka的端口9092监听是否正常

[root@localhost kafka_2.12-2.3.0]# netstat -an|grep 9092
tcp6 0 0 :::9092 :::* LISTEN

测试kafka

创建一个测试主题

1
2
3
4
cd kafka_2.12-2.3.0
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

Created topic "test".

查看刚刚创建的test主题

1
2
3
bin/kafka-topics.sh --list --zookeeper localhost:2181

test

向刚刚创建的test主题中写入数据

1
2
3
4
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
>Hello, World!
>
“Hello World!”为写入的数据。

查看刚刚写入的数据
另外开一个ssh tab连接,然后执行如下命令

1
2
3
4
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic 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].

Hello, World!

CentOS7安装kafka
http://maitianxin.github.io/2019/11/19/linux/kafka/
作者
Matianxin
发布于
2019年11月19日
许可协议