【问题标题】:How to call a Go Cloud Function from another Go Cloud Function in GCP如何从 GCP 中的另一个 Go Cloud Function 调用 Go Cloud Function
【发布时间】:2019-11-06 01:56:09
【问题描述】:

目标:我想通过 HTTP 触发器重用两个 Go 函数中的许多 Go 函数。

我尝试过的方法和重现问题的步骤:

  1. 在 GCP 中,新建一个 Go 1.11 Cloud Function,HTTP 触发器
  2. 命名:MyReusableHelloWorld
  3. function.go 中粘贴:
package Potatoes

import (   
    "net/http"
)


// Potatoes return potatoes
func Potatoes(http.ResponseWriter, *http.Request) {

}
  1. go.mod 中,粘贴:module example.com/foo
  2. 在要执行的函数中,粘贴:Potatoes
  3. 单击部署。它有效。
  4. 在 GCP 中创建另一个 Go 无服务器函数
  5. 在功能中。去吧,粘贴这个:
// Package p contains an HTTP Cloud Function.
package p

import (
    "encoding/json"
    "fmt"
    "html"
    "net/http"
    "example.com/foo/Potatoes"
)

// HelloWorld prints the JSON encoded "message" field in the body
// of the request or "Hello, World!" if there isn't one.
func HelloWorld(w http.ResponseWriter, r *http.Request) {
    var d struct {
        Message string `json:"message"`
    }
    if err := json.NewDecoder(r.Body).Decode(&d); err != nil {
        fmt.Fprint(w, "error here!")
        return
    }
    if d.Message == "" {
        fmt.Fprint(w, "oh boy Hello World!")
        return
    }
    fmt.Fprint(w, html.EscapeString(d.Message))
}
  1. go.mod 中,粘贴:module example.com/foo
  2. 在要执行的函数中,粘贴:HelloWorld
  3. 单击部署。 它不起作用。 你有错误: unknown import path "example.com/foo/Potatoes": cannot find module providing package example.com/foo/Potatoes

我也尝试了各种组合来导入模块/包。 我试过没有 example.com/ 部分。

其他较小的问题: 我想重用的函数可能都在同一个文件中,实际上不需要任何触发器,但似乎没有触发器是不可能的。

我无法实现目标的相关问题和文档:

  1. How can I use a sub-packages with Go on Google Cloud Functions?
  2. https://github.com/golang/go/wiki/Modules,go.mod 部分

【问题讨论】:

    标签: go google-cloud-platform serverless


    【解决方案1】:

    您不能从另一个云函数调用一个云函数,因为每个函数都在其自己的容器中独立。

    因此,如果您要部署具有无法从包管理器下载的依赖项的函数,您需要将代码放在一起,如 here 并使用 CLI 进行部署

    【讨论】:

      【解决方案2】:

      控制台中定义的每个云功能很可能相互独立。如果你想要代码复用,最好按照下面的文档来组织,然后使用 gcloud 命令部署。

      https://cloud.google.com/functions/docs/writing/#structuring_source_code

      【讨论】:

        【解决方案3】:

        您正在混合:包管理和功能部署。

        当你部署一个 Cloud Function 时,如果你想(重新)使用它,你必须使用 http 包调用 if。

        如果您构建一个想要包含在源代码中的包,则必须依赖包管理器。使用 Go,Git 存储库,如 Github,是实现这一目标的最佳方式(不要忘记执行发布并按照 Go 的预期命名:vX.Y.Z)

        如果没有更多的工程和包发布/管理,您的代码将无法运行。

        我使用 Dockerfile 实现了相同的目标,并且我对 Cloud Run 中的代码感到遗憾(如果您不是面向事件且仅面向 HTTP,我建议您使用该代码。我写了一个 comparison on Medium

          • go.mod
          • pkg/foo.go
          • pkg/go.mod
          • service/Helloworld.go
          • service/go.mod

        在我的helloworld.go 中,我可以重复使用包foo。为此,我在我的service/go.mod 文件中执行此操作

        module service/helloworld
        
        go 1.12
        
        require pkg/foo v0.0.0
        
        replace pkg/foo v0.0.0 => ../pkg
        

        然后在构建容器时,从根目录运行 go build service/Helloworld.go

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-04-24
          • 2017-12-17
          • 2021-06-21
          • 2021-06-12
          • 2019-11-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多