Kafka安装
Kafka安装需要有Java和zookeeper环境,如何配置可参考本站文章《Java_sdk安装》和《zookeeper安装》。
1.本地安装
1.1.解压安装
本地安装,适用于本地开发调试使用。自动化安装脚本如下:
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
| #!/bin/bash # kafka下载地址 kafka_addr=""
# kafka的安装路径 kafka_home="/test/kafka"
# 创建安装目录 mkdir -p $kafka_home if [ "$?" != "0" ]; then echo 'create file failed!' exit 1 fi
# 下载安装包 wget $kafka_addr if [ "$?" != "0" ]; then echo 'download failed!' exit 1 fi
# 解压安装包 name=$(ls | grep zookeeper) tar -zxvf $name -C $kafka_home if [ "$?" != "0" ]; then echo 'decompression file failed!' exit 1 fi
echo 'install successful!'
|
1.2.调整配置
进入conf目录下确认server.properties配置是否符合预期,常见的配置项如下:
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
broker.id=0
host.name=10.0.0.1
port =9092
num.network.threads=4
num.io.threads=8
socket.send.buffer.bytes=1048576
socket.receive.buffer.bytes=1048576
socket.request.max.bytes=104857600
num.partitions=2
log.retention.hours=168
log.segment.bytes=536870912
log.retention.check.interval.ms=60000
log.cleaner.enable=false
zookeeper.connect=10.0.0.1:2181
zookeeper.connection.timeout.ms=60000
zookeeper.session.timeout.ms=6000
zookeeper.sync.time.ms =2000
log.dirs=/tmp/kafka.log
|
1.3.启停服务
进入bin目录
1 2 3 4 5
| ./kafka-server-start.sh -daemon ../config/server.properties
./kafka-server-stop.sh
|
1.4.验证功能
1 2 3 4 5 6 7 8 9 10 11
| ./kafka-topics.sh --create --partitions 1 --replication-factor 1 --topic test --bootstrap-server localhost:9092
./kafka-topics.sh --list --bootstrap-server localhost:9092
./kafka-console-producer.sh --topic test --bootstrap-server localhost:9092
./kafka-console-consumer.sh --topic test --from-beginning --bootstrap-server localhost:9092
|