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

前提条件

您已完成以下操作:

操作步骤

  1. 在目标目录中,执行以下命令,初始化项目。
    s init start-fc-custom-container-http-cpp -d start-cc-http-cpp
    -d用于指定项目名称,本示例项目名称为start-cc-http-cpp。
    说明 若已存在该项目则跳过此步骤。
    输出示例:
     Serverless Awesome: https://github.com/Serverless-Devs/package-awesome
    
     file decompression completed
     please select credential alias default
    
     Thanks for using Serverless-Devs
     You could [cd /test/start-cc-http-cpp] and enjoy your serverless journey!
     If you need help for this example, you can use [s -h] after you enter folder.
     Document Star:https://github.com/Serverless-Devs/Serverless-Devs
  2. 执行以下命令,进入项目目录:
    cd start-cc-http-cpp
  3. 执行以下命令,安装依赖:
    s build --use-docker --dockerfile ./code/Dockerfile
    输出示例:
    [2021-10-12T19:34:28.192] [INFO ] [S-CLI] - Start ...
    [2021-10-12T19:34:29.388] [INFO ] [FC-BUILD] - Build artifact start...
    [2021-10-12T19:34:29.404] [INFO ] [FC-BUILD] - Use docker for building.
    [2021-10-12T19:34:29.405] [INFO ] [FC-BUILD] - Building image...
    Sending build context to Docker daemon  3.707MB
    Step 1/16 : FROM python:stretch
     ......
    Step 16/16 : ENTRYPOINT ["/usr/src/app/bootstrap"]
     ---> Running in b385447c0c13
    Removing intermediate container b385447c0c13
     ---> 13e11802c0e3
    Successfully built 13e11802c0e3
    Successfully tagged registry.cn-shenzhen.aliyuncs.com/fc-example/serverless-devs-laravel.laravel:LATEST-1632463623072
    Build image(registry.cn-shenzhen.aliyuncs.com/fc-example/serverless-devs-laravel.laravel:LATEST-1632463623072) successfully
    [2021-10-12T19:34:40.035] [INFO ] [FC-BUILD] - Build artifact successfully.
    
    Tips for next step
    ======================
    * Invoke Event Function: s local invoke
    * Invoke Http Function: s local start
    * Deploy Resources: s deploy
    End of method: build
  4. 执行以下命令,部署项目:
    s deploy --push-registry acr-internet -y
    输出示例:
    [2021-10-12T19:37:29.450] [INFO ] [S-CLI] - Start ...
    [2021-10-12T19:37:30.625] [INFO ] [FC-DEPLOY] - Using region: cn-shenzhen
    [2021-10-12T19:37:30.626] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-10-12T19:37:30.626] [INFO ] [FC-DEPLOY] - Using accessKeyID: LTAI4G4cwJkK4Rza6xd9****
    [2021-10-12T19:37:30.626] [INFO ] [FC-DEPLOY] - Using accessKeySecret: eCc0GxSpzfq1DVspnqqd6nmYNN**** 
     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-10-12T19:37:31.557] [INFO ] [FC-DEPLOY] - Checking Service container-demo exists
    [2021-10-12T19:37:31.863] [INFO ] [FC-DEPLOY] - Setting role: AliyunFCDefaultRole
    [2021-10-12T19:37:32.424] [INFO ] [RAM] - Checking Role AliyunFCDefaultRole exists
    [2021-10-12T19:37:32.744] [INFO ] [RAM] - Updating role: AliyunFCDefaultRole
    [2021-10-12T19:37:32.851] [INFO ] [RAM] - Checking Plicy AliyunFCDefaultRolePolicy exists
    [2021-10-12T19:37:32.975] [WARN ] [FC-DEPLOY] - Reminder --push-registry: will be deprecated soon.
    [2021-10-12T19:37:32.977] [INFO ] [FC-DEPLOY] - Checking Function cpp-http-function exists
    [2021-10-12T19:37:33.182] [INFO ] [FC-DEPLOY] - Using image registry: registry.cn-shenzhen.aliyuncs.com
    [2021-10-12T19:37:33.373] [INFO ] [FC-DEPLOY] - Try to use a temporary token for docker login
    WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
    Configure a credential helper to remove this warning. See
    https://docs.docker.com/engine/reference/commandline/login/#credentials-store
    
    Login to registry: registry.cn-shenzhen.aliyuncs.com with user: cr_temp_user
    Pushing docker image: registry.cn-shenzhen.aliyuncs.com/fc-example/serverless-devs-laravel.laravel:LATEST-1632463623072...
    The push refers to repository [registry.cn-shenzhen.aliyuncs.com/fc-example/serverless-devs-laravel.laravel]
    c238f47ec248: Pushed
    c78169a17b01: Layer already exists
    59bed2cf68a1: Layer already exists
    12ef3c2ef038: Layer already exists
    LATEST-1632463623072: digest: sha256:139893284e806c355b1434bd803d5317fe45356c46b53ec2a10579a3b693b071 size: 1155
    [2021-10-12T19:37:36.205] [INFO ] [FC-DEPLOY] - Checking Trigger httpTrigger exists
    [2021-10-12T19:37:36.268] [INFO ] [FC-DEPLOY] - Checking Trigger httpTrigger exists
     Make service container-demo success.
     Make function container-demo/cpp-http-function success.
     Make trigger container-demo/cpp-http-function/httpTrigger success.
    [2021-10-12T19:37:37.289] [INFO ] [FC-DEPLOY] - Checking Service container-demo exists
    [2021-10-12T19:37:37.725] [INFO ] [FC-DEPLOY] - Checking Function cpp-http-function exists
    [2021-10-12T19:37:38.195] [INFO ] [FC-DEPLOY] - Checking Trigger httpTrigger exists
    [2021-10-12T19:37:38.646] [INFO ] [FC-DEPLOY] - Using customDomain: auto: fc will try to generate related custom domain resources automatically
     End of request
     Deployed.
     End of request
    [2021-10-12T19:37:46.987] [INFO ] [FC-DEPLOY] - Generated auto custom domain: cpp-http-function.container-demo.188077086902****.cn-shenzhen.fc.devsapp.net
    [2021-10-12T19:37:46.987] [INFO ] [FC-DEPLOY] - Creating custom domain: cpp-http-function.container-demo.188077086902****.cn-shenzhen.fc.devsapp.net
    [2021-10-12T19:37:47.165] [INFO ] [FC-DOMAIN] - Creating custom domain: cpp-http-function.container-demo.188077086902****.cn-shenzhen.fc.devsapp.net
    
    There is auto config in the service: container-demo
    
    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
    
    
    
    customContainer-demo:
      region:   cn-shenzhen
      service:
        name: container-demo
      function:
        name:       cpp-http-function
        runtime:    custom-container
        handler:    not-used
        memorySize: 256
        timeout:    60
      url:
        system_url:    https://188077086902****.cn-shenzhen.fc.aliyuncs.com/2016-08-15/proxy/container-demo/cpp-http-function/
        custom_domain:
          -
            domain: http://cpp-http-function.container-demo.188077086902****.cn-shenzhen.fc.devsapp.net
      triggers:
        -
          type: http
          name: httpTrigger

执行结果

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

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