通过函数计算的Custom Runtime,您可以在函数计算平台使用C++来编写函数。本文介绍快速部署和调用C++函数的详细步骤。

前提条件

您已安装8.6.0及以上版本的Node.js。详情请参见官方下载地址
说明 如果您已安装Funcraft可直接跳转至步骤二:部署和调用函数

步骤一:准备环境

安装Funcraft,最简单的方式就是直接下载可执行的二进制文件。

  1. 安装Funcraft到本机。详情请参见安装Funcraft
  2. 执行fun --version检查安装是否成功。
  3. 执行fun config配置Funcraft。然后按照提示依次配置Account IDAccess Key IDAccess Key SecretDefault region name
    fun config
    Aliyun Account ID 1234xxx
    Aliyun Access Key ID xxxx
    Aliyun Access Key Secret xxxx
    Default region name cn-xxxx
    The timeout in seconds for each SDK client invoking 300
    The maximum number of retries for each SDK client 5
    Allow to anonynously report usage statistics to improve the tool over time? (Y/n)

步骤二:部署和调用函数

  1. 执行以下命令克隆示例工程到本地。
    git clone https://github.com/awesome-fc/fc-custom-demo          
    说明 如果您没有安装Git,可以直接在浏览器地址栏输入https://github.com/awesome-fc/fc-custom-demo,然后单击下载按钮直接下载。
  2. 执行以下命令进入克隆的示例项目中。
    cd fc-custom-demo
    cd cpp-demo
  3. 执行以下命令将项目部署到函数计算。
    make deploy
    返回结果如下。
    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
     ---> 5bbdcf6377bd
    ...
    Step 3/3 : RUN apt-get install -y cmake
     ---> Using cache
     ---> a244cd26cec2
    Successfully built a244cd26cec2
    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
    ...
    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
    ...
    -- Build files have been written to: /tmp/sample/release
    ...
    [100%] Built target bootstrap
    fun deploy -y
    
    Waiting for service cpp_demo to be deployed...
            Waiting for function fc_cpp_event to be deployed...
                    Waiting for packaging function fc_cpp_event code...
                    The function fc_cpp_event has been packaged. A total of 2 files were compressed and the final size was 446.51 KB
            function fc_cpp_event deploy success
            Waiting for function fc_cpp_http to be deployed...
                    Waiting for packaging function fc_cpp_http code...
                    The function fc_cpp_http has been packaged. A total of 2 files were compressed and the final size was 446.51 KB
                    Waiting for HTTP trigger http_t to be deployed...
                    triggerName: http_t
                    methods: [ 'GET', 'POST', 'PUT', 'DELETE' ]
                    url: https://123456789.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/cpp_demo/fc_cpp_http/
                    Http Trigger will forcefully add a 'Content-Disposition: attachment' field to the response header, which cannot be overwritten
                    and will cause the response to be downloaded as an attachment in the browser. This issue can be avoided by using CustomDomain.
    
                    trigger http_t deploy success
            function fc_cpp_http deploy success
    service cpp_demo deploy success
    
    Detect 'DomainName:Auto' of custom domain 'my_domain'
    Fun will reuse the temporary domain http://17904911-123456789.test.functioncompute.com, expired at 2020-05-06 20:41:51, limited by 1000 per day.
    
    Waiting for custom domain my_domain to be deployed...
    custom domain my_domain deploy success
  4. 执行以下命令调用部署的函数。
    fun invoke cpp_demo/fc_cpp_event -e "Hello World"
    返回结果如下。
    ...
    ========= FC invoke Logs begin =========
    /invoke is called.
    FC Invoke Start RequestId: bf282a87-0f0b-4953-b159-a31792bad22a
    2020-04-27T08:01:08 bf282a87-0f0b-4953-b159-a31792bad22a [INFO] handling invoke
    FC Invoke End RequestId: bf282a87-0f0b-4953-b159-a31792bad22a
    
    Duration: 9.11 ms, Billed Duration: 100 ms, Memory Size: 512 MB, Max Memory Used: 4.25MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    Hello World
    $ curl http://17904911-123456789.test.functioncompute.com -d "Hello World"
    Hello World