【问题解决】Mac M1 安装Kafka并运行Golang(非M1芯片不会出现这个问题)

911 阅读1分钟

1.用brew安装

brew install kafka # 时间比较久,要耐心

2.安装后启动服务

  • Kafka依赖 Zookeeper,过程中会顺带安装 Zookeeper

  • 启动 Kafka前,要先启动 Zookeeper

  • 启动 Zookeeper

    brew restart zookeeper
    
  • 启动 Kafka

    brew restart kafka
    

3. 检查是否启动成功

# 创建 Topic
kafka-topics --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic mymtopic
​
# 查看 Topic
kafka-topics --bootstrap-server localhost:9092 --list
mymtopic
​
# 监听当前 Topic 消息
kafka-console-consumer --bootstrap-server localhost:9092 --topic mymtopic
​
# 查看 Topic 配置信息
kafka-topics --bootstrap-server localhost:9092 --describe  --topic mymtopic
Topic: mymtopic TopicId: ouzL8Jf9RuqcYMkYnRqENQ PartitionCount: 1 ReplicationFactor: 1  Configs: segment.bytes=1073741824
  Topic: mymtopic Partition: 0  Leader: 0 Replicas: 0 Isr: 0

4.运行Golang程序,报错

git:(master) ✗ go run main.go
# command-line-arguments
/opt/homebrew/Cellar/go/1.19.1/libexec/pkg/tool/darwin_arm64/link: running clang failed: exit status 1
ld: warning: ignoring file /Users/mym/go/pkg/mod/github.com/confluentinc/confluent-kafka-go@v1.6.1/kafka/librdkafka_vendor/librdkafka_darwin.a, building for macOS-arm64 but attempting to link with file built for macOS-x86_64

5. 谷歌搜索找解决方案

发现这个issues:github.com/confluentin…(虽然这个issues没有关闭,但里面有解决方法,总结如下)

  • 安装 librdkafka

    brew install librdkafka
    
  • 配置环境变量

    export PKG_CONFIG_PATH="/opt/homebrew/Cellar/openssl@1.1/1.1.1q/lib/pkgconfig"
    

6. 加上-tags dynamic重新运行Golang程序

git:(master) ✗ go run -tags dynamic main.go

正常了!亲测有效,不过原理不是完全清楚,有兄弟明白的话,评论让我学习一下吧