1.安裝JDK
1.1 安裝文件 http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html 下載Server JRE
1.2 添加環境變量
1.2.1 添加JAVA_HOME環境變量
1.2.2 Path環境變量:在現有的值後面添加";%JAVA_HOME%\bin"
1.3 打開cmd運行 "java -version" 查看當前系統Java的版本
2.安裝運行Zookeeper
Kafka的運行依賴於Zookeeper,所以在運行Kafka之前我們需要安裝並運行Zookeeper
2.1 下載安裝文件 http://zookeeper.apache.org/releases.html
2.2 解壓文件(本文解壓到C:\kafka\zookeeper-3.4.11)
2.3 打開C:\kafka\zookeeper-3.4.11\conf,把zoo_sample.cfg重命名成zoo.cfg
2.4 從文本編輯器裏打開zoo.cfg
2.5 修改數據存儲目錄dataDir值:dataDir=C:/kafka/zookeeper-3.4.11/data
注意:該目錄不需要手動創建,啓動zookeeper服務時會自動創建,路徑要麼是"/"分割,要麼是轉義字符"\\",這樣會生成正確的路徑(層級,子目錄)。
2.6 添加環境變量
2.6.1 ZOOKEEPER_HOME環境變量
2.6.2 Path環境變量:在現有的值後面添加 ";%ZOOKEEPER_HOME%\bin;"
2.7 運行Zookeeper:打開cmd然後執行命令zkserver
注意:不要關了這個窗口
3.安裝運行Kafka
3.1 下載安裝文件:http://kafka.apache.org/downloads.html
3.2 解壓文件(本文解壓到 C:\kafka\kafka_2.12-1.1.0)
3.3 打開 C:\kafka\kafka_2.12-1.1.0\config目錄,從文本編輯器裏打開 server.properties
3.4 修改數據存儲目錄log.dirs值
log.dirs=C:/kafka/kafka_2.12-1.1.0/kafka-logs
多個地址的話用逗號分割,多個目錄分佈在不同磁盤上可以提高讀寫性能,如:log.dirs=C:/kafka/kafka_2.12-1.1.0/kafka-logs-1,C:/kafka/kafka_2.12-1.1.0/kafka-logs-2,C:/kafka/kafka_2.12-1.1.0/kafka-logs-3
注意:以上目錄不需要手動創建,啓動kafka服務時會自動創建,路徑要麼是"/"分割,要麼是轉義字符"\\",這樣會生成正確的路徑(層級,子目錄)。
3.5 修改IP地址和端口號(默認9092),IP地址配置爲127.0.0.1只對本機有效
3.6 修改Zookeeper集羣地址
zookeeper.connect=192.168.50.234:2181
3.7 其他參數參數:
參數 |
參數說明 |
num.network.threads=4 |
處理消息的最大線程數,一般情況下數量爲cpu核數 |
num.io.threads=8 |
處理磁盤IO的線程數,數值爲cpu核數2倍 |
queued.max.requests=5000 |
等待IO線程處理的請求隊列最大數,若是等待IO的請求超過這個數值,那麼會停止接受外部消息,應該是一種自我保護機制。 |
socket.send.buffer.bytes=1024000 |
service的發送緩衝區大小,單位是字節 |
socket.receive.buffer.bytes=1024000 |
service的接受緩衝區大小,單位是字節 |
log.flush.interval.messages=10000 |
每當producer寫入10000條消息時,刷數據到磁盤 |
log.flush.interval.ms=1000 |
每間隔1秒鐘時間,刷數據到磁盤,單位爲毫秒 |
log.cleanup.policy=delete |
日誌清理策略選擇有:delete和compact,主要針對過期數據的處理,或是日誌文件達到限制的額度,會被 topic創建時的指定參數覆蓋 |
log.cleaner.enable=true |
是否開啓日誌清理 |
log.retention.hours=168 |
當kafka broker被寫入海量消息後,會生成很多數據文件,佔用大量磁盤空間,kafka默認是保留7天,建議根據磁盤情況配置,避免磁盤撐爆。 |
log.retention.minutes=2 |
保留2分鐘 |
zookeeper.connect=192.168.50.234:2181 |
zookeeper集羣的地址,可以是多個,多個之間用逗號分割,如: hostname1:port1,hostname2:port2,hostname3:port3 |
zookeeper.connection.timeout.ms=6000 |
Zookeeper的連接超時時間,單位爲毫秒 |
offsets.topic.replication.factor=3 |
|
transaction.state.log.replication.factor=3 |
|
auto.leader.rebalance.enable=true |
|
我的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://192.168.50.234:9092
host.name=192.168.50.234
port=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.50.234:9092
advertised.host.name=192.168.50.234
advertised.port=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=5
# The number of threads that the server uses for processing requests, which may include disk I/O
num.io.threads=8
queued.max.requests=5000
# The send buffer (SO_SNDBUF) used by the socket server
socket.send.buffer.bytes=1024000
# The receive buffer (SO_RCVBUF) used by the socket server
socket.receive.buffer.bytes=1024000
# 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=C:/kafka/kafka_2.12-1.1.0/kafka-logs-1,C:/kafka/kafka_2.12-1.1.0/kafka-logs-2,C:/kafka/kafka_2.12-1.1.0/kafka-logs-3
# 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=3
transaction.state.log.replication.factor=3
transaction.state.log.min.isr=1
auto.leader.rebalance.enable=true
############################# 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
#log.retention.minutes=2
log.cleaner.enable=true
log.cleanup.policy=delete
# 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.50.234: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
3.8 配置完後成,打開cmd,進入kafka文件目錄:cd /d C:\kafka\kafka_2.12-1.1.0\
3.9 輸入並執行以打開kafka:.\bin\windows\kafka-server-start.bat .\config\server.properties
注意:不要關了這個窗口
3.10 遇到的問題:啓動kafka找不到或無法加載主類
解決方案可參考 https://blog.csdn.net/u010775025/article/details/79208238
4.常用kafka命令
4.1 運行zkserver
zkserver
4.2 運行kafka
.\bin\windows\kafka-server-start.bat .\config\server.properties
4.3 創建topic
.\bin\windows\kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
4.4 創建producer
.\bin\windows\kafka-console-producer.bat --broker-list localhost:9092 --topic test
4.5 創建consumer
.\bin\windows\kafka-console-consumer.bat --zookeeper localhost:2181 --topic test
4.6 查看創建的主題列表
bin\windows\kafka-topics.bat --list --zookeeper localhost:2181
4.7 查看指定topic信息
bin\windows\kafka-topics.bat --zookeeper 127.0.0.1:2181 --describe --topic test
4.8 刪除主題
bin\windows\kafka-topics.bat --zookeeper 127.0.0.1:2181 --delete --topic test
-- 刪除topic,慎用,只會刪除zookeeper中的元數據,消息文件須手動刪除
bin\windows\kafka-topics.bat kafka.admin.DeleteTopicCommand --zookeeper 127.0.0.1:2181 --delete --topic test
4.9 查看topic某分區偏移量最大(小)值
.\bin\windows\kafka-run-class.bat kafka.tools.GetOffsetShell --topic luoluo --time -1 --broker-list localhost:9092 --partitions 0