通过函数计算的Custom Runtime+HTTP触发器的方式,可以将C++的Web项目一键迁移至云上,并可直接使用浏览器或者cURL等HTTP客户端工具访问函数。

前提条件

您已完成以下操作:
说明 本文提供的示例适用于安装Docker的场景。如果您不想使用Docker,那么您需要安装.NET Core 3.1。安装详情,请参见dot.net.core3.1。运行命令详情,请参见fc-custom-demo

操作步骤

  1. 执行以下命令克隆示例工程到本地。
    git clone https://github.com/awesome-fc/fc-custom-demo          
    说明 如果您没有安装Git,可以直接在浏览器地址栏输入https://github.com/awesome-fc/fc-custom-demo,然后单击下载按钮直接下载示例代码。
  2. 可选:修改cpp-http-demo目录中的示例代码,实现您的业务逻辑。
  3. 执行以下命令进入克隆的示例项目中。
    1. 执行以下命令进入fc-custom-demo项目。
      cd fc-custom-demo
    2. 执行以下命令进入cpp-http-demo项目中。
      cd cpp-http-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
  5. 执行以下命令将项目部署到云上。
    fun deploy -y
    预期输出:
    Waiting for service cpp_http_demo to be deployed...
            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.37 KB
                    Waiting for HTTP trigger http_t to be deployed...
                    triggerName: http_t
                    methods: [ 'GET', 'POST', 'PUT', 'DELETE' ]
                    trigger http_t deploy success
            function fc_cpp_http deploy success
    service cpp_http_demo deploy success
    
    Detect 'DomainName:Auto' of custom domain 'my_domain'
    The assigned temporary domain is http://********-****************.test.functioncompute.com,expired at 2021-04-03 15:07:42, limited by 1000 per day.
    
    Waiting for custom domain my_domain to be deployed...
    custom domain my_domain deploy success
  6. 执行以下命令调用部署函数。
    注意
    • http://********-*********.test.functioncompute.com可以从步骤5的返回结果中获取。
    • 如果您使用的是Windows操作系统,您需要将bootstrap文件格式修改为UNIX格式。
    curl http://********-****************.test.functioncompute.com -d "Hello World"
    预期输出:
    Hello World