通过函数计算的Custom Runtime,您可以使用Go在函数计算平台编写函数。本文介绍快速部署和调用Go函数的详细步骤。您可以按需修改Go示例中的代码实现二次开发。

前提条件

您已完成以下操作:

操作步骤

  1. 在目标目录中,执行以下命令,初始化项目。
    s init start-fc-custom-samples -d start-fc-custom-samples
  2. 执行以下命令,进入项目目录。
    cd start-fc-custom-samples/golang/event-demo/
  3. 执行以下命令,安装依赖。
    make build
    执行输出:
    docker build -t fc-go-runtime  -f build-image/Dockerfile build-image
    Sending build context to Docker daemon  2.048kB
    Step 1/5 : FROM golang:1.12.16-stretch
     ---> 7ad03a8aece5
    Step 2/5 : RUN mkdir -p $GOPATH/src/golang.org/x/
     ---> Using cache
     ---> 8ae05c78f213
    Step 3/5 : RUN cd $GOPATH/src/golang.org/x/ && git clone https://github.com/golang/net.git
     ---> Using cache
     ---> 4144ef5b258b
    Step 4/5 : RUN cd $GOPATH/src/golang.org/x/ &&  git clone https://github.com/golang/sys.git
     ---> Using cache
     ---> d7bcb9bf6bda
    Step 5/5 : RUN go get github.com/awesome-fc/golang-runtime
     ---> Running in 86b5b13ec231
    Removing intermediate container 86b5b13ec231
     ---> c051b618c0ab
    Successfully built c051b618c0ab
    Successfully tagged fc-go-runtime:latest
    docker run --rm -it -v $(pwd):/tmp fc-go-runtime bash -c "go build -o /tmp/code//bootstrap /tmp/code/main.go"
    chmod +x code/bootstrap
  4. 执行以下命令,部署项目。
    s deploy -y
    输出示例:
    [2021-09-09T17:28:46.906] [INFO ] [S-CLI] - Start ...
    [2021-09-09T17:28:47.428] [INFO ] [FC-DEPLOY] - Using region: cn-hangzhou
    [2021-09-09T17:28:47.429] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-09-09T17:28:47.429] [INFO ] [FC-DEPLOY] - Using accessKeyID: 188077086902****
    [2021-09-09T17:28:47.430] [INFO ] [FC-DEPLOY] - Using accessKeySecret: LTAI4G4cwJkK4Rza6xd9****
      Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
    [2021-09-09T17:28:47.684] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
    [2021-09-09T17:28:47.901] [INFO ] [FC-DEPLOY] - Checking Function goEventFunc exists
      Make service custom-demo success.
      Make function custom-demo/goEventFunc success.
    [2021-09-09T17:29:29.304] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
    [2021-09-09T17:29:29.365] [INFO ] [FC-DEPLOY] - Checking Function goEventFunc exists
    
    Tips for next step
    ======================
    * Display information of the deployed resource: s info
    * Display metrics: s metrics
    * Display logs: s logs
    * Invoke remote function: s invoke
    * Remove Service: s remove service
    * Remove Function: s remove function
    * Remove Trigger: s remove trigger
    * Remove CustomDomain: s remove domain
    
    
    
    custom-demo:
      region: cn-hangzhou
      service:
        name: custom-demo
      function:
        name: goEventFunc
        runtime: custom
        handler: main.handler
        memorySize: 512
        timeout: 30
  5. 执行以下命令,远程调用函数。
    s invoke -e "{}"
    输出示例:
    [2021-09-09T17:30:32.509] [INFO ] [S-CLI] - Start ...
    ========= FC invoke Logs begin =========
    FC Initialize Start RequestId: 1190d876-343a-41ae-a4e8-705192b1d6b8
    2021-09-09T09:30:33.493Z: 1190d876-343a-41ae-a4e8-705192b1d6b8 [INFO]  init golang!
    FC Initialize End RequestId: 1190d876-343a-41ae-a4e8-705192b1d6b8
    FC Invoke Start RequestId: 1190d876-343a-41ae-a4e8-705192b1d6b8
    2021-09-09T09:30:33.506Z: 1190d876-343a-41ae-a4e8-705192b1d6b8 [INFO]  hello golang!
    FC Invoke End RequestId: 1190d876-343a-41ae-a4e8-705192b1d6b8
    
    Duration: 5.74 ms, Billed Duration: 6 ms, Memory Size: 512 MB, Max Memory Used: 7.69 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    {}
    
    
    End of method: invoke