一、Kafka概述
1、定义
Kafka是一个分布式的基于发布/订阅模式的消息队列,主要应用于大数据实时处理领域。
2、基础架构
(1)Producer :消息生产者,就是向kafka broker发消息的客户端;
(2)Consumer :消息消费者,向kafka broker取消息的客户端;
(3)Consumer Group (CG):消费者组,由多个consumer组成。消费者组内每个消费者负责消费不同分区的数据,一个分区只能由一个消费者消费;消费者组之间互不影响。所有的消费者都属于某个消费者组,即消费者组是逻辑上的一个订阅者。
(4)Broker :一台kafka服务器就是一个broker。一个集群由多个broker组成。一个broker可以容纳多个topic。
(5)Topic :可以理解为一个队列,生产者和消费者面向的都是一个topic;
(6)Partition:为了实现扩展性,一个非常大的topic可以分布到多个broker(即服务器)上,一个topic可以分为多个partition,每个partition是一个有序的队列
;
(7)Replica:副本,为保证集群中的某个节点发生故障时,该节点上的partition数据不丢失,且kafka仍然能够继续工作,kafka提供了副本机制,一个topic的每个分区都有若干个副本,一个leader和若干个follower。
(8)leader:每个分区多个副本的“主”,生产者发送数据的对象,以及消费者消费数据的对象都是leader。
(9)follower:每个分区多个副本中的“从”,实时从leader中同步数据,保持和leader数据的同步。leader发生故障时,某个follower会成为新的leader。
针对上面的概念,我们可以这样理解:不同的主题好比不同的高速公路,分区好比某条高速公路上的车道,消息就是车道上运行的车辆。如果车流量大,则扩宽车道,反之,则减少车道。而消费者就好比高速公路上的收费站,开放的收费站越多,则车辆通过速度越快。
关于消费者组(Consumer Group)规定,同一消费者组内不允许多个消费者消费同一分区的消息,而不同的消费者组可以同时消费同一分区的消息。也就是说,分区与同一个消费者组中的消费者的对应关系是多对一而不是一对多。
二、集群安装Kafka
1、下载安装
Kafka依赖Zookeeper集群,搭建Kafka集群之前,需要先搭建好Zookeeper集群,我们在前边已经搭建过Zookeeper集群了。
kafka和Zookeeper版本对应关系:
从Apache官网 下载kafka(地址:http://kafka.apache.org/downl... )的稳定版本,我们根据之Zookeeper版本,下载kafka_2.12-2.5.0.tgz
(由于Kafka使用Scala和Java编写,2.12指Scala版本号,2.5.0指Kafka版本号)
在centos01 节点中,切换到目录 /opt/softwares/
中,并进入到该目录中,先下载,然后解压到目录 /opt/modules/
$ cd /opt/softwares/
$ wget https://archive.apache.org/dist/kafka/2.5.0/kafka_2.12-2.5.0.tgz
$ tar -zxvf kafka_2.12-2.5.0.tgz -C /opt/modules/
2、编写配置文件
切换目录到安装目录下,安装目录名字 kafka_2.12-2.5.0
cd /opt/modules/kafka_2.12-2.5.0
在/opt/modules/kafka_2.12-2.5.0目录下创建logs文件夹
[root@centos01 kafka_2.12-2.5.0]# mkdir logs
[root@centos01 kafka_2.12-2.5.0]# ls -l
总用量 56
drwxr-xr-x. 3 root root 4096 4月 8 2020 bin
drwxr-xr-x. 2 root root 4096 4月 8 2020 config
drwxr-xr-x. 2 root root 8192 7月 29 23:17 libs
-rw-r--r--. 1 root root 32216 4月 8 2020 LICENSE
drwxr-xr-x. 2 root root 6 7月 29 23:49 logs
-rw-r--r--. 1 root root 337 4月 8 2020 NOTICE
drwxr-xr-x. 2 root root 44 4月 8 2020 site-docs
[root@centos01 kafka_2.12-2.5.0]#
修改配置文件 /config/server.properties
修改内容:
#broker的全局唯一编号,不能重复
broker.id=1
#topic在当前broker上的分区个数,默认为1,可以增加分区的数量,但是不能减少分区的数量
num.partitions=2
#Socket监听地址,用于Broker监听生产者和消费者请求,如果没有配置该参数,则默认通过Java的API来获取主机名
listeners=PLAINTEXT://centos01:9092
#kafka运行日志存放的路径
log.dirs=/opt/modules/kafka_2.12-2.5.0/logs
#配置连接Zookeeper集群地址
zookeeper.connect=centos01:2181,centos02:2181,centos03:2181
修改后的配置文件:
[root@centos01 config]# cat server.properties
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# see kafka.server.KafkaConfig for additional details and defaults
############################# Server Basics #############################
# The id of the broker. This must be set to a unique integer for each broker.
broker.id=1
############################# Socket Server Settings #############################
# The address the socket server listens on. It will get the value returned from
# java.net.InetAddress.getCanonicalHostName() if not configured.
# FORMAT:
# listeners = listener_name://host_name:port
# EXAMPLE:
# listeners = PLAINTEXT://your.host.name:9092
listeners=PLAINTEXT://centos01:9092
# Hostname and port the broker will advertise to producers and consumers. If not set,
# it uses the value for "listeners" if configured. Otherwise, it will use the value
# returned from java.net.InetAddress.getCanonicalHostName().
#advertised.listeners=PLAINTEXT://your.host.name:9092
# Maps listener names to security protocols, the default is for them to be the same. See the config documentation for more details
#listener.security.protocol.map=PLAINTEXT:PLAINTEXT,SSL:SSL,SASL_PLAINTEXT:SASL_PLAINTEXT,SASL_SSL:SASL_SSL
# The number of threads that the server uses for receiving requests from the network and sending responses to the network
num.network.threads=3
# The number of threads that the server uses for processing requests, which may include disk I/O
num.io.threads=8
# The send buffer (SO_SNDBUF) used by the socket server
socket.send.buffer.bytes=102400
# The receive buffer (SO_RCVBUF) used by the socket server
socket.receive.buffer.bytes=102400
# The maximum size of a request that the socket server will accept (protection against OOM)
socket.request.max.bytes=104857600
############################# Log Basics #############################
# A comma separated list of directories under which to store log files
log.dirs=/opt/modules/kafka_2.12-2.5.0/logs
# The default number of log partitions per topic. More partitions allow greater
# parallelism for consumption, but this will also result in more files across
# the brokers.
num.partitions=2
# The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.
# This value is recommended to be increased for installations with data dirs located in RAID array.
num.recovery.threads.per.data.dir=1
############################# Internal Topic Settings #############################
# The replication factor for the group metadata internal topics "__consumer_offsets" and "__transaction_state"
# For anything other than development testing, a value greater than 1 is recommended to ensure availability such as 3.
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
############################# Log Flush Policy #############################
# Messages are immediately written to the filesystem but by default we only fsync() to sync
# the OS cache lazily. The following configurations control the flush of data to disk.
# There are a few important trade-offs here:
# 1. Durability: Unflushed data may be lost if you are not using replication.
# 2. Latency: Very large flush intervals may lead to latency spikes when the flush does occur as there will be a lot of data to flush.
# 3. Throughput: The flush is generally the most expensive operation, and a small flush interval may lead to excessive seeks.
# The settings below allow one to configure the flush policy to flush data after a period of time or
# every N messages (or both). This can be done globally and overridden on a per-topic basis.
# The number of messages to accept before forcing a flush of data to disk
#log.flush.interval.messages=10000
# The maximum amount of time a message can sit in a log before we force a flush
#log.flush.interval.ms=1000
############################# Log Retention Policy #############################
# The following configurations control the disposal of log segments. The policy can
# be set to delete segments after a period of time, or after a given size has accumulated.
# A segment will be deleted whenever *either* of these criteria are met. Deletion always happens
# from the end of the log.
# The minimum age of a log file to be eligible for deletion due to age
log.retention.hours=168
# A size-based retention policy for logs. Segments are pruned from the log unless the remaining
# segments drop below log.retention.bytes. Functions independently of log.retention.hours.
#log.retention.bytes=1073741824
# The maximum size of a log segment file. When this size is reached a new log segment will be created.
log.segment.bytes=1073741824
# The interval at which log segments are checked to see if they can be deleted according
# to the retention policies
log.retention.check.interval.ms=300000
############################# Zookeeper #############################
# Zookeeper connection string (see zookeeper docs for details).
# This is a comma separated host:port pairs, each corresponding to a zk
# server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002".
# You can also append an optional chroot string to the urls to specify the
# root directory for all kafka znodes.
zookeeper.connect=centos01:2181,centos02:2181,centos03:2181
# Timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=18000
############################# Group Coordinator Settings #############################
# The following configuration specifies the time, in milliseconds, that the GroupCoordinator will delay the initial consumer rebalance.
# The rebalance will be further delayed by the value of group.initial.rebalance.delay.ms as new members join the group, up to a maximum of max.poll.interval.ms.
# The default value for this is 3 seconds.
# We override this to 0 here as it makes for a better out-of-the-box experience for development and testing.
# However, in production environments the default value of 3 seconds is more suitable as this will help to avoid unnecessary, and potentially expensive, rebalances during application startup.
group.initial.rebalance.delay.ms=0
3、发送安装信息到其他节点
centos01 节点安装完成后,需要复制整个 kafka 安装目录到 centos02、centos03 节点,命令如下:
# centos02 主机用户 hadoop 的密码为 hadoop@123
$ scp -r /opt/modules/kafka_2.12-2.5.0 hadoop@centos02:/opt/modules/
$ scp -r /opt/modules/kafka_2.12-2.5.0 hadoop@centos03:/opt/modules/
4、修改其他节点配置
cd /opt/modules/kafka_2.12-2.5.0/config
vi server.properties
,centos02配置文件修改为:
#broker的全局唯一编号,不能重复,
broker.id=2
#Socket监听地址,用于Broker监听生产者和消费者请求,如果没有配置该参数,则默认通过Java的API来获取主机名
listeners=PLAINTEXT://centos02:9092
centos03配置文件同上。
5、启动Zookeeper集群
分别在三个节点执行以下命令,启动Zookeeper集群(需要进入Zookeeper安装目录)
cd /opt/modules/zookeeper-3.5.9/bin
[root@centos01 bin]# ./zkServer.sh start
6、启动Kafka集群
分别在三个节点上执行以下命令,启动Kafka集群(需要进入Kafka安装目录)
cd /opt/modules/kafka_2.12-2.5.0
bin/kafka-server-start.sh -daemon config/server.properties
关闭
cd /opt/modules/kafka_2.12-2.5.0/bin
./kafka-server-stop.sh
集群启动后,分别在各个节点上执行jps命令,查看启动的java进程
[root@centos01 kafka_2.12-2.5.0]# bin/kafka-server-start.sh -daemon config/server.properties
[root@centos01 kafka_2.12-2.5.0]# jps
7356 QuorumPeerMain
8142 Jps
8111 Kafka
[root@centos01 kafka_2.12-2.5.0]#
可以看到kafka已经启动成功了 ^_^
7、kafka群起脚本(需要修改)
for i in `cat /opt/module/hadoop-2.7.2/etc/hadoop/slaves`
do
echo "========== $i =========="
ssh $i '/opt/module/kafka/bin/kafka-server-start.sh -daemon /opt/module/kafka/config/server.properties'
echo $?
done
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。