在 AWS 上使用 Elastic Beanstalk 的 Go Web 应用程序未在日志中显示输出

Posted

技术标签:

【中文标题】在 AWS 上使用 Elastic Beanstalk 的 Go Web 应用程序未在日志中显示输出【英文标题】:Go Web app on AWS with Elastic Beanstalk not showing output in logs 【发布时间】:2015-08-06 09:05:30 【问题描述】:

这是一个有深度的问题,所以我会尽力解释它。

我在 Go 中构建了一个小型 API 服务,我正在尝试使用相对较新的 AWS Elastic Beanstalk Docker support 将其部署到 AWS。

这是我所做的(代码都是开源的,所以如果你愿意,可以跟随):

$ git clone https://github.com/rdegges/ipify-api.git
$ cd ipify-api
$ git fetch origin aws:aws
$ git checkout aws
$ eb init
$ eb create
$ eb deploy

这将使用弹性 beanstalk 创建一个新应用程序(使用 docker)并部署它。

如果我然后运行eb open 打开我的网络应用程序,我会看到我的公共 IP 地址显示(这是正确的行为),所以我知道我的应用程序正在运行/正常运行。

现在,在我的源代码中,我有几行调试输出:

fmt.Println("WOOOOOOOOOOOO")

上面的语句只是将“WOO…”打印到控制台。每次发出新请求时都会运行它。

不幸的是,当我运行eb logs 来查看我的实例日志时,这条调试行从未出现过——我不知道为什么。

我尝试过打印到 STDERR、打印到 STDOUT 等 - 但我绝对无法获得任何输出。

我已经在互联网上搜索了解决方案,但还没有找到任何解决方案。

【问题讨论】:

你能发布你的Dockerfile吗? 【参考方案1】:

您可能正在运行旧版本的应用程序,当我尝试在本地构建容器时出现以下错误:

➜ docker build .
# Executing 3 build triggers
Trigger 0, COPY . /go/src/app
Step 0 : COPY . /go/src/app
Trigger 1, RUN go-wrapper download
Step 0 : RUN go-wrapper download
 ---> Running in c1854666d13c
+ exec go get -v -d
github.com/julienschmidt/httprouter (download)
github.com/rdegges/ipify-api (download)
github.com/rs/cors (download)
Trigger 2, RUN go-wrapper install
Step 0 : RUN go-wrapper install
 ---> Running in 0bbdec1b99d7
+ exec go install -v
github.com/julienschmidt/httprouter
github.com/rdegges/ipify-api/models
github.com/rs/cors
github.com/rdegges/ipify-api/api
app
# app
./main.go:27: cannot use api.NotFound (type func(http.ResponseWriter, *http.Request)) as type http.Handler in assignment:
    func(http.ResponseWriter, *http.Request) does not implement http.Handler (missing ServeHTTP method)
./main.go:28: cannot use api.MethodNotAllowed (type func(http.ResponseWriter, *http.Request)) as type http.Handler in assignment:
    func(http.ResponseWriter, *http.Request) does not implement http.Handler (missing ServeHTTP method)
The command '/bin/sh -c go-wrapper install' returned a non-zero code: 2

部署后您的事件流中是否有任何错误?我相信“eb events”。

【讨论】:

【参考方案2】:

我认为import "github.com/rdegges/ipify-api/api" 是从 Github 上的副本构建的,而不是从本地副本构建的。 api 包的最新提交没有多余的 fmt 语句。此外,main.go 中的 log 语句工作正常,fmt.Fprintf 已经在 api 包中。尝试验证 api 包是从您认为的源构建的。

2015 年 6 月 1 日编辑:

我的所有测试都表明这是一个由内部子包引起的问题,而远程服务遇到了问题。 api 包不是从本地副本构建的。 fmt.Printmain.go 工作得很好,将 GetIPapi 包移动到 main 包让 fmt 在 Web 请求期间打印到标准输出就好了。

基本上,对本地 api 子包所做的所有更改都将被忽略。

EB 日志可能会说明哪些软件包正在远程下载;我的。

我不是 Godep 专业人士,所以也许 Godep 专业人士可以详细说明。但是 FWIW,似乎它可能与 this issue 有关,切向。这也可能是远程服务没有使用godep go build 构建的问题,但就像我说的,我不是 Godep 专业人士,所以我不确定。

【讨论】:

以上是关于在 AWS 上使用 Elastic Beanstalk 的 Go Web 应用程序未在日志中显示输出的主要内容,如果未能解决你的问题,请参考以下文章

使用 AWS Elastic Beanstalk 在 AWS 上部署 Spring Boot 应用程序

如何使用 aws cli 在 Elastic Beanstalk 上上传和部署?

AWS Elastic Beanstalk 上禁止使用 Django 403

如何在 AWS Elastic Beanstalk 上强制使用 HTTPS

如何在 AWS Elastic Beanstalk 上设置 HTTPS

如何在 AWS Elastic Beanstalk 上设置 HTTPS