在Heroku 上部署golang 應用程式:建置成功,但應用程式顯示錯誤
問題
使用godep 支援在Heroku 上部署Go 應用程式程式時,建置成功,但存取端點會導致“應用程式錯誤”。
程式碼和日誌
應用程式中使用了以下程式碼:
import ( "log" "fmt" "net/http" "os" "github.com/gorilla/mux" "github.com/gorilla/context" "gopkg.in/paytm/grace.v1" ) func main() { log.Println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ CHIT STARTED $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$") log.Println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$") muxRouter := mux.NewRouter() muxRouter.HandleFunc("/", Articles) http.Handle("/", muxRouter) port := os.Getenv("PORT") if port == "" { port = "9000" // Default port if not specified } err := grace.Serve(":" + port, context.ClearHandler(http.DefaultServeMux)) if err != nil { log.Println("[ERROR GRACEFUL]", err) os.Exit(1) } os.Exit(0) } func Articles(w http.ResponseWriter, r *http.Request) { // vars := mux.Vars(r) w.WriteHeader(http.StatusOK) fmt.Fprintf(w, "Hello") // r.Close = true // w.Header().Set("Content-Type", "application/json") // w.Header().Set("Access-Control-Allow-Origin", "*") /*if r.Method == "OPTIONS" { w.WriteHeader(http.StatusOK) return } if err := fn(w, r); err != nil { log.Println(err) apiObject := ConstructAPIError(http.StatusInternalServerError, ErrGeneral, SysMsgErrGeneral, MsgErrGeneral) SendAPIObject(w, apiObject) return }*/ }
部署時的Heroku日誌如下:
-----> Go app detected -----> Fetching jq... done -----> Fetching stdlib.sh.v8... done -----> Checking Godeps/Godeps.json file. -----> New Go Version, clearing old cache -----> Installing go1.12.6 -----> Fetching go1.12.6.linux-amd64.tar.gz... done -----> Running: go install -v -tags heroku ./... bitbucket.org/michaelchandrag/chit/pkg bitbucket.org/michaelchandrag/chit/vendor/github.com/gorilla/context bitbucket.org/michaelchandrag/chit/vendor/github.com/gorilla/mux bitbucket.org/michaelchandrag/chit/vendor/gopkg.in/tylerb/graceful.v1 bitbucket.org/michaelchandrag/chit/vendor/gopkg.in/paytm/grace.v1 bitbucket.org/michaelchandrag/chit/pkg/util bitbucket.org/michaelchandrag/chit Installed the following binaries: ./bin/chit -----> Discovering process types Procfile declares types -> web -----> Compressing... Done: 7.5M -----> Launching... Released v3 https://michaelchandrag-project.herokuapp.com/ deployed to Heroku
應用存取前後的日誌端點是:
2:47.954106+00:00 heroku[web.1]: Starting process with command `chit` 2019-07-08T05:02:49.413453+00:00 app[web.1]: 2019/07/08 05:02:49 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ CHIT STARTED $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 2019-07-08T05:02:49.413476+00:00 app[web.1]: 2019/07/08 05:02:49 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 2019-07-08T05:02:49.413647+00:00 app[web.1]: 2019/07/08 05:02:49 starting serve on :9000 2019-07-08T05:03:48.131507+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch 2019-07-08T05:03:48.131595+00:00 heroku[web.1]: Stopping process with SIGKILL 2019-07-08T05:03:48.214979+00:00 heroku[web.1]: State changed from starting to crashed 2019-07-08T05:03:48.193205+00:00 heroku[web.1]: Process exited with status 137 2019-07-08T10:38:59.721224+00:00 heroku[web.1]: State changed from crashed to starting 2019-07-08T10:39:00.359017+00:00 heroku[web.1]: Starting process with command `chit` 2019-07-08T10:39:02.232435+00:00 app[web.1]: 2019/07/08 10:39:02 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ CHIT STARTED $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 2019-07-08T10:39:02.232458+00:00 app[web.1]: 2019/07/08 10:39:02 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ 2019-07-08T10:39:02.232583+00:00 app[web.1]: 2019/07/08 10:39:02 starting serve on :9000 2019-07-08T10:40:00.462841+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch 2019-07-08T10:40:00.462974+00:00 heroku[web.1]: Stopping process with SIGKILL 2019-07-08T10:40:00.555959+00:00 heroku[web.1]: Process exited with status 137 2019-07-08T10:40:00.573427+00:00 heroku[web.1]: State changed from starting to crashed
解決方案
應用程式啟動但因為它沒有綁定到指定的連接埠而被殺死。從 Heroku 日誌訊息中可以明顯看出:
2019-07-08T05:03:48.131507+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
要解決此問題,網路伺服器必須綁定到 PORT 環境變數指定的連接埠。在 Heroku 上,HTTP 伺服器透過 Heroku 閘道在預設 HTTP 和 HTTPS 連接埠上公開可用。
因此,應用程式應綁定到指定連接埠而不是 :9000。
以上是儘管建置成功,為什麼我在 Heroku 上的 Go 應用程式部署成功,但應用程式卻顯示「應用程式錯誤」?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Go語言使用"encoding/binary"包進行二進制編碼與解碼。 1)該包提供binary.Write和binary.Read函數,用於數據的寫入和讀取。 2)需要注意選擇正確的字節序(如BigEndian或LittleEndian)。 3)數據對齊和錯誤處理也是關鍵,確保數據的正確性和性能。

