Kafka kafka在windows下的安装与配置

2020-07-16 15:29:14 浏览数 (1)

kafka在windows下的安装与配置

By: 授客 QQ:1033553122

1.测试环境.................................................................................................................

2.安装JAVA JDK........................................................................................................

3.安装zookeeper......................................................................................................

4.安装KafKa..............................................................................................................

5.测试.........................................................................................................................

1.测试环境

JDK 1.8.0_121

zookeeper-3.4.13.tar.gz

下载地址1:

http://zookeeper.apache.org/releases.html#download

https://www.apache.org/dyn/closer.cgi/zookeeper/

https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/

下载地址2:

https://pan.baidu.com/s/1dnBgHvySE9pVRZXJVmezyQ

kafka_2.12-2.1.0.tgz

下载地址1:

http://kafka.apache.org/downloads.html

下载地址2:

https://pan.baidu.com/s/1VnHkJgy4iQ73j5rLbEL0jw

2.安装JAVA JDK

3.安装zookeeper

1)解压下载的软件包zookeeper-3.4.13.tar.gz 至目标目录(例中为:D:Program Fileszookeeper-3.4.13)

2)修改配置文件

进入conf目录(例中为:D:Program Fileszookeeper-3.4.13conf), 复制“zoo_sample.cfg”为“zoo.cfg”文件,编辑zoo.cfg,

查找并设置dataDir,设置数据存储目录,如下:

dataDir=D:\Program Files\zookeeper-3.4.13\tmpzookeeper

查找并设置clientPort(有必要的话),设置客户端连接端口,默认端口2181,如下:

clientPort=2181

3)配置系统环境变量

添加系统环境变量:ZOOKEEPER_HOME,设置对应值(例中配置:ZOOKEEPER_HOME= D:Program Fileszookeeper-3.4.13

编辑path系统变量,添加路径:%ZOOKEEPER_HOME%bin

4)验证

打开cmd控制台窗口,输入“zkServer“,运行Zookeeper

4.安装KafKa

1) 解压kafka_2.12-2.1.0.tgz至目标目录(例中为:D:kafka_2.12-2.1.0

2) 修改配置

进入到配置目录(例中为:D:kafka_2.12-2.1.0config),编辑server.properties文件,

查找并设置log.dirs,配置日志目录,例中配置如下:

log.dirs=D:\kafka_2.12-2.1.0\tmp\kafka-logs

查找并设置zookeeper.connect,配置zookeeper连接字符串,格式:ip1:端口1,ip2:端口2,……,ipN:端口N,比如127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002,每对ip和端口分别代表一个zookeeper服务器,kafka会按这里的配置去连接zookeeper,例中配置如下:

zookeeper.connect=127.0.0.1:2181

查找并设置listener,配置监听端口,格式:listeners = listener_name://host_name:port,供kafka客户端连接用的ip和端口,例中配置如下:

listeners=PLAINTEXT://127.0.0.1:9092

3)启动kafka

进入到kafka解压目标目录(bin目录的父目录),执行 .binwindowskafka-server-start.bat .configserver.properties,启动kafka,如下:

实践过程中,执行命令后有遇到以下问题:

问题:

.binwindowskafka-server-start.bat .configserver.properties

此时不应有 myeclipseMyEclipse。

解决方法:

去掉我本机之前配置的一个环境变量(TESTNG_HOME=D:Program Files (x86)myeclipseMyEclipse 10dropinsorg.testng.eclipse_6.8.6.20130607_0745libtestng.jar

)后搞定

问题:

.binwindowskafka-server-start.bat ..configserver.properties

命令语法不正确。

错误: 找不到或无法加载主类 FilesJavajdk1.8.0_121libdt.jar;D:Program

解决方法:

编辑D:kafka_2.12-2.1.0binwindowskafka-run-class.bat文件,

找到

set COMMAND=%JAVA% %KAFKA_HEAP_OPTS% %KAFKA_JVM_PERFORMANCE_OPTS% %KAFKA_JMX_OPTS% %KAFKA_LOG4J_OPTS% -cp %CLASSPATH% %KAFKA_OPTS% %*

替换这里的%CLASSPATH % 为 "%CLASSPATH %"

5.测试

1)创建主题

.binwindowskafka-topics.bat --create --zookeeper 127.0.0.1:2181 --replication-factor 1 --partitions 4 --topic MY_TOPIC1

WARNING: Due to limitations in metric names, topics with a period ('.') or underscore ('_') could collide. To avoid issues it is best to use either, but not both.

Created topic "MY_TOPIC1".

.binwindowskafka-topics.bat --create --zookeeper 127.0.0.1:2181 --replication-factor 1 --partitions 4 --topic MY_TOPIC2

WARNING: Due to limitations in metric names, topics with a period ('.') or underscore ('_') could collide. To avoid issues it is best to use either, but not both.

Created topic "MY_TOPIC2".

2)查看创建的主题

.binwindowskafka-topics.bat --zookeeper 127.0.0.1:2181 --list

MY_TOPIC1

MY_TOPIC2

2)生产消息

.binwindowskafka-console-producer.bat --broker-list localhost:9092 --topic MY_TOPIC1

>Hello, 2019

>Hello,2020

>Hello,2021

3) 消费消息

.binwindowskafka-console-consumer.bat --bootstrap-server localhost:9092 --topic MY_TOPIC1 --from-beginning

Hello, 2019

.binwindowskafka-console-consumer.bat --bootstrap-server localhost:9092 --topic MY_TOPIC1 --group MY_GROUP1

Hello,2020

D:kafka_2.12-2.1.0>.binwindowskafka-console-consumer.bat --bootstrap-server localhost:9092 --topic MY_TOPIC1 --group MY_GROUP2

Hello,2021

说明:

--bootstrap-server 指定需要连接的服务器

--group 指定消费者所属消费组

--topic 指定消费者要消费的主题

--from-beginning 从头开始接收数据,可以理解为offset为0

注:实践中,生产每条消息前,仅开启一个消费者客户端进行消费

0 人点赞