通过函数计算的Custom Runtime结合HTTP触发器的方式,可以将C++的Web项目一键迁移至云上,并可直接使用浏览器或者cURL等HTTP客户端工具访问函数。您可按需修改C++的示例代码实现二次开发。

前提条件

您已完成以下操作:

操作步骤

  1. 在目标目录中,执行以下命令,初始化项目。
    s init start-fc-custom-samples -d start-fc-custom-samples
  2. 执行以下命令,进入项目目录。
    cd start-fc-custom-samples/cpp/http-demo
  3. 可选:按需修改示例文件/sample/src/handlers/http_handler.cppHttpHandler::OnInvokeHttpHandler::OnInitialize参数实现二次开发。
  4. 执行以下命令,安装依赖。
    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
     ---> e1d8dfe9d0d4
    Step 2/3 : RUN apt-get update
     ---> Using cache
     ---> 41399c348fcd
    Step 3/3 : RUN apt-get install -y cmake
     ---> Using cache
     ---> 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/http_handler.cpp.o
    [100%] Linking CXX executable /tmp/bin/bootstrap
    [100%] Built target bootstrap
  5. 执行以下命令,部署项目。
    s deploy -y
    输出示例:
    [2021-09-09T10:56:34.419] [INFO ] [S-CLI] - Start ...
    [2021-09-09T10:56:34.950] [INFO ] [FC-DEPLOY] - Using region: cn-hangzhou
    [2021-09-09T10:56:34.950] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-09-09T10:56:34.951] [INFO ] [FC-DEPLOY] - Using accessKeyID: 188077086902****
    [2021-09-09T10:56:34.951] [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-09T10:56:35.289] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
    [2021-09-09T10:56:35.570] [INFO ] [FC-DEPLOY] - Checking Function cppHttpFunc exists
    [2021-09-09T10:56:35.956] [INFO ] [FC-DEPLOY] - Checking Trigger http_t exists
    [2021-09-09T10:56:36.035] [INFO ] [FC-DEPLOY] - Checking Trigger http_t exists
      Make service custom-demo success.
      Make function custom-demo/cppHttpFunc success.
      Make trigger custom-demo/cppHttpFunc/http_t success.
    [2021-09-09T10:56:40.239] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
    [2021-09-09T10:56:40.286] [INFO ] [FC-DEPLOY] - Checking Function cppHttpFunc exists
    [2021-09-09T10:56:40.365] [INFO ] [FC-DEPLOY] - Checking Trigger http_t exists
    [2021-09-09T10:56:40.488] [INFO ] [FC-DEPLOY] - Using customDomain: auto: fc will try to generate related custom domain resources automatically
      End of request
      Deployed.
      End of request
    [2021-09-09T10:56:44.884] [INFO ] [FC-DEPLOY] - Generated auto custom domain: cpphttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
    [2021-09-09T10:56:44.885] [INFO ] [FC-DEPLOY] - Creating custom domain: cpphttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
    [2021-09-09T10:56:45.033] [INFO ] [FC-DOMAIN] - Creating custom domain: cpphttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
    
    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-cpp_http_demo-fc_cpp_http:
      region: cn-hangzhou
      service:
        name: custom-demo
      function:
        name: cppHttpFunc
        runtime: custom
        handler: http.handler
        memorySize: 512
        timeout: 3
      url:
        system_url: >-
          https://188077086902****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/custom-demo/cppHttpFunc/
        custom_domain:
          - domain: >-
              http://cpphttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
      triggers:
        - type: http
          name: http_t

执行结果

成功部署该项目后,您可以在执行输出中查看到函数计算生成的临时域名,通过该临时域名可以访问刚部署的应用,例如执行以下命令访问应用:

curl http://cpphttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
说明 临时域名仅用作演示以及开发,具有时效性。如需用作生产,请绑定已经在阿里云备案的域名。详细信息,请参见绑定自定义域名