一、资料准备
(一)安装包
- kafka下载地址:kafka_2.12-2.2.2
- zookeeper下载地址:zookeeper-3.4.13
- 百度云下载(包含kafka和zookeeper): 链接:https://pan.baidu.com/s/16iuFdl9_3e0rn7OQ2a0WBg
提取码:ypsd - csdn下载(保证超值):下载
(二)服务器环境(此处用虚拟机)
- centos7_X64:192.168.1.11
- centos7_X64:192.168.1.12
- centos7_X64:192.168.1.13
(三)java环境
export JAVA_HOME=/usr/local/java/jdk1.8.0_212
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
二、开始安装
(一)安装zookeeper
1. zookeeper官方安装文档
2. 将zookeeper安装包上传到服务器中,并解压
[root@localhost opt]# ll
total 36328
drwxr-xr-x. 2 root root 6 Oct 30 2018 rh
drwxr-xr-x. 10 501 games 4096 Jun 30 2018 zookeeper-3.4.13
-rw-r--r--. 1 root root 37191810 Jul 17 19:19 zookeeper-3.4.13.tar.gz
[root@localhost opt]#
3. 接下来看看配置文件
3.1 配置文件目录
刚解压没有zoo.cfg的,这个是我复制出来的
[root@localhost conf]# pwd
/opt/zookeeper-3.4.13/conf
[root@localhost conf]# ll
total 16
-rw-r--r--. 1 501 games 535 Jun 29 2018 configuration.xsl
-rw-r--r--. 1 501 games 2161 Jun 29 2018 log4j.properties
-rw-r--r--. 1 root root 922 Jul 17 19:28 zoo.cfg
-rw-r--r--. 1 501 games 922 Jun 29 2018 zoo_sample.cfg
[root@localhost conf]#
3.2 配置文件详解
vim zoo.cfg
默认的是这样的
# 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=/tmp/zookeeper
# 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
tickTime:Client-Server通信心跳时间,Zookeeper服务器之间或者客户端和服务器之间维持心跳的时间间隔,也就是每个tickTime时间就会发送一个心跳,以毫秒为单位
initLimit:Leader-follower初始通信时限,集群中的follower服务器(F)与leader服务器(L)之间初始连接时能容忍的最多心跳数(tickTime的数量)。
syncLimit: Leader-Follower同步通信时限,集群中的follower服务器与leader服务器之间请求和应答之间能容忍的最多心跳数(tickTime的数量)。
dataDir:Zookeeper保存数据的目录,默认情况下,Zookeeper将写数据的日志文件保存。
clientPort:客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
clientPort=2181
其他参数在后续会持续更新,如有需要,请参考ZooKeeper参数调优
4. 修改配置文件
编辑配置文件
vim zoo.cfg
创建数据目录
cd /opt/zookeeper-3.4.13
mkdir data
修改后
# 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=/opt/zookeeper-3.4.13/data
# 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.168.1.11:2888:3888
server.2=192.168.1.12:2888:3888
server.3=192.168.1.13:2888:3888
在数据目录中,创建myid文件,每台虚拟机都需要建立,文件中存放id,不能重复,我的11–1 12–2 13–3
[root@localhost data]# ll
total 4
-rw-r--r--. 1 root root 4 Jul 17 19:54 myid
[root@localhost data]#
vi myid
5. 启动服务
启动前建议先关闭服务器防火墙,或者将端口开放
cd /opt/zookeeper-3.4.13/bin
./zkServer.sh start
[root@localhost zookeeper-3.4.13]# cd bin/
[root@localhost bin]# ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.4.13/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
6. 检查
6.1 端口检查
netstat -ntlp
可看到有2181、3888端口启动
[root@localhost bin]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1848/master
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1619/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1848/master
tcp6 0 0 :::2181 :::* LISTEN 38847/java
tcp6 0 0 :::36234 :::* LISTEN 38847/java
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 192.168.1.11:3888 :::* LISTEN 38847/java
tcp6 0 0 :::22 :::* LISTEN 1619/sshd
[root@localhost bin]#
另外会发现有一台的端口还有一个 2888,这就是leader
[root@localhost bin]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1851/master
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1624/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1851/master
tcp6 0 0 :::2181 :::* LISTEN 39265/java
tcp6 0 0 192.168.1.12:2888 :::* LISTEN 39265/java
tcp6 0 0 :::44843 :::* LISTEN 39265/java
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 192.168.1.12:3888 :::* LISTEN 39265/java
tcp6 0 0 :::22 :::* LISTEN 1624/sshd
[root@localhost bin]#
6.2 命令检查
- 进入zookeeper客户端
./zkCli.sh -server 127.0.0.1:2181
[root@localhost bin]# ./zkCli.sh -server 127.0.0.1:2181
Connecting to 127.0.0.1:2181
2021-07-17 20:11:25,022 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03, built on 06/29/2018 04:05 GMT
2021-07-17 20:11:25,025 [myid:] - INFO [main:Environment@100] - Client environment:host.name=localhost
2021-07-17 20:11:25,025 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_212
2021-07-17 20:11:25,026 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2021-07-17 20:11:25,026 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/local/java/jdk1.8.0_212/jre
2021-07-17 20:11:25,026 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/opt/zookeeper-3.4.13/bin/../build/classes:/opt/zookeeper-3.4.13/bin/../build/lib/*.jar:/opt/zookeeper-3.4.13/bin/../lib/slf4j-log4j12-1.7.25.jar:/opt/zookeeper-3.4.13/bin/../lib/slf4j-api-1.7.25.jar:/opt/zookeeper-3.4.13/bin/../lib/netty-3.10.6.Final.jar:/opt/zookeeper-3.4.13/bin/../lib/log4j-1.2.17.jar:/opt/zookeeper-3.4.13/bin/../lib/jline-0.9.94.jar:/opt/zookeeper-3.4.13/bin/../lib/audience-annotations-0.5.0.jar:/opt/zookeeper-3.4.13/bin/../zookeeper-3.4.13.jar:/opt/zookeeper-3.4.13/bin/../src/java/lib/*.jar:/opt/zookeeper-3.4.13/bin/../conf:.:/usr/local/java/jdk1.8.0_212/lib:/usr/local/java/jdk1.8.0_212/jre/lib
2021-07-17 20:11:25,026 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2021-07-17 20:11:25,026 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2021-07-17 20:11:25,026 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2021-07-17 20:11:25,027 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2021-07-17 20:11:25,027 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2021-07-17 20:11:25,027 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-1062.el7.x86_64
2021-07-17 20:11:25,027 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2021-07-17 20:11:25,027 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2021-07-17 20:11:25,027 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/opt/zookeeper-3.4.13/bin
2021-07-17 20:11:25,027 [myid:] - INFO [main:ZooKeeper@442] - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@5ce65a89
2021-07-17 20:11:25,049 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1029] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
Welcome to ZooKeeper!
JLine support is enabled
2021-07-17 20:11:25,108 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@879] - Socket connection established to localhost/127.0.0.1:2181, initiating session
2021-07-17 20:11:25,120 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1303] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x200004872a60001, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: 127.0.0.1:2181(CONNECTED) 0]
- 查看zookeeper状态
可看到两台follower。一台leader
./zkServer.sh status
1.11
[root@localhost bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.4.13/bin/../conf/zoo.cfg
Mode: follower
[root@localhost bin]#
1.12
[root@localhost bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.4.13/bin/../conf/zoo.cfg
Mode: leader
[root@localhost bin]#
1.13
[root@localhost bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper-3.4.13/bin/../conf/zoo.cfg
Mode: follower
[root@localhost bin]#
至此安装完成,更多的zookeeper的命令操作,请参考Zookeeper基础命令操作
(二)安装kafka
1. kafka安装文档
2. 上传安装包
解压
[root@localhost opt]# ll
total 92264
drwxr-xr-x. 8 root root 116 Jul 17 20:31 kafka_2.12-2.2.2
-rw-r--r--. 1 root root 57276047 Jul 17 20:23 kafka_2.12-2.2.2.tgz
drwxr-xr-x. 2 root root 6 Oct 30 2018 rh
drwxr-xr-x. 11 501 games 4096 Jul 17 19:43 zookeeper-3.4.13
-rw-r--r--. 1 root root 37191810 Jul 17 19:19 zookeeper-3.4.13.tar.gz
[root@localhost opt]#
3. 修改配置文件
cd kafka_2.12-2.2.2/config/
编辑配置文件
vim server.properties
需要修改的几个地方,每个节点都如此
broker.id=0 #每个节点不能一样
listeners=PLAINTEXT://0.0.0.0:9092 #开放访问
advertised.listeners=PLAINTEXT://192.168.1.11:9092 访问这个主节点需要使用的地址
log.dirs=/opt/kafka_2.12-2.2.2/logdata #日志文件目录,请提前创建
zookeeper.connect=192.168.1.11:2181,192.168.1.12:2181,192.168.1.13:2181 #zookeeper集群地址
完整配置文件
# 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://0.0.0.0: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://192.168.1.11: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/kafka_2.12-2.2.2/logdata
# 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=192.168.1.11:2181,192.168.1.12:2181,192.168.1.13: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
4. 启动
-daemon 后台启动
sh /opt/kafka_2.12-2.2.2/bin/kafka-server-start.sh -daemon /opt/kafka_2.12-2.2.2/config/server.properties
5. 创建topic
./kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 3 --partitions 1 --topic test1
6. 启动生产者
./kafka-console-producer.sh --broker-list 192.168.1.11:9092,192.168.1.12:9092,192.168.1.13:9092 --topic test1
7、 启动消费者
/kafka-console-consumer.sh --bootstrap-server 192.168.1.13:9092,192.168.1.12:9092,192.168.1.11:9092 --from-beginning --topic test1
8. 完成
生产消费都可以,安装完成。