一、kafka安裝:
1.下載kafka安裝包
下載地址:http://archive.apache.org/dist/kafka/0.10.2.0/
2、解壓:
tar -zxf kafka_2.11-0.10.2.0.tgz
ln -s kafka_2.11-0.10.2.0 kafka
3.配置環境變量
vim ~/.bashrc
添加如下語句:
文件最後
export KAFKA_HOME=~/soft/kafka
export PATH=$PATH:$KAFKA_HOME/bin
4、配置文件:
vim config/server.properties
內容:
listeners = PLAINTEXT://0.0.0.0:9092
advertised.listeners=PLAINTEXT://hostname:9092
zookeeper.connect=hostname:2181
vim hosts
內容:
xx.xx.xx.xx hostname
二、kafka啓動
1、啓動 zookeeper
nohup bin/zookeeper-server-start.sh config/zookeeper.properties > zookeeper.log 2>&1 &
2、啓動 kafka
nohup bin/kafka-server-start.sh config/server.properties > kafka.log 2>&1 &
三、kafka簡單使用
新建topic:
bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test1
查看tpic:
bin/kafka-topics.sh --list --zookeeper localhost:2181
刪除tpic:
bin/kafka-topics.sh --delete --zookeeper localhost:2181 --topic playmp3
生產者:
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test3
消費者:
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test3 --from-beginning
四、python 生產者消費者
消費者:
from kafka import KafkaConsumer
consumer = KafkaConsumer('test', bootstrap_servers= ['localhost:9092'])
for msg in consumer:
print(msg)
生產者:
from kafka import KafkaProducer
producer = KafkaProducer(bootstrap_servers=['localhost:9092'])
for i in range(3):
msg = "msg%d " % i
producer.send('test', msg)
producer.close()
五、報錯解決
報錯:內存不足
(1)、cannot allocate memory
解決辦法:
將 kafka-server-start.sh的
export KAFKA_HEAP_OPTS="-Xmx1G -Xms1G"
修改爲
export KAFKA_HEAP_OPTS="-Xmx256M -Xms128M"
因爲我的內存是1G
(2)、端口占用
netstat -tunlp |grep :2181
netstat -tunlp |grep :9092
sudo kill -9 pid
(3)、重複啓動
rm /tmp/kafka-logs/.lock