1.下载解压kafka2.12-2.3.1,放到/usr/local/目录下
2.进入config目录,修改zookeeper配置文件
[root@localhost config]# pwd
/usr/local/kafka_2.12-2.3.1-0/config
[root@localhost config]#
[root@localhost config]# cat zookeeper.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.
# the directory where the snapshot is stored.
# dataDir=/tmp/zookeeper
# disable the per-ip limit on the number of connections since this is a non-production config
maxClientCnxns=0
#修改为自定义的zookeeper数据目录
dataDir=/usr/local/kafka_2.12-2.3.1-0/zookeeper
#修改为自定义的zookeeper日志目录
dataLogDir=/usr/local/kafka_2.12-2.3.1-0/log/zookeeper
# 端口
clientPort=2181
#注释掉
#maxClientCnxns=0
#设置连接参数,添加如下配置
tickTime=2000
#为zk的基本时间单元,毫秒
initLimit=10
#Leader-Follower初始通信时限tickTime*10
syncLimit=5
#Leader-Follower同步通信时限tickTime*5
#设置broker Id的服务地址
server.0=localhost:2888:3888
#server.1=localhost:2888:3888
#server.2=localhost:2888:3888
[root@localhost config]#
3.进入config目录设置kafka配置文件,server.properties
[root@localhost config]# pwd
/usr/local/kafka_2.12-2.3.1-0/config
[root@localhost 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=0
############################# 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://localhost: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://localhost:9092
advertised.host.name=localhost
# 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=../logs/kafka
# 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 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=localhost: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
[root@localhost config]#
4.启动zookeeper
bin/zookeeper-server-start.sh config/zookeeper.properties &
5.启动kafka
bin/kafka-server-start.sh config/server.properties &
6.创建主题及分区
bin/kafka-topics.sh -create -zookeeper localhost:2181 -replication-factor 1 --partitions 3 --topic test
7. 测试生产者和消费者
生产者:
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
消费者:
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
8.查看消费者群组
[root@localhost bin]# ./kafka-consumer-groups.sh --bootstrap-server 172.16.2.6:9092 --group testGroup --describe
成功!!
附录:以下为参考资料
1.jdk 1.8
2.安装zookeeper
(1)问题:错误: 找不到或无法加载主类 org.apache.zookeeper.server.quorum.QuorumPeerMain
解决方案
总结:原来从目前的最新版本3.5.5开始,带有bin名称的包才是我们想要的下载,可以直接使用的里面有编译后的二进制的包,而之前的普通的tar.gz的包里面是只是源码的包无法直接使用
(2)Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error)
./zkCli.sh
sh zkServer.sh status
sudo netstat -anp | grep 2181
netstat -ntlp
进入log目录:[main:ZooKeeperServerMain@79] - Unable to start AdminServer, exiting abnormally
https://www.cnblogs.com/boboooo/p/9242088.html
[root@localhost conf]# pwd
/usr/local/apache-zookeeper-3.5.6-bin/conf
[root@localhost conf]# cat zoo.cfg
tickTime=2000
dataDir=/home/zookeeper/
clientPort=2181
admin.serverPort=8888
[root@localhost conf]#sh zkServer.sh status
sh zkCli.sh
srvr
(3)Command not found: Command not found srvr
验证zookeeper是否成功启动
bin/zkServer.sh status(一个leader,两个follower)
或者在Zookeeper安装的任何一个节点执行客户端连接命令:
bin/zkCli.sh -server 192.168.1.1:2181
3.安装kafka broker
问题1:[root@localhost kafka_2.12-2.3.1-0]# bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
zookeeper is not a recognized option
最后附上0.90版本之后启动消费者的方法: bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
问题2:
WARN Exception causing close of session 0x0: null (org.apache.zookeeper.server.NIOServerCnxn)
[2019-12-05 09:29:16,035] INFO Closed socket connection for client /127.0.0.1:56174 (no session established for client) (org.apache.zookeeper.server.NIOServerCnxn)
[2019-12-05 09:56:48,941] WARN Exception causing close of session 0x0: null (org.apache.zookeeper.server.NIOServerCnxn)
答案:zookeeper配置文件,broker Id未设置
#设置broker Id的服务地址
server.0=localhost:2888:3888
问题3:[2019-12-05 10:06:33,294] INFO Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
[2019-12-05 10:06:33,295] INFO Socket error occurred: localhost/127.0.0.1:2181: 拒绝连接 (org.apache.zookeeper.ClientCnxn)
答案:未启动zookeeper,由于上一步停止了,kafka报的错
问题2:最终原因:命令输入错误
错误命令:[root@localhost bin]# bin/kafka-console-consumer.sh --bootstrap-server localhost:2181 --topic test --from-beginning
正确命令:[root@localhost bin]# bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning
https://www.cnblogs.com/caoweixiong/p/11060533.html
Kafka集群安裝部署(自带Zookeeper)
kafka简介
kafka官网:http://kafka.apache.org/
kafka下载页面:http://kafka.apache.org/downloads
kafka配置快速入门:http://kafka.apache.org/quickstart
新版本的kafka自带有zookeeper,本篇文章记录使用自带zookeeper搭建kafka集群。
為了實現高可用,高吞吐量,kafka以集群方式部署,分別在3臺服務器上安裝部署,因kafka依賴於zookeeper,如果想要使用Kafka,就必须安装ZK,Kafka中的消费偏置信息、kafka集群、topic信息会被存储在ZK中。
這裏採用kafka內置的zookeeper,也是以集群方式部署,無需在單獨安裝zookeeper。
首先让我们看几个基本的消息系统术语:
•Kafka将消息以topic为单位进行归纳。
•将向Kafka topic发布消息的程序成为producers.
•将预订topics并消费消息的程序成为consumer.
•Kafka以集群的方式运行,可以由一个或多个服务组成,每个服务叫做一个broker.
producers通过网络将消息发送到Kafka集群,集群向消费者提供消息,如下图所示:
客户端和服务端通过TCP协议通信。Kafka提供了Java客户端,并且对多种语言都提供了支持。
环境准备
kafka版本:kafka_2.11-1.0.0.tgz
三台主机IP:172.16.10.91、172.16.10.92、172.16.10.93
安裝配置工作
1、kafka安裝
下载后解压kafka,将其解压到/usr/local目录下,删除压缩包即可:
tar xzvf kafka_2.11-1.0.0.tgz #解压
进入到kafka的config目录
自定义目录:
首先新建kafka的日志目录和zookeeper数据目录,因为这两项默认放在tmp目录,而tmp目录中内容会随重启而丢失,所以我们自定义以下目录:
mkdir /usr/local/kafka_2.11-1.0.0/zookeeper #创建zookeeper数据目录
mkdir /usr/local/kafka_2.11-1.0.0/log #创建日志目录
mkdir /usr/local/kafka_2.11-1.0.0/log/zookeeper #创建zookeeper日志目录
mkdir /usr/local/kafka_2.11-1.0.0/log/kafka #创建kafka日志目录
2、zookeeper配置
> 修改 zookeeper.properties
进入config目录下,修改关键配置如下,3台服务器的zookeeper.properties配置文件都一样
#修改为自定义的zookeeper数据目录
dataDir=/usr/local/kafka_2.11-1.0.0/zookeeper
#修改为自定义的zookeeper日志目录
dataLogDir=/usr/local/kafka_2.11-1.0.0/log/zookeeper
# 端口
clientPort=2181
#注释掉
#maxClientCnxns=0
#设置连接参数,添加如下配置
tickTime=2000 #为zk的基本时间单元,毫秒
initLimit=10 #Leader-Follower初始通信时限 tickTime*10
syncLimit=5 #Leader-Follower同步通信时限 tickTime*5
#设置broker Id的服务地址
server.0=172.16.10.91:2888:3888
server.1=172.16.10.92:2888:3888
server.2=172.16.10.93:2888:3888
> zookeeper数据目录添加myid配置
在各台服务器的zookeeper数据目录【/usr/local/kafka_2.11-1.0.0/zookeeper】添加myid文件,写入服务broker.id属性值
如这里的目录是/usr/local/kafka_2.11-1.0.0/zookeeper
注:3臺服務器都要添加myid文件,但值的內容不一樣,具體關係請見下圖:
例如:第一台broker.id为0的服务到该目录【/usr/local/kafka_2.11-1.0.0/zookeeper】下执行以下命令,會生成myid文件內容
[root@mmc config]# echo 0 > myid
3、kafka配置
进入config目录下,修改server.properties文件
注:3臺服務器都要配置,其中【broker.id、advertised.listeners】不一樣,其它配置都一樣
############################# Server Basics #############################
# broker 的全局唯一编号,不能重复
broker.id=1
############################# Socket Server Settings #############################
# 配置监听,修改为本机ip
advertised.listeners=PLAINTEXT://172.16.10.91:9092
# 处理网络请求的线程数量,默认
num.network.threads=3
# 用来处理磁盘IO的线程数量,默认
num.io.threads=8
# 发送套接字的缓冲区大小,默认
socket.send.buffer.bytes=102400
# 接收套接字的缓冲区大小,默认
socket.receive.buffer.bytes=102400
# 请求套接字的缓冲区大小,默认
socket.request.max.bytes=104857600
############################# Zookeeper #############################
# 配置三台服务zookeeper连接地址
zookeeper.connect=172.16.10.91:2181,172.16.10.92:2181,172.16.10.93:2181
############################# Log Basics #############################
# kafka 运行日志存放路径
log.dirs=/usr/local/kafka_2.11-1.0.0/log/kafka
# topic 在当前broker上的分片个数,与broker保持一致
num.partitions=3
# 用来恢复和清理data下数据的线程数量,默认
num.recovery.threads.per.data.dir=1
############################# Log Retention Policy #############################
# segment文件保留的最长时间,超时将被删除,默认
log.retention.hours=168
# 滚动生成新的segment文件的最大时间,默认
log.roll.hours=168
4、kafka启动
kafka启动时先启动zookeeper,再启动kafka;关闭时相反,先关闭kafka,再关闭zookeeper
- 進入kafka根目錄
[root@localhost kafka_2.11-1.0.0]# cd /usr/local/kafka_2.11-1.0.0/
- 啟動zookeeper
輸入命令:
[root@localhostkafka_2.11-1.0.0]# bin/zookeeper-server-start.sh config/zookeeper.properties &
#後臺啟動命令:
[root@localhostkafka_2.11-1.0.0]# nohup bin/zookeeper-server-start.sh config/zookeeper.properties >log/zookeeper/zookeeper.log 2>1 &
查看log/zookeeper/zookeeper.log 文件,第1、2臺服務器啟動的時候,會報錯Connection refused,因為另外1、2臺服務器還沒啟動,先不用管它
查看log/zookeeper/zookeeper.log 文件,等3臺服務都啟動完後,就不在報錯了
- 啟動kafka
輸入命令:
[root@localhostkafka_2.11-1.0.0]# bin/kafka-server-start.sh config/server.properties &
#後臺啟動命令:
[root@localhostkafka_2.11-1.0.0]# nohup bin/kafka-server-start.sh config/server.properties >log/kafka/kafka.log 2>1 &
查看log/kafka/kafka.log 文件,啟動過程中沒有報錯,且有以下信息輸出,說明啟動成功
5、kafka测试验证
分别启动三台机器的zookeeper,三台机器的zookeeper都启动成功后,再分别启动三台机器的kafka。
- 進入kafka根目錄
[root@localhost kafka_2.11-1.0.0]# cd /usr/local/kafka_2.11-1.0.0/
- 在某臺機器創建topic,名稱為test
1 |
|
命令解析:
--create: #指定创建topic动作
--topic: #指定新建topic的名称
--zookeeper: #指定kafka连接zk的连接url,该值和server.properties文件中的配置项{zookeeper.connect}一样
--partitions 3 #指定当前创建的kafka分区数量,默认为1个
--replication-factor 3 #指定每个分区的副本集数量,不能大于 broker 节点数量,多了也没用,1个节点放>=2个副本挂了都完蛋。
- 查看topic,确认topic创建成功
[root@localhost kafka_2.11-1.0.0]# bin/kafka-topics.sh --list --zookeeper 172.16.10.91:2181
- 查看topic,详细信息
[root@localhost kafka_2.11-1.0.0]# bin/kafka-topics.sh --describe --zookeeper 172.16.10.93:2181 --topic test
可以描述为:Topic分区数/副本数/副本Leader/副本ISR
等信息:
“leader”:该节点负责该分区的所有的读和写,每个节点的leader都是随机选择的。
“replicas”:备份的节点列表,无论该节点是否是leader或者目前是否还活着,只是显示。
“isr”:同步备份”的节点列表,也就是活着的节点并且正在同步leader
其中Replicas和Isr中的1,2,0就对应着3个broker他们的broker.id属性!
- 在某臺服務器上创建生产者
[root@localhost kafka_2.11-1.0.0]# bin/kafka-console-producer.sh --broker-list 172.16.10.91:9092,172.16.10.92:9092,172.16.10.93:9092 --topic test
- 在另外两台服務器创建消费者
[root@localhost kafka_2.11-1.0.0]# bin/kafka-console-consumer.sh --zookeeper 172.16.10.91:2181,172.16.10.92:2181,172.16.10.93:2181 --topic test --from-beginning
- 生產者服務器截圖
- 消費者服務器截圖
- 修改topic信息
bin/kafka-topics.sh --zookeeper 192.168.187.146:2181 --alter --topic test --config max.message.bytes=128000
bin/kafka-topics.sh --zookeeper 192.168.187.146:2181 --alter --topic test --delete-config max.message.bytes
bin/kafka-topics.sh --zookeeper 192.168.187.146:2181 --alter --topic test --partitions 10
bin/kafka-topics.sh --zookeeper 192.168.187.146:2181 --alter --topic test --partitions 3 ## Kafka分区数量只允许增加,不允许减少
- 删除topic
注意,只是删除Topic
在zk
的元数据,日志数据仍需手动删除
[root@localhost kafka_2.11-1.0.0]# bin/kafka-topics.sh -delete --zookeeper 172.16.10.91:2181,172.16.10.92:2181,172.16.10.93:2181 --topic test
Note: This will have no impact if delete.topic.enable is not set to true.## 默认情况下,删除是标记删除,没有实际删除这个Topic;如果运行删除Topic,两种方式:
方式一:通过delete命令删除后,手动将本地磁盘以及zk上的相关topic的信息删除即可
方式二:配置server.properties文件,给定参数delete.topic.enable=true,重启kafka服务,此时执行delete命令表示允许进行Topic的删除
- 查看kafka未消费的消息
[root@localhost kafka_2.11-1.0.0]# bin/kafka-console-consumer.sh --bootstrap-server 172.16.10.91:9092,172.16.10.92:9092,172.16.10.93:9092 --topic logstash-busi-2 --from-beginning
- 查看kafka消费進度
命令:bin/kafka-consumer-groups.sh --bootstrap-server <Kafka broker 连接信息> --describe --group <group 名称>
bin/kafka-consumer-groups.sh --bootstrap-server 172.16.10.91:9092,172.16.10.92:9092,172.16.10.93:9092 --describe --group terminalMonitor
6、kafka测试集群的容错能力
Kafka是支持容错的,下面我们来对Kafka的容错性进行测试,测试步骤如下
(1).查看topic的详细信息,观察那个blocker的角色是leader,那些blocker的角色是follower
[hadoop@Master ~]$ kafka-topics.sh --describe --zookeeper localhost:2181 --topic my-replicated-topic
Topic:my-replicated-topic PartitionCount:1 ReplicationFactor:3 Configs:
Topic: my-replicated-topic Partition: 0 Leader: 2 Replicas: 2,3,1 Isr: 2,3,1
(2).手工kill掉任意一个状态是follower的borker,测试生成和消费信息是否正确
步骤1中可以看到 2 为leader,1 和 3为 follower,将follower为1的进程kill掉
启动生产和消费者测试信息是否正确
结论:kill掉任意一个状态是follower的broker,生成和消费信息正确,不受任何影响
(3).手工kill掉状态是leader的borker,测试生产和消费的信息是否正确
borker2的角色为leader,将它kill掉,borker 3变成了leader
启动生产和消费者测试信息是否正确
结论:kill掉状态是leader的borker,生产和消费的信息正确
总结:不管当前状态的borker是leader还是follower,当我们kill掉后,只要有一个borker能够正常使用,则消息仍然能够正常的生产和发送。即Kafka的容错性是有保证的!