【问题标题】:Web process failed to bind to $PORT within 60 seconds of launch golangWeb 进程在启动 golang 后 60 秒内无法绑定到 $PORT
【发布时间】:2019-07-12 16:43:53
【问题描述】:

我使用golang开发了一个rest api。我已经将存储库推送到了Heroku。我还在互联网上托管了一个Mysql Server。Go rest api连接到Mysql Server并获取和插入数据。

heroku 上的应用程序昨天运行良好。但是今天早上我在“heroku logs --tail”上收到此错误:Web process failed to bind to $PORT in 60 seconds of launch

这是 main.go 的代码:

package main

import (
    "os"
    "github.com/mingrammer/go-todo-rest-api-example/app"
    "github.com/mingrammer/go-todo-rest-api-example/config"
)

func main() {
    config := config.GetConfig()

    app := &app.App{}
    app.Initialize(config)
    port, ok := os.LookupEnv("PORT")

    if ok == false {
        port = "3000"
    }

    app.Run(":"+port)
}

【问题讨论】:

    标签: go heroku mux


    【解决方案1】:

    Getenv 没有返回错误。

    不好的做法

    port, err := os.Getenv("PORT")
    if err != nil {
        port = "3000"
    }
    

    port := os.Getenv("PORT")
    app.Run(":"+port)
    

    文档 GETENV Getenv 检索由键命名的环境变量的值。 它返回值,如果变量不存在,该值将为空。 要区分空值和未设置值,请使用 LookupEnv。

    【讨论】:

      【解决方案2】:

      您可以为此使用os.Getenv

      修改后的main.go代码如下:

      package main
      
      import (
          "os"
          "github.com/mingrammer/go-todo-rest-api-example/app"
          "github.com/mingrammer/go-todo-rest-api-example/config"
      )
      
      func main() {
          config := config.GetConfig()
      
          app := &app.App{}
          app.Initialize(config)
          port, err := os.Getenv("PORT")
          if err != nil {
              port = "3000"
          } 
      
          app.Run(":"+port)
      }
      

      【讨论】:

        【解决方案3】:

        我用

        解决了这个问题

        os.Getenv("PORT")

        【讨论】:

          【解决方案4】:

          这是 Heroku 上 node.js 应用程序的已知问题。

          在极少数情况下,您的应用可能正在使用 process.env.PORT,但仍可能无法绑定。这可能是由于应用程序尝试在 localhost 上绑定造成的。相反,您可能需要将其更改为 0.0.0.0。

          可以假设 Go 服务也可能存在同样的问题。在Heroku support site 上查看这篇文章。

          【讨论】:

            猜你喜欢
            • 2014-03-15
            • 1970-01-01
            • 2021-09-08
            • 1970-01-01
            • 2020-02-10
            • 1970-01-01
            • 2016-08-09
            • 1970-01-01
            • 2013-03-19
            相关资源
            最近更新 更多