一胸嘴、前提:
(1)Kafka服務油吭,是基于zookeeper的勾扭。
(2)Kafka使用穩(wěn)定版本:kafka_2.11-1.0.0.tgz;
(3)可在master上安裝后要尔,scp到其他slave機器上舍杜。
二、配置Hbase:
首先要注意在生產(chǎn)環(huán)境中目錄結構要定義好赵辕,防止在項目過多的時候找不到所需的項目
- 目錄統(tǒng)一放在/home下面 既绩,首先創(chuàng)建kafka項目目錄
cd /home
#創(chuàng)建項目目錄
mkdir kafka
cd /home/kafka
#創(chuàng)建kafka消息目錄,主要存放kafka消息
mkdir kafkalogs
- 將 kafka_2.11-1.0.0.tgz 放在 /home/kafka下还惠,并解壓縮饲握,解壓縮完成后可刪除安裝包:
#解壓縮
tar -zxvf kafka_2.11-1.0.0.tgz
#完成后可刪除
rm -f kafka_2.11-1.0.0.tgz
- 修改配置文件
(1)進入到config目錄
cd /home/kafka/kafka_2.11-1.0.0/config/
主要關注:server.properties 這個文件即可,我們可以發(fā)現(xiàn)在目錄下:
有很多文件,這里可以發(fā)現(xiàn)有Zookeeper文件救欧,我們可以根據(jù)Kafka內帶的zk集群來啟動衰粹,但是建議使用獨立的zk集群
(2)修改配置文件 server.properties:
其中broker.id=,host.name=笆怠,listeners=铝耻,每臺服務器都不能相同,scp完后注意修改
# 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://master:9092
# Hostname the broker will bind to. If not set, the server will bind to all interfaces
# 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
num.network.threads=3
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
# A comma seperated list of directories under which to store log files
log.dirs=/home/kafka/kafkalogs
num.recovery.threads.per.data.dir=1
############################# Internal Topic Settings #############################
############################# 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.
# 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
# from the end of the log.
message.max.byte=5242880
replica.fetch.max.bytes=5242880
# segments drop below log.retention.bytes. Functions independently of log.retention.hours.
#log.retention.bytes=1073741824
log.segment.bytes=1073741824
default.replication.factor=2
# 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
log.cleaner.enable=false
############################# Zookeeper #############################
# root directory for all kafka znodes.
############################# 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) 將安裝配置好的kafka目錄復制到所有slave節(jié)點中:
scp -r /home/kafka/ root@slave1:/home/
(4)修改所有slave節(jié)點中server.properties中的broker.id=骑疆,host.name=田篇,listeners=。
三箍铭、啟動kafka:
- 啟動kafka集群,命令如下:
#從后臺啟動Kafka集群(所有機器都需要啟動)
cd
#進入到kafka的bin目錄
cd /home/kafka/kafka_2.11-1.0.0/bin
#啟動服務
./kafka-server-start.sh -daemon ../config/server.properties
使用命令:jps椎镣,檢查服務是否啟動
創(chuàng)建Topic來驗證是否創(chuàng)建成功
(1)創(chuàng)建Topic:
cd /home/kafka/kafka_2.11-1.0.0/bin
./kafka-topics.sh --create --zookeeper master:2181 --replication-factor 3 --partitions 1 --topic test1
#解釋
--replication-factor 2 #復制3份
--partitions 1 #創(chuàng)建1個分區(qū)
--topic #主題為test1
(2)在一臺服務器上創(chuàng)建一個發(fā)布者诈火,這里我們選用slave1:
#創(chuàng)建一個broker,發(fā)布者
./kafka-console-producer.sh --broker-list slave1:9092 --topic test1
(3)在一臺服務器上創(chuàng)建一個訂閱者状答,這里我們選擇master:
./kafka-console-consumer.sh --zookeeper localhost:2181 --topic test1 --from-beginning
(4)測試
在發(fā)布者那里發(fā)布消息看看訂閱者那里是否能正常收到冷守。
如果收到,至此惊科,Kafka集群環(huán)境搭建完成拍摇!
四、補充:
大部分命令可以去官方文檔查看
- 查看topic
#就會顯示我們創(chuàng)建的所有topic
./kafka-topics.sh --list --zookeeper localhost:2181
- 查看topic狀態(tài)
./kafka-topics.sh --describe --zookeeper localhost:2181 --topic test1
#下面是顯示信息
Topic:test1 PartitionCount:1 ReplicationFactor:3 Configs:
Topic: test1 Partition: 0 Leader: 0 Replicas: 0,1,2 Isr: 0,1,2
#分區(qū)為為1 復制因子為2 test1的分區(qū)為0
#Replicas: 0,1 復制的為0馆截,1,充活,3
- 上面的大家你完成之后可以登錄zk來查看zk的目錄情況
#使用客戶端進入zk
./zkCli.sh -server 127.0.0.1:2181 #默認是不用加’-server‘參數(shù)的因為我們修改了他的端口
#標注一個重要的
[zk: 127.0.0.1:2181(CONNECTED) 1] get /brokers/ids/0
{"listener_security_protocol_map":{"PLAINTEXT":"PLAINTEXT"},"endpoints":["PLAINTEXT://master:9092"],"jmx_port":-1,"host":"master","timestamp":"1519797571108","port":9092,"version":4}
cZxid = 0x700000055
ctime = Wed Feb 28 13:59:30 CST 2018
mZxid = 0x700000055
mtime = Wed Feb 28 13:59:30 CST 2018
pZxid = 0x700000055
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x361dafd39070000
dataLength = 182
numChildren = 0
#還有一個是查看partion
[zk: 127.0.0.1:2181(CONNECTED) 3] get /brokers/topics/test1/partitions/0
null
cZxid = 0x700000066
ctime = Wed Feb 28 14:33:35 CST 2018
mZxid = 0x700000066
mtime = Wed Feb 28 14:33:35 CST 2018
pZxid = 0x700000067
cversion = 1
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 1