Heroku Go 应用崩溃了。

huangapple go评论105阅读模式
英文:

Heroku Go app crashing

问题

根据您提供的信息,您在部署应用程序到Heroku后遇到了503错误,并且出现了启动超时的错误(R10)。您想知道如何更好地调试并修复应用程序以使其正常运行。

要解决这个问题,您可以尝试以下几个步骤:

  1. 检查应用程序的日志:根据错误消息中的提示,您可以检查Heroku上的应用程序日志,以获取更多详细信息。您可以使用Heroku命令行工具或Heroku的Web界面来查看日志。

  2. 确保应用程序绑定到正确的端口:根据日志中的信息,应用程序似乎无法在启动后的60秒内绑定到$PORT。请确保您的应用程序代码正确地将应用程序绑定到Heroku提供的环境变量$PORT上的端口。

  3. 检查应用程序的依赖项和配置:确保您的应用程序在部署到Heroku之前具有正确的依赖项和配置。可能有某些依赖项在部署过程中无法满足,导致应用程序无法正常启动。

  4. 尝试重新部署应用程序:如果以上步骤都没有解决问题,您可以尝试重新部署应用程序。有时候重新部署可以解决由于部署过程中的某些问题导致的错误。

希望这些步骤能帮助您更好地调试和修复应用程序的问题。如果问题仍然存在,请提供更多详细信息,以便我能够提供更具体的帮助。

英文:

Following this tutorial, everything works locally. After I deploy my app to Heroku and visit the app on the browser I get a 503 Error and the message:

> Application Error
An error occurred in the application and your page could not be served. Please try again in a few moments.
If you are the application owner, check your logs for details.

The logs say:

2015-09-08T16:31:53.976824+00:00 heroku[web.1]: State changed from crashed to starting
2015-09-08T16:31:56.174376+00:00 heroku[web.1]: Starting process with command `mywebsite`
2015-09-08T16:31:59.312461+00:00 app[web.1]: Listening on port: 39461
2015-09-08T16:32:56.471550+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2015-09-08T16:32:56.471550+00:00 heroku[web.1]: Stopping process with SIGKILL
2015-09-08T16:32:57.390752+00:00 heroku[web.1]: Process exited with status 137
2015-09-08T16:32:57.404208+00:00 heroku[web.1]: State changed from starting to crashed
2015-09-08T16:32:57.645135+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=boiling-eyrie-6897.herokuapp.com request_id=ec26... fwd="xx.xxx.xxx.xxx" dyno= connect= service= status=503 bytes=
2015-09-08T16:32:58.233774+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=boiling-eyrie-6897.herokuapp.com request_id=ef40...fwd="xx.xxx.xxx.xxx" dyno= connect= service= status=503 bytes=

I understand what the errors are, but how can such a tiny tutorial app be causing a boot timeout (R10)?

How can I debug this better and fix the app so it runs?

答案1

得分: 9

当你通过Heroku部署应用程序时,它不允许你指定端口号
换句话说,你不能将你的网络服务的端口号指定为8000或其他任何值,Heroku会在运行时决定端口号。
因此,你不能使用以下代码:

    log.Fatal(http.ListenAndServe(":8000", router))

你可以做的是获取Heroku的运行时端口。
简而言之,只需使用以下代码

    log.Fatal(http.ListenAndServe(":" + os.Getenv("PORT"), router))
英文:

When you deploy an app through heroku, it does not allow you to specify the port number.
In other words, you can not specify your web service's port number as 8000 or something else, heroku decides the port number in runtime.
so, you can not use the following code:

    log.Fatal(http.ListenAndServe(":8000", router))

What you can do is, getting the runtime port of heroku.
In short, just use the following code:

    log.Fatal(http.ListenAndServe(":" + os.Getenv("PORT"), router))

答案2

得分: 7

你的应用程序需要监听所有网络连接。如果它只监听本地主机(localhost),Heroku的进程监视器将无法检测到你绑定了端口,也无法向你的应用程序发送请求。

这意味着你需要将代码从:

http.ListenAndServe("127.0.0.1:"+port, nil)

改为:

http.ListenAndServe(":"+port, nil)

另请参阅Heroku的Go应用程序入门指南:https://github.com/heroku/go-getting-started/blob/master/cmd/go-getting-started/main.go#L27

英文:

Your app needs to listen to all network connections. If it only listens on localhost, heroku's process watcher will not be able to detect that you bound the port, nor send requests to your app.

That means instead of:

http.ListenAndServe("127.0.0.1:"+port, nil)

You need to call:

http.ListenAndServe(":"+port, nil)

See also the heroku getting started with Go app: https://github.com/heroku/go-getting-started/blob/master/cmd/go-getting-started/main.go#L27

huangapple
  • 本文由 发表于 2015年9月9日 00:39:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/32463004.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定