GAE/Go1.9试行(第0期:「快速入门」)

请用中文改写以下句子,仅提供一种选项:

題目

为了理解GAE(Google App Engine),我决定亲自制作应用并部署上去尝试一下。
因为在工作中使用了Java版本,所以对它有一些了解(至少是初级水平)。我还写了一些相关的文章。
接下来,我想试试Golang版本。
首先,我会尝试一下常规的快速入门教程。

开发环境

操作系统

$ cat /etc/os-release 
NAME="Ubuntu"
VERSION="17.10 (Artful Aardvark)"

# Go编程语言

$ go version
go version go1.9.7 linux/amd64

版本的切换是用goenv进行的。

gcloud

$ gcloud version
Google Cloud SDK 224.0.0

前提的意思是指在某件事情或者情况发生之前的条件、要求或者假设。

スタンダード環境での開発とする。
GCPプロジェクトは作成済み。
gcloudコマンドインストール済み。

实践 (Shí

按照以下步骤,并逐步检查相关文件的内容。
https://cloud.google.com/appengine/docs/standard/go/quickstart?hl=ja

获取示例项目

按照步骤。

$ go get -u -d github.com/GoogleCloudPlatform/golang-samples/appengine/helloworld/...
$
$ ll golang-samples/appengine/helloworld/
合計 16K
-rw-r--r-- 1 koge koge   69 11月 12 02:51 app.yaml
-rw-r--r-- 1 koge koge  391 11月 12 02:51 hello.go

你好. 去吧.

package main

import (
    "fmt"
    "net/http"

    "google.golang.org/appengine"
)

func main() {
    http.HandleFunc("/", handle)
    appengine.Main()
}

func handle(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Hello, world!")
}

使用http包,在Golang中定义对“/”访问的行为是创建Web应用程序的基本操作。
通常情况下,之后会编写启动服务器逻辑,如使用“http.ListenAndServe(“:8080”, nil)”指定端口进行监听,但也可以替代地使用“appengine.Main()”进行编写。
(嗯,这应该是在内部进行封装的)

确认一下。

func Main() {
    internal.Main()
}
func Main() {
    installHealthChecker(http.DefaultServeMux)

    port := "8080"
    if s := os.Getenv("PORT"); s != "" {
        port = s
    }

    host := ""
    if IsDevAppServer() {
        host = "127.0.0.1"
    }
    if err := http.ListenAndServe(host+":"+port, http.HandlerFunc(handleHTTP)); err != nil {
        log.Fatalf("http.ListenAndServe: %v", err)
    }
}

func installHealthChecker(mux *http.ServeMux) {
    // If no health check handler has been installed by this point, add a trivial one.
    const healthPath = "/_ah/health"
    hreq := &http.Request{
        Method: "GET",
        URL: &url.URL{
            Path: healthPath,
        },
    }
    if _, pat := mux.Handler(hreq); pat != healthPath {
        mux.HandleFunc(healthPath, func(w http.ResponseWriter, r *http.Request) {
            io.WriteString(w, "ok")
        })
    }
}

嗯,果然是这样的。
通过添加健康检查口(在AppEngine中以”/_ah/”开头的路径),(如果有主机和端口的环境变量设置,将采用并启动服务器。

app.yaml文件

决定App Engine行为的配置文件。
下面的链接中描述了应定义的元素。
https://cloud.google.com/appengine/docs/standard/go/config/appref?hl=ja

runtime: go
api_version: go1

handlers:
- url: /.*
  script: _go_app

在用于快速启动的app.yaml中,仅包含了必要的最基本定义。

要素説明備考runtimeGAEアプリを動かすための実行環境を指定する。必須要素api_versionruntimeのAPIのバージョン。
go1を指定すると、アプリデプロイのたびに最新のサポートされている実行時環境が使用される(現在go1.9)。
明示的にするならgo1.9と書く。必須要素handlersURLパターンのリストとそれらの処理方法の説明。
GAEは、アプリコードの実行、ないし、静的ファイルのホスティングによって、指定のURLアクセス時の挙動を制御できる。必須要素handlers/urlどのパスにアクセスした時にハンドリング処理を発動するか。handlers指定時は必須要素handlers/scriptGAEアプリルートからのスクリプトのパスを指定する。
ただし、Goアプリの場合は常に_go_appにする必要がある。任意要素

■本地启动

在本地运行时,启动脚本类似于Python。

$ dev_appserver.py app.yaml
INFO     2018-11-11 19:03:00,371 devappserver2.py:224] Using Cloud Datastore Emulator.
We are gradually rolling out the emulator as the default datastore implementation of dev_appserver.
   〜〜〜
INFO     2018-11-11 19:03:02,127 dispatcher.py:256] Starting module "default" running at: http://localhost:8080
INFO     2018-11-11 19:03:02,129 admin_server.py:152] Starting admin server at: http://localhost:8000
INFO     2018-11-11 19:03:04,904 instance.py:294] Instance PID: 11917
001.png

■部署

当然的,假设已经完成了”gcloud init”和”gcloud auth login”。

$ gcloud app deploy
Services to deploy:

descriptor:      [/work/src/golang/src/github.com/GoogleCloudPlatform/golang-samples/appengine/helloworld/app.yaml]
source:          [/work/src/golang/src/github.com/GoogleCloudPlatform/golang-samples/appengine/helloworld]
target project:  [【プロジェクトID】]
target service:  [default]
target version:  [20181112t042229]
target url:      [https://【プロジェクトID】.appspot.com]


Do you want to continue (Y/n)?  y

Beginning deployment of service [default]...
╔════════════════════════════════════════════════════════════╗
╠═ Uploading 87 files to Google Cloud Storage               ═╣
╚════════════════════════════════════════════════════════════╝
File upload done.
Updating service [default]...done.                                                                                                                                                                         
Setting traffic split for service [default]...done.                                                                                                                                                        
Deployed service [default] to [https://【プロジェクトID】.appspot.com]

You can stream logs from the command line by running:
  $ gcloud app logs tail -s default

To view your application in the web browser run:
  $ gcloud app browse
002.png
003.png

总结

使用GAE,将准备好的应用程序部署到GCP上的速度很快。
我以前尝试过使用GKE+go的组合,但是通过使用GAE的限制,让我想看看能变得多么容易,或者会让我对其灵活性感到不满意,我打算尝试一些不同的方法。

bannerAds