Kafka架构

producer: 生产者
consumer: 消费者
broker: 类似容器,存放数据
topic: 类似标签

多节点多Broker部署和使用

0. 服务器地址  先安装zookeeper集群 https://www.cnblogs.com/yoyo1216/p/12700989.html

192.168.104.94
192.168.104.96
192.168.104.97

1.创建文件夹

mkdir -p /usr/local/kafka/data  /usr/local/kafka/logs
cd /usr/local/kafka

2.下载Kafka http://kafka.apache.org/downloads

wget http://apache.mirror.iphh.net/kafka/2.5.0/kafka_2.13-2.5.0.tgz

3.解压

tar -zvxf kafka_2.13-2.5.0.tgz

4.修改配置文件

vim /etc/profile
export KAFKA_HOME=/usr/local/kafka/kafka_2.13-2.5.0
export PATH=$KAFKA_HOME/bin:$PATH

source /etc/profile

5.修改配置

cd /usr/local/kafka/kafka_2.13-2.5.0/config
vim server.properties
broker.id=0   # broker id 这个不能重复  集群这个推荐注释掉
listeners=PLAINTEXT://192.168.104.91:9092   # 取消注释  默认端口是9092
advertised.listeners=PLAINTEXT://127.25.17.156:9092
host.name=192.168.104.91   # ip
log.dirs=/usr/local/kafka/logs  # log文件放置路径
zookeeper.connect=192.168.104.94:2181,192.168.104.96:2181,192.168.104.97:2181   # zookeeper连接信息

delete.topic.enable=true   # 新增
advertised.host.name=192.168.104.91   # 新增  不同服务器要改ip

6.启动kafka

kafka-server-start.sh -daemon $KAFKA_HOME/config/server.properties
kafka-server-start.sh $KAFKA_HOME/config/server.properties
-daemon: 后台启动
config/server.properties: server.properties文件路径
kafka-server-stop.sh  # 关闭kafka

jps  查看进程  jps -m: 查看比较详细的信息
kafka

启动报错: kafka.common.InconsistentClusterIdException: The Cluster ID BmhgroWiQEWltKkCY_4u4Q doesn't match stored clusterId Some(3BVX0S4nTjSvbCvqUU3WGQ) in meta.properties. The broker is trying to join the wrong cluster. Configured zookeeper.connect may be wrong.
    在Kafka config目录中,打开kafka config属性文件,让server.properties查找具有参数log.dirs =的日志路径目录,然后转到日志路径目录并在其中找到文件meta.properties。打开文件meta.properties并更新cluster.id =或从日志路径目录中删除此文件或所有日志文件,然后重新启动kafka。(这个方法并没有解决我的问题)

kafka-server-stop.sh $KAFKA_HOME/config/server.properties  # 关闭kafka

cd /usr/local/kafka/kafka_2.13-2.5.0/logs
cat server.log | tail -n 200  # 查看kafka启动时产生日志

grep  -A 1 -B 1 "INFO KafkaConfig values:" server.log.*   # 查看是否重启过

7.创建topic

kafka-topics.sh --zookeeper 192.168.104.94:2181,192.168.104.96:2181,192.168.104.97:2181 --create --topic test-topic --partitions 3 --replication-factor 3  # 创建
    kafka-topics.sh --zookeeper 192.168.104.94:2181,192.168.104.96:2181,192.168.104.97:2181 -list  # 查看所有topic信息
    kafka-topics.sh --zookeeper 192.168.104.94:2181,192.168.104.96:2181,192.168.104.97:2181 --describe --topic test-topic  # 查看指定的topic
    kafka-topics.sh --delete --zookeeper 192.168.104.94:2181,192.168.104.96:2181,192.168.104.97:2181 --topic test-topic  # 删除topic
    kafka-console-consumer.sh --bootstrap-server 192.168.104.94:9092,192.168.104.96:9092,192.168.104.97:9092 --topic test-topic  # 发送消息
    kafka-console-consumer.sh --bootstrap-server 192.168.104.94:9092,192.168.104.96:9092,192.168.104.97:9092 --topic test-topic --from-beginning  # 消费消息