两种方式一种免安装版,一种安装版 免安装版已经吧命令默认加入全局命令,无需指定启动脚本。
两种方式都需要zookeeper,因为kafka依赖使用zookeeper具体就不再这里赘述。
1 安装版:
1.1 使用brew安装zookeeper
brew install zookeeper
zookeeper安装目录:/usr/local/Cellar/zookeeper/
zookeeper的配置文件位置:/usr/local/etc/zookeeper/zookeeper.properties
1.2 使用brew安装kafka
brew install kafka
kafka安装路径:/usr/local/Cellar/kafka/
kafka的配置文件位置:/usr/local/etc/kafka/server.properties
1.2.1 修改kafka配置
文件位置 /usr/local/etc/kafka/server.properties
listeners=PLAINTEXT://:9092修改为listeners=PLAINTEXT://localhost:9092
1.3 启动
需要先启动zookeeper,之后启动kafka
1.3.1 启动zookeeper
后台启动运行
nohup zookeeper-server-start /usr/local/etc/kafka/zookeeper.properties &
1.3.2 启动kafka
后台启动运行
nohup kafka-server-start /usr/local/etc/kafka/server.properties &
1.4 使用
1.4.1 创建topic
kafka-topics --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
1.4.2 查看已创建主题
kafka-topics --list --zookeeper localhost:2181
1.4.3 创建消息生产者(消息发送方)
kafka-console-producer -topic test --broker-list localhost:9092
1.4.4 创建消息消费者(消息监听方)
需要新建一个终端窗口
kafka-console-consumer --bootstrap-server localhost:9092 -topic test
发送消息
接收消息
2 免安装版
2.1 下载压缩包
地址:https://www.apache.org/dyn/closer.cgi?path=/kafka/0.8.1.1/kafka_2.9.2-0.8.1.1.tgz
2.2 解压
tar -xzf kafka_2.9.2-0.8.1.1.tgz
cd kafka_2.9.2-0.8.1.1
2.3 启动zookeeper服务器
可以把压缩文件复制到任意地方,然后进入根目录
复制到software目录下 没有的话自己创建或者自己选择合适的目录
cp ~/Downloads/kafka_2.9.2-0.8.1.1 /Users/software/kafka_2.9.2-0.8.1.1
进入根目录
cd /Users/software/kafka_2.9.2-0.8.1.1
启动zookeeper
./bin/zookeeper-server-start.sh config/zookeeper.properties
2.4 启动kafka
./bin/kafka-server-start.sh config/server.properties
2.4 创建topic
./bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
2.5 查看已存在的topic
./bin/kafka-topics.sh --list --zookeeper localhost:2181
2.6 创建消息生产者
./bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test
2.7 创建消息消费者
./bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
免安装版使用方式和安装版使用方式一样我就不上图了。