Google App Engine 更新 Go Runtime 到版本 1.11

未匹配的标注

本文为官方 Go Blog 的中文翻译,详见 翻译说明

Eno Compton and Tyler Bui-Palsulich
16 October 2018

App Engine于2011年推出了对Go的实验支持。在随后的几年中,Go社区取得了长足的发展,并决定采用基于云的应用程序的惯用模式。今天,Google Cloud 宣布一个新的Go 1.11运行时适用于提供App Engine的所有功能的App Engine标准环境(例如仅为您使用的东西付费,自动缩放和托管基础结构),同时支持惯用的Go。

从 Go 1.11开始, App Engine 中的 Go 对应用程序结构、受支持的程序包、context.Context值或HTTP客户端没有限制。根据您的喜好编写Go应用程序,添加一个app.yaml文件,您的应用程序即可在App Engine上部署。 指定依赖项描述了新运行时如何支持[Vendor](golang.org/cmd/go/# hdr-Vendor_Directories)和模块(实验性),用于进行依赖项管理。

让我们来看看为App Engine创建一个小型应用程序。对于此处的示例,尽管Go模块具有实验支持,我们依然使用基于GOPATH的工作流程 。

首先,在 GOPATH 中创建应用程序:

// 此服务器可以在App Engine上运行。
package main

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

func main() {
    port := os.Getenv("PORT")
    if port == "" {
        port = "8080"
    }
    http.HandleFunc("/", hello)

    log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}

func hello(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello, 世界"))
}

该代码包含用于响应 “Hello, 世界.” 的小型 HTTP 服务器的常用设置。如果您以前有 App Engine 经验,则会注意到此程序没有对 appengine.Main()的任何调用,这个调用现在完全是可选的。此外,应用程序代码是完全可移植的 —— 与您的应用程序所部署的基础结构没有关系。

如果需要使用外部依赖项,则可以将这些依赖项添加到新运行时支持的vendor目录或go.mod文件中。

完成应用程序代码后,创建一个app.yaml文件以指定运行时:

runtime: go111

最后,在电脑上配置 Google Cloud Platform 账户:

完成所有设置后,可以使用以下命令进行部署:

gcloud app deploy

我们认为 Go 开发人员会发现 App Engine 的新 Go 1.11 运行时是运行 Go 应用程序的可用选项的一个令人兴奋的补充。该运行时提供免费套餐。请查看入门指南或[迁移指南](cloud.google.com/appengine / docs / standard / go111 / go-differences),并立即将应用程序部署到新的运行时!

Eno Compton and Tyler Bui-Palsulich
16 October 2018

App Engine launched experimental support for Go in 2011. In the subsequent years, the Go community has grown significantly and has settled on idiomatic patterns for cloud-based applications. Today, Google Cloud is announcing a new Go 1.11 runtime for the App Engine standard environment that provides all the power of App Engine—things like paying only for what you use, automatic scaling, and managed infrastructure—while supporting idiomatic Go.

Starting with Go 1.11, Go on App Engine has no limits on application structure, supported packages, context.Context values, or HTTP clients. Write your Go application however you prefer, add an app.yaml file, and your app is ready to deploy on App Engine. Specifying Dependencies describes how the new runtime supports vendoring and modules (experimental) for dependency management.

Along with Cloud Functions support for Go (more on that in a future post), App Engine provides a compelling way to run Go code on Google Cloud Platform (GCP) with no concern for the underlying infrastructure.

Let’s take a look at creating a small application for App Engine. For the example here, we assume a GOPATH-based workflow, although Go modules have experimental support as well.

First, you create the application in your GOPATH:

// This server can run on App Engine.
package main

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

func main() {
    port := os.Getenv("PORT")
    if port == "" {
        port = "8080"
    }
    http.HandleFunc("/", hello)

    log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}

func hello(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello, 世界"))
}

The code contains an idiomatic setup for a small HTTP server that responds with “Hello, 世界.” If you have previous App Engine experience, you’ll notice the absence of any call to appengine.Main(), which is now entirely optional. Furthermore, the application code is completely portable—there are no ties to the infrastructure that your application is deployed on.

If you need to use external dependencies, you can add those dependencies to a vendor directory or to a go.mod file, both of which the new runtime supports.

With the application code complete, create an app.yaml file to specify the runtime:

runtime: go111

Finally, set your machine up with a Google Cloud Platform account:

With all the setup complete, you can deploy using one command:

gcloud app deploy

We think Go developers will find the new Go 1.11 runtime for App Engine an exciting addition to the available options to run Go applications. There is a free tier. Check out the getting started guide or the migration guide and deploy an app to the new runtime today!

本文章首发在 LearnKu.com 网站上。

本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:https://learnku.com/docs/go-blog/appengi...

译文地址:https://learnku.com/docs/go-blog/appengi...

上一篇 下一篇
Summer
贡献者:1
讨论数量: 0
发起讨论 只看当前版本


暂无话题~