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

前提条件

您已完成以下操作:

操作步骤

  1. 在目标目录中,执行以下命令,初始化项目。
    s init start-fc-custom-samples -d start-fc-custom-samples
  2. 执行以下命令,进入项目目录。
    cd start-fc-custom-samples/dart/event-demo/
  3. 执行以下命令,安装依赖。
    make biuld
    输出示例:
    docker run --rm -it -v $(pwd):/tmp google/dart:2.8.4 bash -c "export PUB_HOSTED_URL=https://pub.flutter-io.cn && cd tmp/code && dart2native index.dart && mv index.exe bootstrap"
    Unable to find image 'google/dart:2.8.4' locally
    2.8.4: Pulling from google/dart
    291bafe4382e: Pull complete
    0cbc81b3bc90: Pull complete
    Digest: sha256:0ddfc141f0f03b41822ebf6b7245f5fbfcdea89be08f7617f49a7b01a1571dd1
    Status: Downloaded newer image for google/dart:2.8.4
    Generated: /tmp/code/index.exe
    chmod +x code/bootstrap
  4. 执行以下命令,部署项目。
    s deploy -y
    输出示例:
    [2021-09-09T10:14:08.452] [INFO ] [S-CLI] - Start ...
    [2021-09-09T10:14:08.992] [INFO ] [FC-DEPLOY] - Using region: cn-hangzhou
    [2021-09-09T10:14:08.993] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-09-09T10:14:08.993] [INFO ] [FC-DEPLOY] - Using accessKeyID: 188077086902****
    [2021-09-09T10:14:08.993] [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:14:09.272] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
    [2021-09-09T10:14:09.502] [INFO ] [FC-DEPLOY] - Checking Function dartEventFunc exists
      Make service custom-demo success.
      Make function custom-demo/dartEventFunc success.
    [2021-09-09T10:14:37.070] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
    [2021-09-09T10:14:37.108] [INFO ] [FC-DEPLOY] - Checking Function dartEventFunc exists
    
    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-dart-demo-dart-func:
      region: cn-hangzhou
      service:
        name: custom-demo
      function:
        name: dartEventFunc
        runtime: custom
        handler: index.handler
        memorySize: 1024
        timeout: 60
  5. 执行以下命令,远程调用函数。
    s invoke -e "hello"
    输出示例:
    [2021-09-09T10:15:54.604] [INFO ] [S-CLI] - Start ...
    ========= FC invoke Logs begin =========
    FC Initialize Start RequestId: 1ebb693d-86c6-4832-b5ea-034033fef2f2
    init
    FC Initialize End RequestId: 1ebb693d-86c6-4832-b5ea-034033fef2f2
    FC Invoke Start RequestId: 1ebb693d-86c6-4832-b5ea-034033fef2f2
    hello
    FC Invoke End RequestId: 1ebb693d-86c6-4832-b5ea-034033fef2f2
    
    Duration: 5.07 ms, Billed Duration: 6 ms, Memory Size: 1024 MB, Max Memory Used: 15.06 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    OK
    
    
    End of method: invoke