简体   繁体   English

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

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

I'm trying to use Firestore in my AppEngine (standard environment) app written in Go. 我正在尝试在用Go编写的AppEngine(标准环境)应用中使用Firestore。 I've been following the " Getting Started with Cloud Firestore" guide and have been using the firestore package documentation to implement a simple example that works fine when running it on my local dev server. 我一直在遵循“ Cloud Firestore入门”指南,并且一直在使用firestore软件包文档来实现一个简单的示例,该示例在本地开发服务器上运行时可以正常工作。

However when I deploy the app and try the deployed version the call to DocumentRef.Set() fails with the error 但是,当我部署应用程序并尝试部署的版本时,对DocumentRef.Set()的调用失败,并显示以下错误:

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

This is my code that reproduces the issue: 这是我的代码,重现了这个问题:

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")
}

As mentioned before, on the dev server this works fine. 如前所述,在开发服务器上可以正常工作。 So there the returned page contains the text Data stored in Firestore successfully . 因此,返回的页面包含Data stored in Firestore successfully中的文本Data stored in Firestore successfully

When running the deployed code I get Failed to create a firestore document: rpc error: code = Unavailable desc = all SubConns are in TransientFailure instead. 运行部署的代码时,我Failed to create a firestore document: rpc error: code = Unavailable desc = all SubConns are in TransientFailure Why do I get this error and how can I avoid it? 为什么会出现此错误,如何避免呢?

I've raised an issue about this in the Firestore client library issue tracker and it seems like the situation is a bit complex. 我提出了一个问题,这个在公司的FireStore客户端库问题追踪器,它好像情况有点复杂。

When using App Engine the Firestore client library's network connections goes trough the App Engine socket library . 使用App Engine时,Firestore客户端库的网络连接会穿过App Engine 套接字库 However sockets is only available for paid App Engine apps : 但是,套接字仅适用于付费App Engine应用程序

Sockets are only available for paid apps, and traffic from sockets is billed as outgoing bandwidth. 套接字仅适用于付费应用,套接字的流量记为传出带宽。 Sockets are also limited by daily and per minute (burst) quotas. 套接字还受每日和每分钟(突发)配额的限制。

So this is the reason why the Firestore client library fails. 因此,这就是Firestore客户端库失败的原因。 For small scale projects it's possible to enable billing of your App Engine app and still stay within the free range. 对于小型项目,可以为您的App Engine应用启用结算功能,并且仍处于免费范围内。 If billing is enabled it should work when the app is deployed as well. 如果启用了计费,则在部署该应用程序时它也应能正常工作。

However if you are living within the European Union you are not allowed to have a paid App Engine app for non commercial purposes due to Google policies : 但是,如果您生活在欧盟内部,则由于Google政策的限制,您不得出于非商业目的而使用付费的App Engine应用程序:

If you are located in the European Union and the sole purpose for which you want to use Google Cloud Platform services has no potential economic benefit you should not use the service. 如果您位于欧盟,并且您要使用Google Cloud Platform服务的唯一目的没有潜在的经济利益,则不应使用该服务。 If you have already started using Google Cloud Platform, you should discontinue using the service. 如果您已经开始使用Google Cloud Platform,则应停止使用该服务。 See Create, modify, or close your billing account to learn how to disable billing on your projects. 请参阅创建,修改或关闭您的计费帐户,以了解如何在项目中禁用计费。

So if you are in Europe or for some other reason are unable to use have a paid App Engine app you will not be able to use the Firestore client library. 因此,如果您在欧洲或由于某些其他原因而无法使用拥有付费App Engine应用程序,则将无法使用Firestore客户端库。

One alternative in this case is to use the Firestore REST API instead and manually make HTTP requests to Firestore. 在这种情况下,一种替代方法是改用Firestore REST API,然后手动向Firestore发出HTTP请求。 It's a bit more work, but for smaller scale projects it works. 这需要更多的工作,但是对于规模较小的项目,它可以工作。

On AppEngine you need to create a client that uses Http client provided by urlfetch service. 在AppEngine上,您需要创建一个使用urlfetch服务提供的Http客户端的客户端。

The firestore.NewClient() function accepts ClientOptions parameters that you can create using WithHTTPCLient() function. firestore.NewClient()函数接受可以使用WithHTTPCLient()函数创建的ClientOptions参数。

Here is an article on issuing HTTP requests from AppEngine Go . 这是有关从AppEngine Go发出HTTP请求的文章。

That should help. 那应该有帮助。

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

相关问题 使用 node.js 标准环境在 AppEngine 上找不到模块 @google-cloud/firestore - Cannot find module @google-cloud/firestore on AppEngine using node.js standard environment 使用Admin API的Google Cloud Appengine标准环境部署:“无法找到导入”错误 - Google cloud Appengine standard environment deployment using Admin API: “Cant find import” Error Go中的AppEngine标准环境发布/订阅上下文 - AppEngine Standard Environment Pub/Sub Context in Go nodejs 应用在 AppEngine 标准环境中运行时未收到 SIGTERM - nodejs app doesn't receive SIGTERM when running in AppEngine Standard Environment Google Cloud /标准Appengine:本地运行应用程序失败 - Google Cloud / standard Appengine : running application locally fails 在AppEngine中运行浏览时出错 - Error when running browse in AppEngine 从云中的appengine运行时,images.get_serving_url(blob_key)返回错误。 - images.get_serving_url(blob_key) returns error when ran from appengine in cloud. Google Cloud Appengine 在调用 Spring 启动 REST API 时返回“404 错误” - Google Cloud Appengine returns '404 Error' when calling Spring Boot REST APIs 首次在 Google AppEngine Standard 项目上运行 gradle 时出现 NullPointerException - NullPointerException when running gradle for the first time on Google AppEngine Standard project 使用 Appengine 和 Go 时的子文件夹和包 - Subfolders and packages when using Appengine and Go
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM