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

前提条件

您已完成以下操作:

操作步骤

  1. 在目标目录中,执行以下命令,初始化项目。
    s init start-fc-custom-samples -d start-fc-custom-samples
  2. 执行以下命令,进入项目目录。
    cd start-fc-custom-samples/cpp/event-demo/
  3. 执行以下命令,安装依赖。
    make build
    输出示例:
    docker build -t fc-cpp-runtime  -f build-image/Dockerfile build-image
    Sending build context to Docker daemon  2.048kB
    Step 1/3 : FROM aliyunfc/runtime-custom:base
    base: Pulling from aliyunfc/runtime-custom
    80369df48736: Already exists
    aaba0609d543: Already exists
    33536ac8210e: Already exists
    35aa8bff600e: Already exists
    6e4c9666aa4e: Already exists
    23f26080a3b8: Already exists
    e4ff0d898211: Already exists
    cc9db5ac5f2e: Already exists
    ab66569cc1ab: Already exists
    548471460061: Already exists
    4cded641b296: Already exists
    c5000ba7c212: Already exists
    f52cab0dab04: Already exists
    7e24034ee7f5: Already exists
    b93b67627729: Already exists
    0d4d86cdd1f0: Already exists
    7fb82f6797a9: Already exists
    4581f7ef00a2: Already exists
    e28564bca6e6: Already exists
    747218a80da2: Already exists
    9fd663374826: Already exists
    797f3ef5d0ac: Already exists
    d38bb193879c: Already exists
    db8b023f4f29: Already exists
    1cd1a11e3d0d: Already exists
    a604135fad89: Already exists
    2e1eb81092cd: Already exists
    b17563598739: Already exists
    a63c04e5e1cb: Already exists
    32d3594c85b0: Already exists
    504482070142: Already exists
    f16ea7bdba96: Already exists
    Digest: sha256:1b4125b440a8ecf5faa2d5f7e242a8080d547538e2870a125cdaae88acd2a3f3
    Status: Downloaded newer image for aliyunfc/runtime-custom:base
     ---> e1d8dfe9d0d4
    Step 2/3 : RUN apt-get update
     ---> Running in 933b754d7552
    Ign:1 http://mirrors.aliyun.com/debian stretch InRelease
    Get:2 http://mirrors.aliyun.com/debian stretch-updates InRelease [93.6 kB]
    Get:3 http://mirrors.aliyun.com/debian stretch-backports InRelease [91.8 kB]
    Get:4 http://mirrors.aliyun.com/debian-security stretch/updates InRelease [53.0 kB]
    Get:5 http://mirrors.aliyun.com/debian stretch Release [118 kB]
    Get:6 http://mirrors.aliyun.com/debian stretch Release.gpg [3177 B]
    Get:7 http://mirrors.aliyun.com/debian stretch-backports/non-free Sources [8412 B]
    Get:8 http://mirrors.aliyun.com/debian stretch-backports/main Sources [557 kB]
    Get:9 http://mirrors.aliyun.com/debian stretch-backports/contrib Sources [6528 B]
    Get:10 http://mirrors.aliyun.com/debian stretch-backports/contrib amd64 Packages [10.9 kB]
    Get:11 http://mirrors.aliyun.com/debian stretch-backports/non-free amd64 Packages [33.2 kB]
    Get:12 http://mirrors.aliyun.com/debian stretch-backports/main amd64 Packages [612 kB]
    Get:13 http://mirrors.aliyun.com/debian-security stretch/updates/contrib Sources [1384 B]
    Get:14 http://mirrors.aliyun.com/debian-security stretch/updates/main Sources [370 kB]
    Get:15 http://mirrors.aliyun.com/debian-security stretch/updates/non-free Sources [2212 B]
    Get:16 http://mirrors.aliyun.com/debian-security stretch/updates/contrib amd64 Packages [1760 B]
    Get:17 http://mirrors.aliyun.com/debian-security stretch/updates/main amd64 Packages [711 kB]
    Get:18 http://mirrors.aliyun.com/debian-security stretch/updates/non-free amd64 Packages [5604 B]
    Get:19 http://mirrors.aliyun.com/debian stretch/main Sources [6736 kB]
    Get:20 https://packages.microsoft.com/repos/microsoft-debian-stretch-prod stretch InRelease [4008 B]
    Get:21 http://mirrors.aliyun.com/debian stretch/non-free Sources [79.1 kB]
    Get:22 http://mirrors.aliyun.com/debian stretch/contrib Sources [44.5 kB]
    Get:23 http://mirrors.aliyun.com/debian stretch/contrib amd64 Packages [50.7 kB]
    Get:24 http://mirrors.aliyun.com/debian stretch/non-free amd64 Packages [78.3 kB]
    Get:25 http://mirrors.aliyun.com/debian stretch/main amd64 Packages [7080 kB]
    Get:26 https://packages.microsoft.com/repos/microsoft-debian-stretch-prod stretch/main amd64 Packages [162 kB]
    Get:27 https://packages.sury.org/php stretch InRelease [6839 B]
    Get:28 https://packages.sury.org/php stretch/main amd64 Packages [335 kB]
    Fetched 17.3 MB in 2s (5864 kB/s)
    Reading package lists...
    Removing intermediate container 933b754d7552
     ---> 41399c348fcd
    Step 3/3 : RUN apt-get install -y cmake
     ---> Running in 65cce5e5b910
    Reading package lists...
    Building dependency tree...
    Reading state information...
    The following additional packages will be installed:
      cmake-data libarchive13 libjsoncpp1 liblzo2-2 libuv1
    Suggested packages:
      codeblocks eclipse ninja-build lrzip
    The following NEW packages will be installed:
      cmake cmake-data libarchive13 libjsoncpp1 liblzo2-2 libuv1
    0 upgraded, 6 newly installed, 0 to remove and 58 not upgraded.
    Need to get 4764 kB of archives.
    After this operation, 23.8 MB of additional disk space will be used.
    Get:1 http://mirrors.aliyun.com/debian stretch/main amd64 cmake-data all 3.7.2-1 [1216 kB]
    Get:2 http://mirrors.aliyun.com/debian stretch/main amd64 liblzo2-2 amd64 2.08-1.2+b2 [55.0 kB]
    Get:3 http://mirrors.aliyun.com/debian stretch/main amd64 libarchive13 amd64 3.2.2-2+deb9u2 [294 kB]
    Get:4 http://mirrors.aliyun.com/debian stretch/main amd64 libjsoncpp1 amd64 1.7.4-3 [75.6 kB]
    Get:5 http://mirrors.aliyun.com/debian stretch/main amd64 libuv1 amd64 1.9.1-3 [84.4 kB]
    Get:6 http://mirrors.aliyun.com/debian stretch/main amd64 cmake amd64 3.7.2-1 [3038 kB]
    debconf: delaying package configuration, since apt-utils is not installed
    Fetched 4764 kB in 0s (12.9 MB/s)
    Selecting previously unselected package cmake-data.
    (Reading database ... 34420 files and directories currently installed.)
    Preparing to unpack .../0-cmake-data_3.7.2-1_all.deb ...
    Unpacking cmake-data (3.7.2-1) ...
    Selecting previously unselected package liblzo2-2:amd64.
    Preparing to unpack .../1-liblzo2-2_2.08-1.2+b2_amd64.deb ...
    Unpacking liblzo2-2:amd64 (2.08-1.2+b2) ...
    Selecting previously unselected package libarchive13:amd64.
    Preparing to unpack .../2-libarchive13_3.2.2-2+deb9u2_amd64.deb ...
    Unpacking libarchive13:amd64 (3.2.2-2+deb9u2) ...
    Selecting previously unselected package libjsoncpp1:amd64.
    Preparing to unpack .../3-libjsoncpp1_1.7.4-3_amd64.deb ...
    Unpacking libjsoncpp1:amd64 (1.7.4-3) ...
    Selecting previously unselected package libuv1:amd64.
    Preparing to unpack .../4-libuv1_1.9.1-3_amd64.deb ...
    Unpacking libuv1:amd64 (1.9.1-3) ...
    Selecting previously unselected package cmake.
    Preparing to unpack .../5-cmake_3.7.2-1_amd64.deb ...
    Unpacking cmake (3.7.2-1) ...
    Setting up libuv1:amd64 (1.9.1-3) ...
    Setting up cmake-data (3.7.2-1) ...
    Processing triggers for libc-bin (2.24-11+deb9u4) ...
    Setting up liblzo2-2:amd64 (2.08-1.2+b2) ...
    Setting up libjsoncpp1:amd64 (1.7.4-3) ...
    Setting up libarchive13:amd64 (3.2.2-2+deb9u2) ...
    Setting up cmake (3.7.2-1) ...
    Processing triggers for libc-bin (2.24-11+deb9u4) ...
    Removing intermediate container 65cce5e5b910
     ---> ed8388f61d09
    Successfully built ed8388f61d09
    Successfully tagged fc-cpp-runtime:latest
    docker run --rm -it -v $(pwd):/tmp fc-cpp-runtime bash -c "cd /tmp && ./build.sh"
    -- The C compiler identification is GNU 6.3.0
    -- The CXX compiler identification is GNU 6.3.0
    -- Check for working C compiler: /usr/bin/cc
    -- Check for working C compiler: /usr/bin/cc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /tmp/cpp_runtime/release
    Scanning dependencies of target cppruntime
    [ 33%] Building CXX object CMakeFiles/cppruntime.dir/src/handler.cpp.o
    [ 66%] Building CXX object CMakeFiles/cppruntime.dir/src/logging.cpp.o
    [100%] Linking CXX shared library /tmp/bin/libcppruntime.so
    [100%] Built target cppruntime
    -- The C compiler identification is GNU 6.3.0
    -- The CXX compiler identification is GNU 6.3.0
    -- Check for working C compiler: /usr/bin/cc
    -- Check for working C compiler: /usr/bin/cc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Configuring done
    -- Generating done
    -- 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
  4. 执行以下命令,部署项目。
    s deploy -y
    输出示例:
    [2021-09-08T17:23:44.089] [INFO ] [S-CLI] - Start ...
    [2021-09-08T17:23:44.631] [INFO ] [FC-DEPLOY] - Using region: cn-hangzhou
    [2021-09-08T17:23:44.632] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-09-08T17:23:44.632] [INFO ] [FC-DEPLOY] - Using accessKeyID: 188077086902****
    [2021-09-08T17:23:44.632] [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-08T17:23:44.925] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
    [2021-09-08T17:23:45.095] [INFO ] [FC-DEPLOY] - Checking Function cppEventFunc exists
      Make service custom-demo success.
      Make function custom-demo/cppEventFunc success.
    [2021-09-08T17:23:49.660] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
    [2021-09-08T17:23:49.694] [INFO ] [FC-DEPLOY] - Checking Function cppEventFunc 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
    
    fc-custom-demo-cppEventFunc:
      region: cn-hangzhou
      service:
        name: custom-demo
      function:
        name: cppEventFunc
        runtime: custom
        handler: event.handler
        memorySize: 512
        timeout: 10
  5. 执行以下命令,远程调用函数。
    s invoke -e "{}"
    输出示例:
    [2021-09-08T17:25:50.317] [INFO ] [S-CLI] - Start ...
    ========= FC invoke Logs begin =========
    FC Initialize Start RequestId: 1a3b5dce-22cd-4c31-85f9-624346439cca
    FC Initialize End RequestId: 1a3b5dce-22cd-4c31-85f9-624346439cca
    /invoke is called.
    FC Invoke Start RequestId: 1a3b5dce-22cd-4c31-85f9-624346439cca
    2021-09-08T09:25:51 1a3b5dce-22cd-4c31-85f9-624346439cca [INFO] handling invoke
    FC Invoke End RequestId: 1a3b5dce-22cd-4c31-85f9-624346439cca
    
    Duration: 3.47 ms, Billed Duration: 4 ms, Memory Size: 512 MB, Max Memory Used: 5.94 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    {}
    
    
    End of method: invoke