Mac环境安装Kafka
Kafka依赖于zookeeper,以下方式安装Kafka后自带zk
brew install kafka
# Kafka启动方式
To restart kafka after an upgrade:
brew services restart kafka
Or, if you don't want/need a background service you can just run:
/usr/local/opt/kafka/bin/kafka-server-start /usr/local/etc/kafka//usr/local/etc/kafka/server.properties
# Kafka安装目录
==> Summary
🍺 /usr/local/Cellar/kafka/3.2.0: 200 files, 99.4MB
# 配置文件路径
/usr/local/etc/kafka/
kafka取消自动创建topic,编辑/usr/local/etc/kafka/server.properties,auto.create.topics.enable=false
启动zookeeper与kafka
启动zkbrew services start zookeeper
默认端口2181,启动kafkabrew services start kafka
,默认端口9092。如需要修改默认端口号可以在/usr/local/etc/kafka/中zookeeper.properties和server.properties中对应修改即可。
创建Topic
说明:在较新版本(2.2 及更高版本)的 Kafka 不再需要 ZooKeeper 连接字符串,即- -zookeeper localhost:2181。使用 Kafka Broker的 --bootstrap-server localhost:9092来替代- -zookeeper localhost:2181。
cd /usr/local/Cellar/kafka/3.2.0/bin
# 创建topic
./kafka-topics --bootstrap-server localhost:9092 --create --topic domancy --partitions 2 --replication-factor 1
./kafka-topics --bootstrap-server localhost:9092 --create --topic order --partitions 2 --replication-factor 1
# 查看topic
./kafka-topics --bootstrap-server localhost:9092 --describe --topic domancy
# 删除topic
./kafka-topics --bootstrap-server localhost:9092 --delete --topic domancy
测试Kakfa
生产者
➜ /usr/local/Cellar/kafka/3.2.0/bin/kafka-console-producer --broker-list localhost:9092 --topic domancy
>白日依山尽
>黄河入海流
>欲穷千里目
>更上一层楼
>
消费者
➜ bin kafka-console-consumer --bootstrap-server localhost:9092 --topic domancy --from-beginning
白日依山尽
黄河入海流
欲穷千里目
更上一层楼
Springboot项目
pom文件
<dependencies>
<!-- 高版本的SpringBoot需要应用spring-boot-starter-web包,
不然会出现AbstractDiscoveryClientOptionalArgs不存在异常-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>