Kafka安装

安装kafka需要安装zookeeper,zookeeper安装过程见 https://www.cnblogs.com/chentop/p/10303465.html

1、下载Kafka,直接去官网下载 http://kafka.apache.org/downloads.html

官网的安装包提供了源码包和二进制包,这里选择下载二进制的安装包版本选择最2.12版本

2、上传到要安装的目录,在这里我自己选择上传到目录/opt/kafka/ 目录下,并解压

tar -zxvf kafka_2.12-2.1.0.tgz

3、切换到解压后的目录,并创建logs目录

cd kafka_2.12-2.1.0
mkdir logs

4、进入config目录,修改配置文件 server.properties

cd config/
vim server.properties

5、修改server.properties内容如下:

#broker的全局唯一编号,不能重复
broker.id=0
#是否允许删除topic
delete.topic.enable=true
#处理网络请求的线程数量
num.network.threads=3
#用来处理磁盘IO的线程数量
num.io.threads=8
#发送套接字的缓冲区大小
socket.send.buffer.bytes=102400
#接收套接字的缓冲区大小
socket.receive.buffer.bytes=102400
#请求套接字的最大缓冲区大小
socket.request.max.bytes=104857600
#kafka运行日志存放的路径
log.dirs=/opt/module/kafka/logs
#topic在当前broker上的分区个数
num.partitions=1
#用来恢复和清理data下数据的线程数量
num.recovery.threads.per.data.dir=1
#segment文件保留的最长时间,超时将被删除
log.retention.hours=168
#配置连接Zookeeper集群地址
zookeeper.connect=192.168.0.101:2181,192.168.0.102:2181,192.168.0.103:2181

6、启动服务,可通过加参数 “-daemon” 以后台方式启动 

bin/kafka-server-start.sh config/server.properties     //前台方式启动,客户端关闭会导致服务关闭
bin/kafka-server-start.sh -daemon config/server.properties   //后台守护进程方式启动

添加多个broker集群

上述只是添加了单个broker节点,即单节点集群,实际生产上需要多集群模式,

7、新加两个broker节点,为每个broker复制一份配置文件,如果要实现跨机器部署broker节点,可使用scp命令复制

cp config/server.properties config/server-1.properties 
cp config/server.properties config/server-2.properties

 8、修改新加broker的配置文件分别为:

config/server-1.properties: 
    broker.id=1 
    listeners=PLAINTEXT://:9093 
    log.dir=/tmp/kafka-logs-1
------------------------------
config/server-2.properties: 
    broker.id=2 
    listeners=PLAINTEXT://:9094 
    log.dir=/tmp/kafka-logs-2

 broker.id为节点的唯一标识,不可重复

9、然后启动新加的节点

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

这样就完成了一个有3个节点的集群搭建

Kafka命令行操作(以单节点演示)

查看当前服务器中的所有topic

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

创建topic

bin/kafka-topics.sh --zookeeper localhost:2181 --create --replication-factor 3 --partitions 1 --topic first

  --topic:定义topic名

  --replication-factor: 定义副本数

  --partitions :定义分区数 

删除topic

bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic first

   注意:需要server.properties中设置delete.topic.enable=true才会真正删除,否则只是标记删除。

发送消息:

bin/kafka-console-producer.sh --broker-list localhost:9092 --topic first

如果想测试执行效果,可打开两个终端,分别运行发送消息命令、消费消息的命令,消费命令会打印出生产的数据。

消费消息:

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic first

如果需要从头开始接收数据,需要添加--from-beginning参数

查看某个Topic的详情:

bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic first

 查看消费者组列表:

bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --list

注意:kafka老版本的消费者组信息保存在zookeeper中,需要指定--zookeepe参数,新版本的组信息保存在broker中,指定参数 --bootstrap-server,

旧版本查询消费者组可使用命令: bin/kafka-consumer-groups.sh --zookeeper localhost:2181 --list  


 

 

原文地址:https://www.cnblogs.com/chentop/p/10328007.html