繁体   English   中英

在AppEngline上运行时,将Cloud Firestore与AppEngine Go标准环境配合使用会返回rpc错误

[英]Using Cloud Firestore with AppEngine Go Standard Environment returns rpc error when running on AppEngline

我正在尝试在用Go编写的AppEngine(标准环境)应用中使用Firestore。 我一直在遵循“ Cloud Firestore入门”指南,并且一直在使用firestore软件包文档来实现一个简单的示例,该示例在本地开发服务器上运行时可以正常工作。

但是,当我部署应用程序并尝试部署的版本时,对DocumentRef.Set()的调用失败,并显示以下错误:

rpc error: code = Unavailable desc = all SubConns are in TransientFailure

这是我的代码,重现了这个问题:

func init() {
    http.HandleFunc("/test", testHandler)
}

type testData struct {
    TestData string `firestore:"myKey,omitempty"`
}

func testHandler(w http.ResponseWriter, r *http.Request) {
    ctx := appengine.NewContext(r)

    var firestoreClient *firestore.Client
    var firebaseApp *firebase.App
    var err error

    conf := &firebase.Config{ProjectID: "my-project"}
    firebaseApp, err = firebase.NewApp(ctx, conf)

    if err != nil {
        fmt.Fprintf(w, "Failed to create a new firestore app: %v", err)
        return
    }

    firestoreClient, err = firebaseApp.Firestore(ctx)
    if err != nil {
        fmt.Fprintf(w, "Failed to create a new firestore client: %v", err)
        return
    }

    data := testData{"my value"}
    _, err = firestoreClient.Collection("testCollection").Doc("testDoc").Set(ctx, data)
    if err != nil {
        fmt.Fprintf(w, "Failed to create a firestore document: %v", err)
        return
    }
    firestoreClient.Close()
    fmt.Fprint(w, "Data stored in Firestore successfully")
}

如前所述,在开发服务器上可以正常工作。 因此,返回的页面包含Data stored in Firestore successfully中的文本Data stored in Firestore successfully

运行部署的代码时,我Failed to create a firestore document: rpc error: code = Unavailable desc = all SubConns are in TransientFailure 为什么会出现此错误,如何避免呢?

我提出了一个问题,这个在公司的FireStore客户端库问题追踪器,它好像情况有点复杂。

使用App Engine时,Firestore客户端库的网络连接会穿过App Engine 套接字库 但是,套接字仅适用于付费App Engine应用程序

套接字仅适用于付费应用,套接字的流量记为传出带宽。 套接字还受每日和每分钟(突发)配额的限制。

因此,这就是Firestore客户端库失败的原因。 对于小型项目,可以为您的App Engine应用启用结算功能,并且仍处于免费范围内。 如果启用了计费,则在部署该应用程序时它也应能正常工作。

但是,如果您生活在欧盟内部,则由于Google政策的限制,您不得出于非商业目的而使用付费的App Engine应用程序:

如果您位于欧盟,并且您要使用Google Cloud Platform服务的唯一目的没有潜在的经济利益,则不应使用该服务。 如果您已经开始使用Google Cloud Platform,则应停止使用该服务。 请参阅创建,修改或关闭您的计费帐户,以了解如何在项目中禁用计费。

因此,如果您在欧洲或由于某些其他原因而无法使用拥有付费App Engine应用程序,则将无法使用Firestore客户端库。

在这种情况下,一种替代方法是改用Firestore REST API,然后手动向Firestore发出HTTP请求。 这需要更多的工作,但是对于规模较小的项目,它可以工作。

在AppEngine上,您需要创建一个使用urlfetch服务提供的Http客户端的客户端。

firestore.NewClient()函数接受可以使用WithHTTPCLient()函数创建的ClientOptions参数。

这是有关从AppEngine Go发出HTTP请求的文章。

那应该有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM