mac(Linux)安装配置kafka环境

时间:2021-11-30 21:34:38

mac(Linux)安装配置kafka环境

导读

本文介绍如何在Mac系统上安装和配置kafka环境,也适用于Linux系统,强烈不建议在Windows环境下尝试本博客。

软件环境说明如下:

  1. 操作系统:OS X 10.10.3
  2. JDK版本: 1.8
  3. zookeeper版本:zookeeper-3.4.9
  4. kafka版本:1.0.0

一、安装包下载

1.1 zookeeper下载

zookeeper官网 :http://zookeeper.apache.org/.

下载截至当前日期(2018-01-13)的zookeeper稳定版本3.4.10,下载地址:http://archive.apache.org/dist/zookeeper/zookeeper-3.4.10/,找到zookeeper-3.4.10.tar.gz 并下载。如果由于网络原因下载速度过慢,可以镜像地址:https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/stable/ 。解压至 /usr/local/Cellar目录下,解压命令:

tar -zxvf zookeeper-3.4.10.tar.gz -C /usr/local/Cellar/

[1.2] kafka下载

kafka官网:http://kafka.apache.org/

kafka 1.0.0下载地址: http://archive.apache.org/dist/kafka/1.0.0/

解压至 /usr/local/Cellar目录下,解压命令:

tar -zxvf kafka_2.12-1.0.0.tgz -C /usr/local/Cellar/

好了,现在 /usr/local/Cellar目录下有两个文件夹:

--/usr/local/Cellar

-- zookeeper-3.4.10

-- kafka_2.12-1.0.0

二、zookeeper配置与启动

### 2.1 配置zookeeper

把zookeeper-3.4.10/conf目录下的zoo_sample.cfg另存为zoo.cfg:

cd  zookeeper-3.4.10/conf/

cp zoo_sample.cfg zoo.cfg

找到dataDir=/tmp/zookeeper,把dataDir修改为自定义目录。当然,也可以保持默认值。这里重新指定dataDir:

dataDir=/usr/local/Cellar/zookeeper-3.4.10/zookeeperData

2.2 启动zookeeper

执行zookeeper-3.4.10/bin目录下的zkServer.sh脚本即可启动zookeeper:

sh zkServer.sh start

如果看到如下输出,说明zookeeper启动成功:

ZooKeeper JMX enabled by default
Using config: /usr/local/Cellar/zookeeper-3.4.10/bin/../conf/zoo.cfg
-n Starting zookeeper ...
STARTED

zookeeper启动后 ,默认端口为2181.

三、kafka配置与启动

3.1 配置kafka

3.1.1 连接zookeeper

打开kafka_2.12-1.0.0/config/server.properties,找到zookeeper.connect=localhost:2181,这里的ip和端口要和zookeeper保持一致。

3.1.2 配置log.dirs

server.properties中找到log.dirs,修改为自定义目录:

log.dirs=/usr/local/Cellar/kafka_2.12-1.0.0/kafka-logs

3.2 启动kafka

 sh bin/kafka-server-start.sh  config/server.properties &

此时可以用jps查看进程信息:

$ jps
1635 Jps
1404 Kafka
988 QuorumPeerMain

3.3 创建topic

创建一个topic和数据库里面建一个库是类似的(初次接触,暂时这么理解),测试命令:

 sh bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic testtopic

命令里面 --create表示创建,--zookeeper localhost:2181 指定zookeeper的ip和端口, --replication-factor 1表示topic的副本为1,--partitions 1表示分区为1,--topic testtopic表示创建一个名为testtopic的topic.

执行完以上命令,如果看到如下输出,说明topic创建成功:

......
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
Created topic "testtopic".
[2018-01-14 00:24:06,526] INFO [ReplicaFetcherManager on broker 0] Removed fetcher for partitions testtopic-0 (kafka.server.ReplicaFetcherManager)
......

3.4 启动生产者

执行以下命令,启动生产者:

$ sh bin/kafka-console-producer.sh --broker-list localhost:9092 --sync --topic testtopic

输出如下:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/usr/local/Cellar/hbase-1.2.4/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/usr/local/Cellar/kafka_2.12-1.0.0/libs/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]

3.5 启动消费者

执行以下命令,启动消费者:

sh bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic testtopic --from-beginning