2

One, Kafka overview

1. Definition

message queue based on the publish/subscribe model, mainly used in the big data real-time processing field .

2. Infrastructure

file

(1) Producer: The message producer is the client that sends messages to the Kafka broker;
(2) Consumer: message consumer, the client that fetches messages from kafka broker;
(3) Consumer Group (CG): Consumer group, composed of multiple consumers. Each consumer in a consumer group is responsible for consuming data in different partitions, and a partition can only be consumed by one consumer; the consumer groups do not affect each other. All consumers belong to a certain consumer group, that is, the consumer group is a logical subscriber.
(4) Broker: A kafka server is a broker. A cluster is composed of multiple brokers. A broker can hold multiple topics.
(5) Topic: can be understood as a queue, producers and consumers are facing a topic ;
(6) Partition: In order to achieve scalability, a very large topic can be distributed to multiple brokers (ie servers), a topic can be divided into multiple partitions , each partition is an ordered queue;
(7) Replica: Replica, in order to ensure that when a node in the cluster fails, the partition data on the node is not lost, and Kafka can still continue to work, Kafka provides a copy mechanism, and each partition of a topic has several A copy, a leader and several followers.
(8) Leader: The "master" of multiple copies of each partition, the object to which the producer sends data, and the object to which the consumer consumes data are all leaders.
(9) Follower: The "slave" in multiple copies of each partition synchronizes data from the leader in real time and maintains synchronization with the leader's data. When the leader fails, a follower will become the new leader.

For the above concepts, we can understand it like this: Different themes are like different highways, and partitions are like lanes on a certain highway, and the message is the vehicles running on the lane. If the traffic volume is large, widen the lane, otherwise, reduce the lane. Consumers are like toll stations on a highway. The more toll stations that are open, the faster the vehicles will pass.

Regarding Consumer Group (Consumer Group) regulations, multiple consumers in the same consumer group are not allowed to consume messages from the same partition, and different consumer groups can consume messages from the same partition at the same time. In other words, the correspondence between partitions and consumers in the same consumer group is many-to-one rather than one-to-many.

file

Two, cluster installation Kafka

1. Download and install

Kafka relies on the Zookeeper cluster. Before building the Kafka cluster, you need to build the Zookeeper cluster first. We have already built the Zookeeper cluster before.

The correspondence between kafka and Zookeeper versions:
file

http://kafka.apache.org/downloads ) from the Apache official website kafka_2.12-2.5.0.tgz according to the Zookeeper version (Since Kafka is written in Scala and Java, 2.12 refers to the Scala version number, 2.5 .0 refers to the Kafka version number)

file

In the centos01 node, switch to the directory /opt/softwares/ , and enter the directory, download first, and then unzip to the directory /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. Write a configuration file

Change the directory to the installation directory, the installation directory name is kafka_2.12-2.5.0

cd /opt/modules/kafka_2.12-2.5.0

Create the logs folder in the /opt/modules/kafka_2.12-2.5.0 directory

[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]# 

Modify configuration file /config/server.properties

Modified content:

#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

Modified configuration file:

[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. Send installation information to other nodes

After the centos01 node is installed, you need to copy the entire kafka installation directory to the centos02 and centos03 nodes. The command is as follows:

# 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. Modify other node configuration

cd /opt/modules/kafka_2.12-2.5.0/config

vi server.properties , the centos02 configuration file is modified to:

#broker的全局唯一编号,不能重复,
broker.id=2
#Socket监听地址,用于Broker监听生产者和消费者请求,如果没有配置该参数,则默认通过Java的API来获取主机名
listeners=PLAINTEXT://centos02:9092

The centos03 configuration file is the same as above.

5. Start the Zookeeper cluster

Execute the following commands on the three nodes to start the Zookeeper cluster (you need to enter the Zookeeper installation directory)

cd /opt/modules/zookeeper-3.5.9/bin
[root@centos01 bin]# ./zkServer.sh start

6. Start Kafka cluster

Execute the following commands on the three nodes to start the Kafka cluster (you need to enter the Kafka installation directory)

cd /opt/modules/kafka_2.12-2.5.0
bin/kafka-server-start.sh -daemon config/server.properties

closure

cd /opt/modules/kafka_2.12-2.5.0/bin
./kafka-server-stop.sh

After the cluster is started, execute the jps command on each node to view the started java process

[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]# 

You can see that Kafka has been successfully started^_^

7. Kafka group up script (need to modify)

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

Corwien
6.3k 声望1.6k 粉丝

为者常成,行者常至。