【问题标题】:Why doesn't this simple Go server run in Azure App Services?为什么这个简单的 Go 服务器不在 Azure 应用服务中运行?
【发布时间】:2020-02-21 02:48:22
【问题描述】:

我在server.go 中有此代码:

package main

import (
    "fmt"
    "net/http"
    "os"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "You just browsed page (if blank you're at the root): %s", r.URL.Path[1:])
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":"+os.Getenv("HTTP_PLATFORM_PORT"), nil)
}

还有这个web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="httpplatformhandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
        </handlers>
        <httpPlatform processPath="d:\home\site\wwwroot\go\bin\go.exe" 
                      arguments="run d:\home\site\wwwroot\server.go" 
                      startupTimeLimit="60">
            <environmentVariables>
              <environmentVariable name="GOROOT" value="d:\home\site\wwwroot\go" />
            </environmentVariables>
        </httpPlatform>
    </system.webServer>
</configuration>

这两个文件都位于 Azure 应用服务的 d:\home\site\wwwroot 目录中。我还在d:\home\site\wwwroot\go 中安装了1.13.3 版本的x64 Windows go 运行时(从go1.13.3.windows-amd64.zip 解压缩)。

当我浏览到fwWebApi.azurewebsites.net/hello 时,它会超时。

我从http://www.wadewegner.com/2014/12/4-simple-steps-to-run-go-language-in-azure-websites/ 得到了这个样本,这个样本已经有几年的历史了。但我希望能够在 Azure App Services 中运行 Go Web 应用程序。

谁能建议我能做什么?

【问题讨论】:

  • 您的端口是否正确配置为允许端口 80 上的入站流量?
  • 我相信是的。在创建应用服务时,我不必为端口 80 设置任何特定内容。
  • 像这样使用go run 意味着每次处理程序启动时都会构建并运行您的代码。 Go 不是 PHP 或 Ruby,你不应该使用 go run 做任何事情。您应该在您的工作站或构建服务器上go build,并将编译后的二进制文件部署到您的服务器(根本不需要安装 Go)。
  • @Adrian 谢谢——我将web.config 文件更改为调用server.exe,我在本地构建并复制了该文件。这给了我一个 502。
  • 这在几个月前肯定对我有用。我刚刚回到该项目,突然这不起作用。仍在尝试调试。希望不必走集装箱路线。如果我有任何地方会在这里更新。

标签: azure go azure-web-app-service azure-app-service-envrmnt


【解决方案1】:

我强烈建议您使用 docker 映像在 azure 应用服务上运行不受支持的语言:

支持的语言 ASP.NET、ASP.NET Core、Java、Ruby、Node.js、PHP 或 Python

Github:https://github.com/AnassKartit/helloworld-golang

Docker 镜像 https://hub.docker.com/r/anasskartit/hello-world-golang

首次运行需要一些时间,因为它会下载您可以查看日志的图像

结果

【讨论】:

  • 如果这是我必须做的,我会试试的。我只是想看看是否可以直接在应用服务中调用标准 Go 代码。
  • 您好阿纳斯,非常感谢您提供的样品!我试试看。
  • 虽然azure.microsoft.com/fr-fr/blog/…这个是旧的
  • 我不想将其标记为已完成,而是希望保持打开状态,因为我非常希望让 Go 程序在没有容器的情况下运行。仍然想知道为什么一个独立的本机 Go 程序在 Azure 应用服务中不起作用。
猜你喜欢
  • 2021-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
  • 2016-06-03
  • 2022-12-18
相关资源
最近更新 更多