一、前提
1、kafka安裝包下載:http://kafka.apache.org/downloads
2、jdk已安裝
3、scala已安裝
4、zookeeper集群已安裝并運行
二、步驟
1、對kafka_2.9.2-0.8.1.tgz進行解壓縮:tar -zxvf kafka_2.9.2-0.8.1.tgz。
2、對kafka目錄進行改名:mv kafka_2.9.2-0.8.1 kafka
3、配置kafka
vi /usr/local/kafka/config/server.properties
broker.id:依次增長的整數,0、1、2、3、4,集群中Broker的唯一id
zookeeper.connect=192.168.1.107(BD01):2181,192.168.1.108:2181,192.168.1.109:2181
(用域名更好,萬一測試環境ip地址變了,可以不用改配置)
三、啟動
#從后臺啟動Kafka集群(3臺都需要啟動)
cd ? ?/opt/kafka/kafka_2.11-0.9.0.1//bin#進入到kafka的bin目錄
./kafka-server-start.sh -daemon ../config/server.properties
四、測試
集群中任選一臺,如BD03,進入kafka\bin文件夾下
輸入命令
#創建主題
./kafka-topics.sh --zookeeper BD03:2181,BD04:2181,BD05:2181 --topic TestTopic --replication-factor 1 --partitions 1 --create
集群中任選一臺,如BD04,進入kafka\bin文件夾下
輸入命令
#創建生產者
./kafka-console-producer.sh --broker-list BD03:9092,BD04:9092,BD05:9092 --topic TestTopic
集群中任選一臺,如BD05,進入kafka\bin文件夾下
輸入命令
#創建消費者
./kafka-console-consumer.sh --zookeeper BD03:2181,BD04:2181,BD05:2181 --topic TestTopic --from-beginning
----------------
BD04 生產者BD05 消費者
五、關閉
以下參考了:http://blog.csdn.net/M_SIGNALs/article/details/53201595
似乎有一個是用來關閉服務的,”kafka-server-stop.sh“,于是我們運行這個腳本:
[root@master bin]# kafka-server-stop.sh
No kafka server to stop[root@master bin]#
- 1
- 2
- 3
- 4
what ? 沒有服務要被關閉?我們可以看一下這個腳本到底是怎么寫的,是不是我們的參數不正確還是怎么滴。
[root@master bin]# cat kafka-server-stop.sh
#!/bin/sh
# 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.
PIDS=$(ps ax | grep -i 'kafka\.Kafka' | grep java | grep -v grep | awk '{print $1}')if [ -z "$PIDS" ]; thenecho "No kafka server to stop"exit 1
else kill -s TERM $PIDS
fi[root@master bin]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
好吧,這么看來也就是我們用這樣的方法是不行了。干脆直接 kill -9
[root@master kafka_2.11-0.10.1.0]# jps
3448 Kafka
2136 NodeManager
3033 QuorumPeerMain
1772 DataNode
5757 Jps
3711 Kafka
[root@master kafka_2.11-0.10.1.0]# kill -9 3448
...
[1]- Killed kafka-server-start.sh server4.properties
...
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
就是這么簡單粗暴。