Cent OS7 安装Kafka 2.5.0-亲测可用

一、环境配置

  • Cent OS 7
  • Java 1.8+
  • Zookeeper 3.6.1

二、安装过程

2.1 下载 (如果太慢,可换yum下载,yum配置阿里源试试)

[root@VM_0_13_centos local]# wget https://downloads.apache.org/kafka/2.5.0/kafka_2.12-2.5.0.tgz

2.2解压

> tar -xzf kafka_2.12-2.5.0.tgz
> cd kafka_2.12-2.5.0

2.3 创建日志文件夹

[root@VM_0_13_centos kafka_2.12-2.5.0]# pwd
/usr/local/kafka_2.12-2.5.0
[root@VM_0_13_centos kafka_2.12-2.5.0]# mkdir logs

2.4 修改配置文件

如果zookeeper和kafka不在同一个机器,或者要集群化部署或者要修改端口号以及其他一些配置,可以先修改kafka配置文件
路径:

kafka/config/server.properties

# The id of the broker. This must be set to a unique integer for each broker.
#broker的全局唯一编号,不能重复 
21 broker.id=0
# 删除topic功能
delete.topic.enable=true (我这个版本似乎没有这个配置了)
# kafka 日志存放的路径

############################# Log Basics #############################
# A comma separated list of directories under which to store log files
log.dirs=/tmp/kafka-log
#配置集群配置 
############################# Zookeeper #############################
# Zookeeper connection string (see zookeeper docs for details).
# This is a comma separated host:port pairs, each corresponding to a zk
# server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".
# You can also append an optional chroot string to the urls to specify the root directory for all kafka znodes.
zookeeper.connect=localhost:2181

2.5 启动zk

因为kafka是依赖zk的,因此需要先启动zk,因为我本机之前已经搭过zk了,这儿也就不过多介绍了,没搭的可以翻看之前的文章

zk启动
> bin/zookeeper-server-start.sh config/zookeeper.properties
[2013-04-22 15:01:37,495] INFO Reading configuration from: config/zookeeper.properties (org.apache.zookeeper.server.quorum.QuorumPeerConfig)
...

2.6配置环境变量

[root@VM_0_13_centos config]# vi /etc/profile
# KAFKA_HOME
export KAFKA_HOME=/usr/local/kafka_2.12-2.5.0
export PATH=$PATH:$KAFKA_HOME/bin

环境变量生效

[root@VM_0_13_centos config]# source /etc/profile

2.7 后台启动 kafka Server

[root@VM_0_13_centos bin]# kafka-server-start.sh -daemon /usr/local/kafka_2.12-2.5.0/config/server.properties 
...

2.8启动错误

[root@VM_0_13_centos logs]# cat kafkaServer.out 
Java HotSpot(TM) Server VM warning: INFO: os::commit_memory(0xa6c00000, 1073741824, 0) failed; error='Cannot allocate memory' (errno=12)

因为我用的腾讯云服务起,磁盘只有500MB,但是kafka server启动脚本默认申请的内存是 1G,所以导致启动时内存申请失败

解决:
修改server启动脚本的堆内存大小。

将 kafka-server-start.sh的
export KAFKA_HEAP_OPTS="-Xmx1G -Xms1G"
修改为
export KAFKA_HEAP_OPTS="-Xmx256M -Xms128M"

2.9启动检查

[root@VM_0_13_centos bin]# jps
12944 Jps
4437 QuorumPeerMain
32534 jar
12888 Kafka    有kafka就说明启动了

三、kafka使用

3.1 创建前有没有topic

[root@VM_0_13_centos bin]# ./kafka-topics.sh --list --bootstrap-server localhost:9092

3.2 创建一个单独分区和单独副本的 名称为first的topic

–topic 定义topic名称
–replication-factor 定义副本数
–partitions 定义分区数

[root@VM_0_13_centos bin]# ./kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 2 --topic first
Created topic first.

3.3 查看topic列表

[root@VM_0_13_centos bin]# ./kafka-topics.sh --list --bootstrap-server localhost:9092
first

3.4 查看topic详情

[root@VM_0_13_centos bin]# ./kafka-topics.sh --describe --bootstrap-server localhost:9092 --topic first
Topic: first	PartitionCount: 2	ReplicationFactor: 1	Configs: segment.bytes=1073741824
	Topic: first	Partition: 0	Leader: 0	Replicas: 0	Isr: 0
	Topic: first	Partition: 1	Leader: 0	Replicas: 0	Isr: 0

3.5 删除topic

老版本需要在server.properties中设置delete.topic.enable=true以开启topic删除,否则只是标记删除

[root@VM_0_13_centos bin]# ./kafka-topics.sh --list --bootstrap-server localhost:9092
__consumer_offsets
first
[root@VM_0_13_centos bin]# ./kafka-topics.sh --delete --bootstrap-server localhost:9092 --topic first
[root@VM_0_13_centos bin]# ./kafka-topics.sh --list --bootstrap-server localhost:9092
__consumer_offsets

3.2 生产者消费者消息操作

3.2.1 生产者发送一条消息

运行生产者,然后在控制台中键入一些消息以发送到服务器。

[root@VM_0_13_centos bin]# ./kafka-console-producer.sh --bootstrap-server localhost:9092 --topic first
### 消息内容
This is a 1 message
exit

3.2.2 开启一个消费者,消费消息

启动消费者,消费消息,如果启动多个消费者,都是可以同步接受到广播消息的

–from-beginning:会把主题中以往所有的数据都读取出来

[root@VM_0_13_centos bin]# ./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic first --from-beginning
### 消息内容
This is a 1 message
exit
hello
test2

注:

–bootstrap-server 和–broker-list 以及 --zookeeper
个人理解的 --bootstrap-server 和–broker-list 都是一样的
而–zookeeper 是老版本(0.8)的kafka需要的参数,因为老版本的消费者进度offset是存储在zk上的,而后来的版本都统一由broker管理,所以就用bootstrap-server了。

参考:

  • 官网: https://kafka.apache.org/quickstart
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值