1 在windows系统上配置java环境

kafka服务依赖于java,所以第一步需要在Windows上配置java环境,这里就不赘述了。

2 下载kafka

kafka官方下载页面 下载kafka的二进制版本,这里以kafka_2.12-2.6.2版本为例。

Windows下搭建kafka服务器-StubbornHuang Blog

下载之后解压,windows系统的相关脚本主要在bin/windows目录下。需要注意的是,最好解压到英文目录下,并且目录层级不能太深 。Kafka包已经自带了Zookeeper,因此不必另外下载。

3 启动kafka服务

我们需要先启动zookeeper,然后再启动kafka服务,首先进入到kafka的解压目录

cd E:\software\kafka\kafka_2.12-2.6.2

然后首先启动zookeeper服务

bin\windows\zookeeper-server-start.bat config\zookeeper.properties

然后启动kafka服务

bin\windows\kafka-server-start.bat config\server.properties

3.1 Windows 11 启动kafka服务报'wmic' 不是内部或外部命令,也不是可运行的程序 或批处理文件错误

在Windows 11的高版本,由于在系统中移除了wmic,所以在启动低版本kafka服务时会出现以下错误

'wmic' 不是内部或外部命令,也不是可运行的程序 或批处理文件错误

这个时候我们只需要将bin/windows/kafka-server-start.bat这个文件的内容从

@echo off
rem Licensed to the Apache Software Foundation (ASF) under one or more
rem contributor license agreements.  See the NOTICE file distributed with
rem this work for additional information regarding copyright ownership.
rem The ASF licenses this file to You under the Apache License, Version 2.0
rem (the "License"); you may not use this file except in compliance with
rem the License.  You may obtain a copy of the License at
rem
rem     http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License.

IF [%1] EQU [] (
    echo USAGE: %0 server.properties
    EXIT /B 1
)

SetLocal
IF ["%KAFKA_LOG4J_OPTS%"] EQU [""] (
    set KAFKA_LOG4J_OPTS=-Dlog4j.configuration=file:%~dp0../../config/log4j.properties
)
IF ["%KAFKA_HEAP_OPTS%"] EQU [""] (
    rem 64-bit OS
    set KAFKA_HEAP_OPTS=-Xmx1G -Xms1G
)
"%~dp0kafka-run-class.bat" kafka.Kafka %*
EndLocal

修改为

@echo off
rem Licensed to the Apache Software Foundation (ASF) under one or more
rem contributor license agreements.  See the NOTICE file distributed with
rem this work for additional information regarding copyright ownership.
rem The ASF licenses this file to You under the Apache License, Version 2.0
rem (the "License"); you may not use this file except in compliance with
rem the License.  You may obtain a copy of the License at
rem
rem     http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License.

IF [%1] EQU [] (
    echo USAGE: %0 server.properties
    EXIT /B 1
)

SetLocal
IF ["%KAFKA_LOG4J_OPTS%"] EQU [""] (
    set KAFKA_LOG4J_OPTS=-Dlog4j.configuration=file:%~dp0../../config/log4j.properties
)
IF ["%KAFKA_HEAP_OPTS%"] EQU [""] (
    rem detect OS architecture
    wmic os get osarchitecture | find /i "32-bit" >nul 2>&1
    IF NOT ERRORLEVEL 1 (
        rem 32-bit OS
        set KAFKA_HEAP_OPTS=-Xmx512M -Xms512M
    ) ELSE (
        rem 64-bit OS
        set KAFKA_HEAP_OPTS=-Xmx1G -Xms1G
    )
)
"%~dp0kafka-run-class.bat" kafka.Kafka %*
EndLocal

保存退出,然后重启使用启动命令启动服务即可。

4 kafka使用

4.1 创建topic

在kafka中创建一个topic,可使用以下命令

bin\windows\kafka-topics.bat --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic topic名字

4.2 查看topic列表

bin\windows\kafka-topics.bat --list --bootstrap-server localhost:9092

参考链接