這篇博客是用來記錄用go安裝及操作kafka庫的時候踩到的坑~
安裝kafka庫
首先我參考了博客:https://blog.csdn.net/tflasd1157/article/details/81985722和https://blog.csdn.net/u011596455/article/details/80073841
在go get github.com/Shopify/sarama安裝過程中出現了
package golang.org/x/net/proxy: unrecognized import path "golang.org/x/net/proxy" (https fetch: Get https://golang.org/x/net/proxy?go-get=1: dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)
起初我以為是被牆了的原因,后來借用同學的ssrr節點翻牆后還是沒能解決。
直到我看到這篇博客:https://www.jianshu.com/p/a55ba2ae6507
go get的時候遇到這個unrecognized import path "golang.org/x/net/html"提示,因為golang被大清牆了。
因此我們只能從github上拿到這部分包,放入項目中。命令:
git clone https://github.com/golang/net
也可手動下載后解壓
在gopath目錄的src文件夾內建立如下目錄 golang.org/x/net,將上面下載的net里面的文件放到該net目錄中即可!
隨后繼續go get,然后又是報這個錯。。
# github.com/DataDog/zstd
cc1.exe: sorry, unimplemented: 64-bit mode not compiled in
意思是mingw需要下載64位版本,而系統自帶的是32位,所以又參考惹這篇博客安裝mingw64:https://www.cnblogs.com/ggg-327931457/p/9694516.html
安裝之前的gcc版本

還沒安裝又彈出個錯誤

折騰了半天試了各種方法還是不好使最后結果把校園網換成熱點竟然成了。。成了。。。。
安裝好之后修改配置變量,把其他所有有關mingw配置全刪了,替換成剛下載的mingw64/bin

繼續執行go get github.com/Shopify/sarama
順利安裝成功~
go操作kafka
首先啟動zookeeper和kafka
創建了一個main.go
package main import ( "fmt" "github.com/Shopify/sarama" ) func main() { config := sarama.NewConfig() config.Producer.RequiredAcks = sarama.WaitForAll config.Producer.Partitioner = sarama.NewRandomPartitioner config.Producer.Return.Successes = true msg := &sarama.ProducerMessage{} msg.Topic = "test"//topic沒有的話會新建 msg.Value = sarama.StringEncoder("this is a good test,my message is zhaofan") client, err := sarama.NewSyncProducer([]string{"192.168.0.118:9092"}, config) if err != nil { fmt.Println("producer close err:", err) return } defer client.Close() pid, offset, err := client.SendMessage(msg) if err != nil { fmt.Println("send message failed,", err) return } fmt.Printf("pid:%v offset:%v\n", pid, offset) }
go run main.go后消費者執行
kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --from-beginning
成功