1)usebybytes.joinforconcatenatinges,2)bytes.bufferforincrementalwriting,3)bytes.indexorbytes.indexorbytes.indexbyteforsearching bytes.bytes.readereforrednorederencretingnchunknunknchunknunk.sss.inc.softes.4)

theencoding/binarypackageingoiseforporptimizingBinaryBinaryOperationsDuetoitssupportforendiannessessandefficityDatahandling.toenhancePerformance:1)usebinary.nativeendiandiandiandiandiandiandiandian nessideendian toavoid avoidByteByteswapping.2)

Go的bytes包主要用於高效處理字節切片。 1)使用bytes.Buffer可以高效進行字符串拼接,避免不必要的內存分配。 2)bytes.Equal函數用於快速比較字節切片。 3)bytes.Index、bytes.Split和bytes.ReplaceAll函數可用於搜索和操作字節切片,但需注意性能問題。

字節包提供了多種功能來高效處理字節切片。 1)使用bytes.Contains檢查字節序列。 2)用bytes.Split分割字節切片。 3)通過bytes.Replace替換字節序列。 4)用bytes.Join連接多個字節切片。 5)利用bytes.Buffer構建數據。 6)結合bytes.Map進行錯誤處理和數據驗證。

Go的encoding/binary包是處理二進制數據的工具。 1)它支持小端和大端字節序,可用於網絡協議和文件格式。 2)可以通過Read和Write函數處理複雜結構的編碼和解碼。 3)使用時需注意字節序和數據類型的一致性,尤其在不同系統間傳輸數據時。該包適合高效處理二進制數據,但需謹慎管理字節切片和長度。

“字節”包裝封裝becapeitoffersefficerSoperationsOnbyteslices,cocialforbinarydatahandling,textPrococessing,andnetworkCommunications.byteslesalemutable,允許forforforforforformance-enhangingin-enhangingin-placemodifications,makaythisspackage

go'sstringspackageIncludeSessentialFunctionsLikeContains,trimspace,split,andreplaceAll.1)contunsefefitedsseffitedsfificeCheckSforSubStrings.2)trimspaceRemovesWhitespaceToeensuredity.3)splitparsentertparsentertparsentertparsentertparstructedtextlikecsv.4)report textlikecsv.4)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver CS6
視覺化網頁開發工具

WebStorm Mac版
好用的JavaScript開發工具