我正在编写一个用Postgres方言与Cloud Spanner对话的程序.我的应用程序是一台GIN服务器,并且我正在使用pgAdapter进行连接,如this doc中所述.

My application runs fine locally. But when I deploy it to cloud run, I get the following log. enter image description here

误差主要来自StartPGAdapterWithCredentials函数.

func StartPGAdapter(ctx context.Context, project, instance string) (port int, cleanup func(), err error) {
    credentials, err := google.FindDefaultCredentials(ctx)
    fmt.Println("credentials " + (credentials.ProjectID) + "json " + utils.ToString(credentials.JSON) + "ts " + utils.ToString(credentials.TokenSource))
    if err != nil {
        return 0, func() {}, err
    }
    return StartPGAdapterWithCredentials(ctx, project, instance, credentials)
}
func StartPGAdapterWithCredentials(ctx context.Context, project, instance string, credentials *google.Credentials) (port int, cleanup func(), err error) {
    if credentials == nil {
        return 0, func() {}, fmt.Errorf("credentials cannot be nil")
    }
    if credentials.JSON == nil || len(credentials.JSON) == 0 {
        return 0, func() {}, fmt.Errorf("only JSON based credentials are supported")
    }
    credentialsFile, err := os.CreateTemp(os.TempDir(), "pgadapter-credentials")
    if err != nil {
        return 0, func() {}, err
    }

在我的本地系统上,设置了GOOGLE_APPLICATION_CREDENTIALS,因此它能够获得凭据.然而,这在云运行中是行不通的.

我如何让它在云运行中运行?

附加信息:按照示例呈现here.

推荐答案

您似乎正在try 在Cloud Run上启动嵌入式容器中的PGAdapter,然后将服务帐户凭据从您的环境复制到PGAdapter嵌入式容器.问题是Cloud Run不提供对底层服务帐户文件的访问.相反,您应该让Google Cloud库获得环境的默认凭据.

在您的示例中,困难的部分是在嵌入式容器中启动PGAdapter,这意味着在该环境中没有默认凭据.在Cloud Run上运行PGAdapter的推荐方式是将其包含在您的主容器中.这将使PGAdapter可以只获取Cloud Run提供的默认凭据.这意味着在启动PGAdapter时,您应该not指定-c /path/to/credentials.json参数.

有(至少)两种方法可以将PGAdapter包含在主容器中:

  1. 将Java .jar文件版本添加到您的Docker映像中,并在容器中启动PGAdapter.如何从.jar个文件直接启动PGAdapter,请参考[本例].这还需要将Java JRE添加到您的Docker镜像中.
  2. 让您的Docker构建扩展基于PGAdapter的Docker映像.这将自动包括PGAdapter所需的所有依赖项.您必须覆盖基本图像的ENTRYPOINT.

有关后一种 Select 的广泛(可运行)示例,请参见PGAdapter Cloud Run Sample for Go.

Go相关问答推荐

如何创建两个连接的io.ReadWriteClosers以用于测试目的

具有GRPC的RBAC(基于角色的访问控制)-网关生成的REST风格的API

Go在使用HTTP代理时如何处理DNS请求?

禁用Golang中的终端

按键值排序字符串- Golang

如何在gofr发起的服务间调用请求中添加Authorization Header?

Golang String

格式化 Azure SDK 的 golang 时间

Golang telegram 机器人

Go 中的 protobuf FieldMask 解组

使用 LINQ 对内部数组进行排序

如何将整数哈希细分为范围

Gorm 预加载给出了模糊的列错误

查找、解析和验证邮箱地址

无法建立连接,因为目标机器主动拒绝它 Golang

Golang ACMEv2 HTTP-01 挑战不挑战服务器

httprouterhttp.HandlerFunc() 是如何工作的?

Golang - 使用正则表达式提取链接

传递上下文的最佳方式

Golang 中的无实体函数