kafka集群搭建

集群搭建准备

kafka集群搭建需要zookeeper集群,zookeeper集群搭建有两种方法:

1、使用kafka有自带的zookeeper,这种好像都不怎么推荐;

2、使用自己的zookeeper搭建集群,搭建方法:zookeeper集群搭建

kafka集群搭建

注:1、zookeeper的版本:zookeeper-3.5.8;

       2、三个节点ip和port:192.168.205.128:2181,192.168.205.128:2182,192.168.205.128:2183;

       3、myid文件内容分别为:1,2,3;

       4、kafka官方下载地址:http://kafka.apache.org/downloads

       5、本次使用的是kafka_2.13-2.6.0版本。

搭建步骤

1、下载,将压缩文件下载到/usr/local/tmp下:

下载地址:https://www.apache.org/dyn/closer.cgi?path=/kafka/2.6.0/kafka_2.13-2.6.0.tgz,或直接使用 wget 下载镜像:

cd /usr/local/tmp
wget https://mirror.bit.edu.cn/apache/kafka/2.6.0/kafka_2.13-2.6.0.tgz

2、解压,并将解压后文件夹移动到/usr/local/kafka-cluster下,重名名为kafka01,为搭建集群做准备,如果没有kafka-cluster路径,事先建好:

tar -zxvf kafka_2.13-2.6.0.tgz
mv kafka_2.13-2.6.0 /usr/local/kafka-cluster/kafka01

3、进入kafka01,创建log目录,用来存放日志文件

cd /usr/local/kafka-cluster/kafka01
mkdir log

4、修改server.properties配置文件

vim config/server.properties

修改内容为:

# 每个server的broker.id不能一样
broker.id=1
# 日志文件存储路径
log.dirs=/usr/local/kafka-cluster/kafka01/log

listeners=PLAINTEXT://192.168.205.128:9091
 
# 在log.retention.hours=168 下面新增下面三项
message.max.byte=5242880
default.replication.factor=2
replica.fetch.max.bytes=5242880
 
# 设置zookeeper的连接端口
zookeeper.connect=192.168.205.128:2181,192.168.205.128:2182,192.168.205.128:2183

5、回到kafka-cluster路径下,复制两份kafka01为kafka02,kafka03:

cp -rf kafka01 kafka02
cp -rf kafka01 kafka03

6、修改kafka02和kafka03的配置文件:

      1、broker.id分别为:2,3;

      2、log.dirs分别为:/usr/local/kafka-cluster/kafka02/log,/usr/local/kafka-cluster/kafka03/log。

      3、listeners分别为:listeners=PLAINTEXT://192.168.205.128:9092,listeners=PLAINTEXT://192.168.205.128:9093

启动、测试

1、编写启动脚本,将脚本放在kafka-cluster路径下:

cd /usr/local/kafka-cluster
vim kafka_startAll.sh

内容为:

./kafka01/bin/kafka-server-start.sh -daemon kafka01/config/server.properties
./kafka02/bin/kafka-server-start.sh -daemon kafka02/config/server.properties
./kafka02/bin/kafka-server-start.sh -daemon kafka03/config/server.properties

给脚本授权:

chmod u+x kafka_startAll.sh

2、启动,测试是否启动,使用jps测试,结果如下:

3091 QuorumPeerMain
3123 QuorumPeerMain
3155 QuorumPeerMain
16200 Jps
15644 Kafka
15293 Kafka
15645 Kafka

3、kafka启动后,创建topic验证是否创建成功:

./kafka01/bin/kafka-topics.sh --create --zookeeper 192.168.205.128:2181 --replication-factor 2 --partitions 1 --topic test
# 打印结果
Created topic test.

4、测试topic是否创建成功:

./kafka01/bin/kafka-topics.sh --list --zookeeper 192.168.205.128:2181
# 打印结果
test

5、在一个xshell窗口中创建发布者:

./kafka01/bin/kafka-console-producer.sh --broker-list 192.168.205.128:9092 --topic test

6、在另一个xshell窗口中创建消费者:

./kafka02/bin/kafka-console-consumer.sh --bootstrap-server 192.168.205.128:9091 --topic test --from-beginning

测试结果:

发布端:

[root@localhost kafka-cluster]# ./kafka01/bin/kafka-conso.sh --broker-list 192.168.205.128:9092 --topic test
>测试
>正常

消费者:

[root@localhost kafka-cluster]# ./kafka02/bin/kafka-console-consumer.sh --bootstrap-server 192.168.205.128:9091 --topic test --from-beginning
测试
正常

kafka配置文件server.properties解析

############################# Server Basics #############################

# The id of the broker. This must be set to a unique integer for each broker.
#当前机器在集群中的唯一标识,和zookeeper的myid性质一样
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
# kafka的连接协议名、主机名和端口
listeners=PLAINTEXT://192.168.205.128:9091


# 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().
# 生产者和消费者使用的主机名和端口,如果没有配置,将使用listeners的配置
advertised.listeners=PLAINTEXT://your.host.name:9092

# The number of threads that the server uses for receiving requests from the network and sending responses to the network
# 这个是borker进行网络处理的线程数
num.network.threads=3

# The number of threads that the server uses for processing requests, which may include disk I/O
# 这个是borker进行I/O处理的线程数
num.io.threads=8

# The send buffer (SO_SNDBUF) used by the socket server
# 发送缓冲区buffer大小
socket.send.buffer.bytes=102400

# The receive buffer (SO_RCVBUF) used by the socket server
# kafka接收缓冲区大小
socket.receive.buffer.bytes=102400

# The maximum size of a request that the socket server will accept (protection against OOM)
# kafka接收请求缓冲区大小
socket.request.max.bytes=104857600

############################# Log Basics #############################

# A comma separated list of directories under which to store log files
# 日志存放路径
log.dirs=/usr/local/kafka-cluster/kafka01/log

# 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.
#默认的分区数,一个topic默认1个分区数
num.partitions=1

# 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.
# 每一个数据目录用于在启动kafka时恢复数据和在关闭时刷新数据的线程个数
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
# 每当producer写入10000条消息时,刷数据到磁盘
log.flush.interval.messages=10000

# The maximum amount of time a message can sit in a log before we force a flush
# 每间隔1秒钟时间,刷数据到磁盘
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


message.max.byte=5242880
default.replication.factor=2
replica.fetch.max.bytes=5242880

# 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的连接端口
zookeeper.connect=192.168.205.128:2181,192.168.205.128:2182,192.168.205.128:2183

# Timeout in ms for connecting to zookeeper
#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.
# 设置coordinator接收到成员加入空消费组的请求后,开启rebalance的延迟时间
group.initial.rebalance.delay.ms=0

 


版权声明:本文为llangzh原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。