亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

每次使用應用引擎中的數據存儲都會超時

每次使用應用引擎中的數據存儲都會超時

Go
慕姐8265434 2022-01-17 16:37:56
我第一次使用谷歌云環境,特別是谷歌應用引擎和數據存儲,當我在本地運行時一切正常。我通過按照文檔設置環境變量 GOOGLE_APPLICATION_CREDENTIALS 對數據存儲進行身份驗證。但是一旦我部署到應用程序引擎,請求總是超時,似乎 GetAll 方法永遠不會返回。以下是我的應用程序的代碼:package appimport (    "fmt"    "net/http"    "time"    "golang.org/x/net/context"    "google.golang.org/appengine"    "google.golang.org/cloud/datastore")type User struct {    FirstName string    LastName string    Email string    Created time.Time    id      int64}func init() {    http.HandleFunc("/", handler)}func handler(w http.ResponseWriter, r *http.Request) {    var err error    var dbClient *datastore.Client    var ctx context.Context    ctx = appengine.NewContext(r)    dbClient, err = datastore.NewClient(ctx, "app-id")//this has the real app id, not sure if this is meant to be secret    if err != nil {        fmt.Fprintf(w, "Could not create datastore client: %+v", err)        return    }    defer dbClient.Close()    var users []*User    query := datastore.NewQuery("User").Filter("Email=", "[email protected]")    keys, err := dbClient.GetAll(ctx, query, &users)    if err != nil {        fmt.Fprintf(w, "Could not query users: %+v", err)        return    }    for i, key := range keys {        users[i].id = key.ID()        fmt.Fprintf(w, "%+v\n",users[i])    }    fmt.Fprintf(w, "done!!!")}App Engine 日志中的錯誤有以下兩行:This request caused a new process to be started for your application, and thus caused your application code to be loaded for the first time. This request may thus take longer and use more CPU than a typical request for your application.Process terminated because the request deadline was exceeded. (Error code 123)順便說一句,這在我的本地快速完成,并且我的數據存儲中只有一條記錄。關于為什么會發生這種情況或如何調試它的任何猜測?謝謝
查看完整描述

2 回答

?
吃雞游戲

TA貢獻1829條經驗 獲得超7個贊

這些datastore包使用gRPC連接到數據存儲服務。您不能直接在 App Engine 上使用它,因為您不能直接建立 TCP 連接。


您需要使用socketsAPI 為您建立 TCP 連接:


import "google.golang.org/appengine/socket" // et al


ctx := appengine.NewContext(r)

ctx, cancel := context.WithTimeout(ctx, 5*time.Second)

defer cancel()


dialer := func(addr string, timeout time.Duration) (net.Conn, error) {

    return socket.DialTimeout(ctx, "tcp", addr, timeout)

}


client, err := datastore.NewClient(ctx, "app-id", cloud.WithGRPCDialOption(grpc.WithDialer(dialer)))

您也可以dialer在調試時直接調用,以確保它能夠按預期訪問 datastore.googleapis.com:443:


conn, err := dialer("datastore.googleapis.com:443", 5*time.Second)

if err != nil {

    log.Errorf(ctx, "Dial: %v", err)

    http.Error(w, "Dial failed: "+err.Error(), http.StatusInternalServerError)

    return

}

fmt.Fprintf(w, "Addr: %v\n", conn.RemoteAddr())

conn.Close()


查看完整回答
反對 回復 2022-01-17
?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

我的問題的解決方案是切換數據存儲庫

從: "google.golang.org/cloud/datastore"

到: "google.golang.org/appengine/datastore"

appengine/datastore 似乎使用了不同的協議,因此您不必使用 GOOGLE_APPLICATION_CREDENTIALS 環境變量進行身份驗證。在本地使用時,它將為不同的應用程序引擎服務使用開發服務器,在部署時使用云資源。


查看完整回答
反對 回復 2022-01-17
  • 2 回答
  • 0 關注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號