一. 搭建Zookeeper

py
1
2
3
$ jps
1699 QuorumPeerMain # zookeeper进程
1702 Jps

二. 搭建单机Kafka

1. 下载Kafka: 参考官网下载地址。

py
1
2
3
4
5
6
# 下载编译版本(2.12代表Scala版本,2.3.0表示Kafka的版本)
$ curl -O http://mirror.bit.edu.cn/apache/kafka/2.3.0/kafka_2.12-2.3.0.tgz 
$ tar -zxvf kafka_2.12-2.3.0.tgz

# 安装到目标目录下
$ mv ./kafka_2.12-2.3.0 /usr/local/kafka

2. 配置环境变量

py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$ vim /etc/profile
#********************Kafka配置*********************
export KAFKA_HOME=/usr/local/kafka
export PATH=$KAFKA_HOME/bin:$PATH
#**************************************************

ZOOKEEPER_HOME=/usr/local/zookeeper

# 刷新使立即生效
$ souce /etc/profile

3. 启动服务

py
1
2
3
4
5
6
# 启动服务
$ kafka-server-start.sh -daemon $KAFKA_HOME/config/server.properties
$ ps -ef|grep kafka

# 停止服务
$ kafka-server-stop.sh ./config/server.properties

4. 测试服务

py
1
2
3
4
5
# 创建topic
$ kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

# 查看topic
$ kafka-topics.sh --list --zookeeper localhost:2181
py
1
2
# 查看Kafka在ZK中的数据
$ zkCli.sh ls /

三. 搭建Kafaka伪集群

服务名称IP地址broker.id
kafka-1127.0.0.1:90920
kafka-2127.0.0.1:90931
kafka-3127.0.0.1:90942

1. 创建集群配置目录

py
1
2
3
4
5
6
7
8
9
$ cd /usr/local/kafka

# 集群工作目录
$ mkdir kfk9092 kfk9093 kfk9094

# 创建config副本
$ cp -rp ./config ./kfk9092
$ cp -rp ./config ./kfk9093
$ cp -rp ./config ./kfk9094

2. 创建集群配置文件,以$KAFKA_HOME/config/server.properties为模板,修改配置文件,如./kfk9092/config/server.properties:

py
1
2
3
4
5
# 配置信息参考
broker.id=0
listeners=PLAINTEXT://:9092
log.dirs=usr/local/kafka/kfk9092
zookeeper.connect=localhost:2181,localhost:2182,localhost:2183
py
1
2
3
4
5
$ sed "s/9092/9093/g" ./kfk9092/config/server.properties > ./kfk9093/config/server.properties
$ sed "s/9092/9094/g" ./kfk9092/config/server.properties > ./kfk9094/config/server.properties

$ sed -i "s/broker.id=0/broker.id=1/g" ./kfk9093/config/server.properties 
$ sed -i "s/broker.id=0/broker.id=2/g" ./kfk9094/config/server.properties

3. 分别启动集群服务

py
1
2
3
4
5
6
7
$ sudo kafka-server-start.sh -daemon $KAFKA_HOME/kfk9092/config/server.properties
$ sudo kafka-server-start.sh -daemon $KAFKA_HOME/kfk9093/config/server.properties
$ sudo kafka-server-start.sh -daemon $KAFKA_HOME/kfk9094/config/server.properties

$ sudo kafka-server-stop.sh $KAFKA_HOME /kfk9092/config/server.properties
$ sudo kafka-server-stop.sh $KAFKA_HOME /kfk9093/config/server.properties
$ sudo kafka-server-stop.sh $KAFKA_HOME /kfk9094/config/server.properties

6. 测试

py
1
2
3
4
bin/kafka-topics.sh --describe --zookeeper localhost:2181 --topic my-replicated-topic
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning

四. Kafka服务配置参考

  • 参考:$KAFKA_HOME/config/server.properties
toml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
############################# Server Basics #############################

broker.id=0

listeners=PLAINTEXT://:9092

num.network.threads=3

num.io.threads=8

socket.send.buffer.bytes=102400

socket.receive.buffer.bytes=102400

socket.request.max.bytes=104857600
############################# Log Basics #############################

log.dirs=/tmp/kafka-logs

num.partitions=1

num.recovery.threads.per.data.dir=1
############################# Internal Topic Settings  #############################
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
############################# Log Flush Policy #############################
#log.flush.interval.messages=10000
#log.flush.interval.ms=1000
############################# Log Retention Policy #############################
log.retention.hours=168

#log.retention.bytes=1073741824

log.segment.bytes=1073741824

log.retention.check.interval.ms=300000
############################# Zookeeper #############################
zookeeper.connect=localhost:2181

zookeeper.connection.timeout.ms=6000
############################# Group Coordinator Settings #############################
group.initial.rebalance.delay.ms=0

参考链接

http://kafka.apache.org/documentation.html#quickstart
https://blog.csdn.net/u012965203/article/details/92634343
https://www.cnblogs.com/mythdoraemon/p/10171883.html
更换kafka在ZK中的路径
https://www.cnblogs.com/mediocreWorld/p/11311998.html


「真诚赞赏,手留余香」

Hollson码夫庄园

嗯,我的梦想是被读者的稿费包养~

使用微信扫描二维码完成支付

Hollson

评论