通过函数计算的Custom Container,您可以使用C++在函数计算平台编写函数。本文介绍如何快速部署和调用C++函数。您可以按需修改C++的示例代码实现二次开发。

操作步骤

  1. 在目标目录中,执行以下命令,初始化项目。
    s init start-fc-custom-container-event-cpp -d start-cc-event-cpp
    说明 -d用于指定该示例项目文件夹的名称,支持自定义。
  2. 执行以下命令,进入项目目录。
    cd start-cc-event-cpp
  3. 可选:按需修改函数的示例代码,实现二次开发。
  4. 修改s.yaml文件。
    s.yaml文件内的image字段的参数信息修改为您的镜像名称。
  5. 执行以下命令,部署项目。
    s deploy -y
    输出示例:
    [2022-02-09 06:20:43] [INFO] [S-CLI] - Start the pre-action
    [2022-02-09 06:20:43] [INFO] [S-CLI] - Action: s build --use-docker --dockerfile ./code/Dockerfile
    [2022-02-09 06:20:44] [INFO] [FC-BUILD] - Build artifact start...
    [2022-02-09 06:20:44] [INFO] [FC-BUILD] - Use docker for building.
    [2022-02-09 06:20:44] [INFO] [FC-BUILD] - Building image...
    Sending build context to Docker daemon  3.707MB
    ......
    Successfully built 6eb96a23f2a4
    Successfully tagged registry.cn-hangzhou.aliyuncs.com/fc-example/test:nginx
    Build image(registry.cn-hangzhou.aliyuncs.com/fc-example/test:nginx) successfully
    [2022-02-09 06:21:03] [INFO] [FC-BUILD] - Build artifact successfully.
    
    Tips for next step
    ======================
    * Invoke Event Function: s local invoke
    * Invoke Http Function: s local start
    * Deploy Resources: s deploy
    End of method: build
    [2022-02-09 06:21:03] [INFO] [S-CLI] - End the pre-action
    ......
    8f0b8fdd795f: Pushed
    nginx: digest: sha256:2f94960e325d8b3bc33226eb183bc1ad2b2bbd073c6996f9723a44d51ed1afc1 size: 1155
     Checking Service, Function (85.52s)
     Creating Service, Function (0.41s)
    
    There is auto config in the service: hello-world-service
    
    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
    
    helloworld:
      region:   cn-hangzhou
      service:
        name: hello-world-service
      function:
        name:       cpp-event-function
        runtime:    custom-container
        handler:    not-used
        memorySize: 256
        timeout:    60
  6. 执行以下命令,远程调用函数。
    s invoke -e '{"key":"val"}'
    输出示例:
    ========= FC invoke Logs begin =========
    /invoke is called.
    FC Invoke Start RequestId: c212ecbb-f5c8-4a0f-b9cf-80be2d3c4e07
    2022-02-09T06:24:06 c212ecbb-f5c8-4a0f-b9cf-80be2d3c4e07 [INFO] handling invoke
    FC Invoke End RequestId: c212ecbb-f5c8-4a0f-b9cf-80be2d3c4e07
    
    Duration: 1.74 ms, Billed Duration: 2 ms, Memory Size: 256 MB, Max Memory Used: 0.42 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    {"key":"val"}
    
    End of method: invoke