使用zookeeper 和 kafka 搭建kafka环境用来高并发消息的处理。就集群结构来说可以分为三种:
第一种是单节点单broker的模式,这种模式主要用来进行kafka的学习和开发环境的配置,搭建起来简单、快捷、使用的资源少,不会出现各种问题。
第二种方式是单节点和多broker的模式(最少三个broker),这种集群方式用于分布式部署,但是由于是单节点因此构建的集群稳定性欠佳(当zookeeper节点出现故障后,整个集群无法正常工作),下面记录的是这种部署方式。
第三种方式是多节点和多broker的模式,这种集群方式用于生产环境的分布式部署,稳定性、并发处理、安全性得到进一步提升。
一、下面记录的是搭建集群的结构:一个zookeeper 节点,三个kafka结点。
1、搭建前的准备工作:
- zookeeper 安装包的下载:http://zookeeper.apache.org/ ,当前使用的zookeeper 版本是:zookeeper-3.4.6.tar.gz
- kafka 安装包的下载:http://kafka.apache.org/ , 当前使用的kafka版本是:kafka_2.11-0.11.0.1.tgz
- 整个集群搭建的安装包已放在网盘:https://pan.baidu.com/s/1miiSp64
- 使用的服务器是三台linux服务器 ,用途如下:
编号 | IP | zookeeper节点 | kafka节点 |
1 | 192.1.2.177 | 是 | 是 |
2 | 192.1.2.178 | 否 | 是 |
3 | 192.1.2.231 | 否 | 是 |
即其中一台服务器放有zookeeper节点和kafka节点。
2、修改zookeeper集群配置文件,zookeeper集群配置主要修复zookeeper安装目录下的zoo.cfg文件(如果没有请把zoo_sample.cfg改成zoo.cfg),zookeeper启动时需要使用这个配置文件,我的修改位置是:cd /usr/local/work/zookeeper-3.4.6/conf/
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/usr/zookeeper/data
dataLogDir=/usr/zookeeper/logs
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.1=192.1.2.231:2888:3888
这里重要的是
dataDir=/usr/zookeeper/data
dataLogDir=/usr/zookeeper/logs
server.1=192.1.2.231:2888:3888
三个配置,其他的都是采用zookeeper的默认值,dataDir和dataLogDir两个目录需要手动创建好,否则zookeeper可能无法启动,server.1是zookeeper所在节点的IP地址,是手动添加的配置。
运行 ./bin/zkServer.sh start 命令启动zookeeper 服务。
3、部署三个kafka broker, 把kafka安装文件上传至三个服务器的相应目录,然后找到kafka的配置文件配置,我的文件位置是:cd /usr/local/work/kafka_2.11-0.11.0.1/config/ 下面,运行命令 vim server.properties 编辑文件,文件改动如下:
kafka node 1
# 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=0
host.name=192.1.2.231
# Switch to enable topic deletion or not, default value is false
#delete.topic.enable=true
############################# 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://: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 seperated list of directories under which to store log files
log.dirs=/tmp/kafka-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=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.
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 for 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 exceessive 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 as long as the remaining
# segments don't 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=192.1.2.231:2181
# Timeout in ms for connecting to zookeeper
zookeeper.connection.timeout.ms=6000
############################# 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
这里需要注意三个配置:
broker.id=0 #当前kafka节点的唯一标识
host.name=192.1.2.231 #手动添加的配置,暴露节点当前的IP,如果不配置,则消费消息时会出现错误
zookeeper.connect=192.1.2.231:2181 #当前zookeeper节点所在服务器的位置.
类似的另外两个kafka节点的配置如下:
kafka node 2
broker.id=1
host.name=192.1.2.177
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/tmp/kafka-logs
num.partitions=1
num.recovery.threads.per.data.dir=1
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connect=192.1.2.231:2181
zookeeper.connection.timeout.ms=6000
group.initial.rebalance.delay.ms=0
kafka node3
broker.id=2
host.name=192.1.2.178
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/tmp/kafka-logs
num.partitions=1
num.recovery.threads.per.data.dir=1
offsets.topic.replication.factor=1
transaction.state.log.replication.factor=1
transaction.state.log.min.isr=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connect=192.1.2.231:2181
zookeeper.connection.timeout.ms=6000
group.initial.rebalance.delay.ms=0
然后运行命令启动kakfa服务:
(1)进入kafka安装目录,cd /usr/local/work/kafka_2.11-0.11.0.1/
(2)运行 ./bin/kafka-server-start.sh config/server.properties 或 nohup ./bin/kafka-server-start.sh config/server.properties & (以守护线程方式启动)启动kafka服务,三个节点均进行相同操作。
4、测试kafka 集群
- 输入命令创建topic: ./bin/kafka-topics.sh --create --zookeeper 192.1.2.231:2181 --replication-factor 3 --partitions 1 --topic my-replicated-topic
- 查看topic是否创建成功:./bin/kafka-topics.sh --list --zookeeper 192.1.2.231:2181
- 启动kafka自带的produce 程序:./bin/kafka-console-producer.sh --broker-list 192.1.2.231:9092,192.1.2.177:9092,192.1.2.178:9092 --topic my-replicated-topic
- 启动kafka自带的consumer 程序:./bin/kafka-console-consumer.sh --bootstrap-server 192.1.2.231:9092,192.1.2.177:9092,192.1.2.178:9092 --from-beginning --topic my-replicated-topic
- 在produce控制台输入消息,在consumer控制台查看有没有接受到消息,若能接收到消息,则kafka集群建立成功。
二、下面记录的是搭建集群的结构:三个zookeeper 节点集群和三个kafka结点组成的集群。
这里和单zookeeper节点,多kafka节点不同的地方就是zookeeper的配置文件和kafka 指向zookeeper的配置不同。
zookeeper 的zoo.cfg配置文件列举出所有的zookeeper节点IP,例如:
server.2=192.1.2.177:2888:3888
kafka的配置只需要更改zookeeper的连接地址:
zookeeper.connect=192.1.2.231:2181,192.1.2.177:2181,192.1.2.178:2181