kafka集群搭建

 1、简介

    Kafka is a distributed,partitioned,replicated commit logservice。它提供了类似于JMS的特性,但是在设计实现上完全不同,此外它并不是JMS规范的实现。kafka对消息保存时根据Topic进行归类,发送消息者成为Producer,消息接受者成为Consumer,此外kafka集群有多个kafka实例组成,每个实例(server)成为broker。无论是kafka集群,还是producer和consumer都依赖于zookeeper来保证系统可用性集群保存一些meta信息。

1. 服务器基本信息

kafka需要配合zookeeper使用,在安装kafka之前,需要先安装zookeeper集群,关于安装zookeeper集群,可以参考:https://www.cnblogs.com/zhangan/p/10978012.html

ip地址安装服务
192.168.1.11 zookeeper-3.4.10、kafka2.10、kafka-manager
192.168.1.12 zookeeper-3.4.10、kafka2.10
192.168.1.13 zookeeper-3.4.10、kafka2.10

 

2. 安装jdk(三台主机上执行)

3. 安装kafka(三台主机上执行)

3.1 安装kafka

下载地址: https://www.apache.org/dyn/closer.cgi?path=/kafka/2.1.1/kafka_2.11-2.1.1.tgz

cd /usr/local/src
tar zxvf kafka_2.11-2.1.1.tgz
mv kafka_2.11-2.1.1 /usr/local/kafka

3.2 修改配置文件

vim /usr/local/kafka/config/server.properties

具体参数如下:

# The id of the broker. This must be set to a unique integer for each broker.
broker.id=2
host.name=kafka2.teleracing.lan
############################# Socket Server Settings #############################
#listeners=PLAINTEXT://:9092
#advertised.listeners=PLAINTEXT://your.host.name: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=/opt/app/data/kafka/kafka-logs
num.partitions=24
num.recovery.threads.per.data.dir=1
############################# Internal Topic Settings  #############################
offsets.topic.replication.factor=3
transaction.state.log.replication.factor=3
transaction.state.log.min.isr=1
############################# Log Retention Policy #############################
log.retention.hours=72
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
group.initial.rebalance.delay.ms=3000
#broker能接收的单条最大消息的大小,默认1M message.max.bytes=15728640 replica.fetch.max.bytes=15728640
# 是否开启删除topic delete.topic.enable=true #是否开启自动创建topic auto.create.topics.enable=true
############################# Zookeeper ############################# zookeeper.connect=registry1.teleracing.lan:2181,registry2.teleracing.lan:2181,registry3.teleracing.lan:2181 zookeeper.connection.timeout.ms=18000

 

集群配置更改broker.id文件如下: 
1. 改server.prorperties文件配置; 
2. 改meta.properties,默认情况下,应该在/tmp/kafka-logs目录下; 同时需注意数据存在多个目录时,需要修改多个目录的meta.propertie。

 

3.3 启动kafka(三台)

cd /usr/local/kafka/kafka_2.11-2.1.1/bin
./kafka-server-start.sh -daemon ../config/server.properties &

3.4 创建topic

./kafka-topics.sh --create --zookeeper 192.168.1.11:2181,192.168.1.12:2181,192.168.1.13:2181 --replication-factor 2 --partitions 1 --topic tttt

新版本kafka:

./kafka-topics.sh --create --bootstrap-server 172.16.11.239:9092 --replication-factor 1 --partitions 1 --topic CEP

参数解释:

  • 复制两份

  --replication-factor 2

  • 创建1个分区

  --partitions 1

  • topic 名称

  --topic tttt

3.5 查看已经存在的topic

./kafka-topics.sh --list --zookeeper 192.168.1.11:2181,192.168.1.12:2181,192.168.1.13:2181

3.6 删除topic

删除topic,需要在server.properties中设置delete.topic.enable=true否则只是标记删除或者直接重启。

./bin/kafka-topics.sh --delete --zookeeper 192.168.1.11:2181,192.168.1.12:2181,192.168.1.13:2181 --topic tttt

3.7 消费topic

kafka-console-consumer.sh --bootstrap-server 172.31.15.70:9092 --topic NotifyAPP --from-beginning --zookeeper 172.31.15.70:2181

3.8 可以针对某一个topic设置数据的过期时间,可以不需要重启kafka

kafka-configs.sh -zookeeper localhost:2181 -entity-type topics -entity-name elk-pipe-access -alter -add-config retention.ms=3600000

3.9 查看设置过期时间

kafka-configs.sh --zookeeper localhost:2181 --describe --entity-name elk-pipe-access --entity-type topics

3.10 数据没有立即删除,执行下面

kafka-topics.sh --zookeeper localhost:2181 --alter --topic elk-pipe-access --config cleanup.policy=delete

4.1 查询

  • 查询集群描述

  bin/kafka-topics.sh --describe --zookeeper

  • 消费者列表查询

  bin/kafka-topics.sh --zookeeper 127.0.0.1:2181 --lis

  • 新消费者列表查询(支持0.9版本+)

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

  • 显示某个消费组的消费详情(仅支持offset存储在zookeeper上的)

  bin/kafka-run-class.sh kafka.tools.ConsumerOffsetChecker --zookeeper localhost:2181 --group test

  • 显示某个消费组的消费详情(支持0.9版本+)

  bin/kafka-consumer-groups.sh --new-consumer --bootstrap-server localhost:9092 --describe --group test-consumer-group

 

posted on 2019-06-06 17:45  走路带风的帅界扛把子  阅读(7941)  评论(0编辑  收藏  举报