乐趣区

关于kafka:大数据之-Hadoop10Kafka

一、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
退出移动版