如何在 OpenWhisk 上 运行 带有 redis 包的 Go 函数?

How to run a Go function with redis package on OpenWhisk?

我在 运行 OpenWhisk (IBM Cloud Functions) 上的 golang 包时遇到了一些麻烦。

我 运行 在我的本地计算机上执行以下操作,它没有任何问题 (go run sample.go):

package main


import (
    "fmt"
    "encoding/json"
    "github.com/go-redis/redis"
)


func main() {

    var redisClient *redis.Client = redis.NewClient(&redis.Options{
        Addr: "...",
        Password: "...",
        DB: 0,
    })

    redisClient.Set("foo", "bar", 0)

    defer redisClient.Close()

    msg := map[string]string{"msg": ("Done !")}
    res, _ := json.Marshal(msg)
    fmt.Println(string(res))

}

但我没有找到任何方法让它在 OpenWhisk 上运行。我 运行 以下内容:

GOOS=linux GOARCH=amd64 go build -o exec sample.go

zip exec.zip exec

bx wsk action update myfunction --native exec.zip

bx wsk action invoke myfunction -r
bx wsk activation logs --last --strip

"error": "The action did not return a dictionary."

"2018-02-21T01:21:05.962244788Z stdout: [Errno 2] No such file or directory: '/action/exec'"

问题与 github.com/go-redis/redis 包有关,当我删除它和它的代码时,功能 运行ning 很好。 mgo 包 (MongoDB)...

我遇到了同样的问题

我是 Golang 的新手,所以这可能很明显,但现在我被困住了:/

"error": "The action did not return a dictionary."

这是打开 wsk 的错误信息。 这意味着您没有 return stdout

上的字典

下面是一个有效的 return,因为它 return 是一个 json 对象

{"error":"something broke!"}

以下内容无效

"something broke!"

建议:检查构建时是否有错误(正常) 您保管箱中的文件看起来不像二进制文件...我建议检查您是否可以先构建二进制文件

zip 文件中的二进制文件动态链接到平台上不可用的共享库。

使用 fileldd 证实了这一点:

$ file exec 
exec: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, not stripped 
$ ldd exec
/lib64/ld-linux-x86-64.so.2 (0x7f3f63f10000)
libpthread.so.0 => /lib64/ld-linux-x86-64.so.2 (0x7f3f63f10000)
libc.so.6 => /lib64/ld-linux-x86-64.so.2 (0x7f3f63f10000)

在平台 (openwhisk/dockerskeleton) 使用的 Docker 映像中构建静态二进制文件或动态链接二进制文件。

file not found 错误具有误导性,但 reported by bash 在执行具有此问题的文件时。