一、前提
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
就是这么简单粗暴。