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

操作步骤

  1. 执行以下命令,初始化项目。
    s init fc-custom-cpp-event -d fc-custom-cpp-event
    说明 -d用于指定该示例项目文件夹的名称,支持自定义。
  2. 执行以下命令,进入项目目录。
    cd fc-custom-cpp-event
  3. 可选:按需修改函数的示例代码,实现二次开发。
  4. 执行以下命令,部署项目。
    s deploy -y
    输出示例:
    [2022-01-26 08:39:21] [INFO] [S-CLI] - Start the pre-action
    [2022-01-26 08:39:21] [INFO] [S-CLI] - Action: make build
    docker build -t fc-cpp-runtime  -f build-image/Dockerfile build-image
    Sending build context to Docker daemon  2.048kB
    ......
    Reading package lists...
    Building dependency tree...
    Reading state information...
    The following additional packages will be installed:
      cmake-data libarchive13 libjsoncpp1 libuv1
    Suggested packages:
      codeblocks eclipse ninja-build lrzip
    The following NEW packages will be installed:
      cmake cmake-data libarchive13 libjsoncpp1 libuv1
    0 upgraded, 5 newly installed, 0 to remove and 49 not upgraded.
    Need to get 4709 kB of archives.
    .......
    -- Build files have been written to: /tmp/sample/release
    Scanning dependencies of target bootstrap
    [ 33%] Building CXX object CMakeFiles/bootstrap.dir/src/register_handler.cpp.o
    [ 66%] Building CXX object CMakeFiles/bootstrap.dir/src/handlers/event_handler.cpp.o
    [100%] Linking CXX executable /tmp/bin/bootstrap
    [100%] Built target bootstrap
    [2022-01-26 08:41:55] [INFO] [S-CLI] - End the pre-action
     Checking Service, Function (1.46s)
     Creating Service, Function (4.28s)
    
    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:       cppEventFunc
        runtime:    custom
        handler:    index.handler
        memorySize: 512
        timeout:    10
  5. 执行以下命令,远程调用函数。
    s invoke -e "{}"
    输出示例:
    ========= FC invoke Logs begin =========
    FC Initialize Start RequestId: 9c7ff302-6a67-42b3-9076-3790d1c1****
    FC Initialize End RequestId: 9c7ff302-6a67-42b3-9076-3790d1c1****
    /invoke is called.
    FC Invoke Start RequestId: 9c7ff302-6a67-42b3-9076-3790d1c1****
    2022-01-26T08:46:00 9c7ff302-6a67-42b3-9076-3790d1c19e4b [INFO] handling invoke
    FC Invoke End RequestId: 9c7ff302-6a67-42b3-9076-3790d1c1****
    
    Duration: 0.72 ms, Billed Duration: 1 ms, Memory Size: 512 MB, Max Memory Used: 2.11 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    {}
    
    End of method: invoke