Prometheus官網文檔中編譯random示例時go語言報錯的解決辦法

在Prometheus官網的GETTING STARTED 文檔中,有個用go語言開發的client_golang的例子,這段示例執行後,可以模擬三個被監控的目標。

代碼所屬的章節如下:

Starting up some sample targets

Download the Go client library for Prometheus and run three of these example processes:

相關命令行如下:

# Fetch the client library code and compile example.
git clone https://github.com/prometheus/client_golang.git
cd client_golang/examples/random
go get -d
go build

# Start 3 example targets in separate terminals:
./random -listen-address=:8080
./random -listen-address=:8081
./random -listen-address=:8082

下載並安裝安裝了golang和git後,就可以執行上面的代碼了。Prometheus官網給出的示例是linux操作系統的,我的是windows環境,所以我在windows PowerShell下執行,除了提示符不一樣,命令都一樣。

git clone https://github.com/prometheus/client_golang.git
cd client_golang/examples/random 

前兩行都正常,沒有報錯。

當執行到 go get -d 命令時,報了類似下面的錯:

Fetching https://golang.org/x/net?go-get=1 https fetch failed: Get https://golang.org/x/net?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. package golang.org/x/net/...: unrecognized import path "golang.org/x/net/..." 

報錯原因是由於被牆了,無法連接到https://golang.org/x/net...

由於從未學習過go語言,無任何基礎,在網上一通搜索,發現此類問題最常用的的解決方法是:

在src目錄下面構造目錄結構
$mkdir -p $GOPATH/src/golang.org/x/
$cd $GOPATH/src/golang.org/x/
$git clone 
https://github.com/golang/net.git net
$go install net
執行go install之後沒有提示,就說明安裝好了。

對於Prometheus的這個示例,這個方法無效。又用go env命令檢查了環境變量設置,主要看了GOPATH也沒發現有什麼問題。

後來在client_golang下看到一個go.mod裏有所有get的的依賴包,go.mod的文件內容如下:

module github.com/prometheus/client_golang

require (
    github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973
    github.com/golang/protobuf v1.2.0
    github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
    github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910
    github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce
    github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d
    golang.org/x/net v0.0.0-20181114220301-adae6a3d119a
    golang.org/x/sync v0.0.0-20181108010431-42b317875d0f // indirect
)

其中後面兩行,跟執行 go get -d 命令時的報錯內容關鍵字完全匹配。懷疑問題出在這兒。然後上網又搜了一通,找到了下面的內容:

go mod命令

隨着Go 1.11的發佈,go開始支持mod子命令

這個命令解決了Go依賴更新的問題。剛開始學起來有一點難度,不過學會了之後,會發現這玩意比原來的 vendor好太多。
官方還專門弄了一個
https://goproxy.io網站,專門方便我們這些國內的用戶。

然後到https://goproxy.io網站一看,頁面及其簡潔,除了標題,就只有下面幾句,也沒有鏈接:

 

A global proxy for go modules 

First, you will need to enable the Go Modules feature and configure Go to use the proxy.

export GOPROXY=https://goproxy.ioOr$env:GOPROXY = "https://goproxy.io"Now, when you build and run your applications, go will fetch dependencies via goproxy.io.

Note: This proxy can't fetch your private repos of course.

  其核心就是配置GOPROXY環境變量,把GOPROXY配置爲https://goproxy.io。我的是windows環境,所以就在PowerShell命令行執行了$env:GOPROXY = "https://goproxy.io"。

 設置好GOPROXY後,再執行 go get -d 和 以後的命令都沒有再報錯。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章