在 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中文网其他相关文章!

Golang更适合高并发任务,而Python在灵活性上更有优势。1.Golang通过goroutine和channel高效处理并发。2.Python依赖threading和asyncio,受GIL影响,但提供多种并发方式。选择应基于具体需求。

Golang和C 在性能上的差异主要体现在内存管理、编译优化和运行时效率等方面。1)Golang的垃圾回收机制方便但可能影响性能,2)C 的手动内存管理和编译器优化在递归计算中表现更为高效。

selectgolangforhighpperformanceandcorrency,ifealforBackendServicesSandNetwork程序; selectpypypythonforrapiddevelopment,dataScience和machinelearningDuetoitsverserverserverserversator versator anderticality andextility andextentensivelibraries。

Golang和Python各有优势:Golang适合高性能和并发编程,Python适用于数据科学和Web开发。 Golang以其并发模型和高效性能着称,Python则以简洁语法和丰富库生态系统着称。

Golang和Python分别在哪些方面更易用和学习曲线更平缓?Golang更适合高并发和高性能需求,学习曲线对有C语言背景的开发者较平缓。Python更适合数据科学和快速原型设计,学习曲线对初学者非常平缓。

Golang和C 在性能竞赛中的表现各有优势:1)Golang适合高并发和快速开发,2)C 提供更高性能和细粒度控制。选择应基于项目需求和团队技术栈。

Golang适合快速开发和并发编程,而C 更适合需要极致性能和底层控制的项目。1)Golang的并发模型通过goroutine和channel简化并发编程。2)C 的模板编程提供泛型代码和性能优化。3)Golang的垃圾回收方便但可能影响性能,C 的内存管理复杂但控制精细。

GoimpactsdevelopmentPositationalityThroughSpeed,效率和模拟性。1)速度:gocompilesquicklyandrunseff,ifealforlargeprojects.2)效率:效率:ITScomprehenSevestAndArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdEcceSteral Depentencies,增强开发的简单性:3)SimpleflovelmentIcties:3)简单性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

记事本++7.3.1
好用且免费的代码编辑器

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

WebStorm Mac版
好用的JavaScript开发工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),