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

前提条件

您已完成以下操作:

操作步骤

  1. 执行以下命令克隆示例工程到本地。
    git clone https://github.com/awesome-fc/fc-custom-demo          
    说明 如果您没有安装Git,可以直接在浏览器地址栏输入https://github.com/awesome-fc/fc-custom-demo,然后单击下载按钮直接下载示例代码。
  2. 可选:修改cpp-demo目录中的示例代码,实现您的业务逻辑。
  3. 执行以下命令进入克隆的示例项目中。
    1. 执行以下命令进入fc-custom-demo目录。
      cd fc-custom-demo
    2. 执行以下命令进入cpp-demo目录。
      cd cpp-demo
  4. 执行以下命令将项目部署到函数计算。
    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://1******1-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
  5. 执行以下命令调用部署的函数。
    fun invoke cpp_demo/fc_cpp_event -e "Hello World"
    说明 如果您使用的是Windows操作系统,则需将bootstrap文件格式修改为UNIX格式。
    预期输出。
    ...
    ========= 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://1******1-123456789.test.functioncompute.com -d "Hello World"
    Hello World