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

操作步骤

  1. 执行以下命令,初始化项目。
    s init fc-custom-dart-event -d fc-custom-dart-event
  2. 执行以下命令,进入项目目录。
    cd fc-custom-dart-event
  3. 可选:按需修改函数的示例代码,实现二次开发。
  4. 执行以下命令,部署项目。
    s deploy -y
    输出示例:
    [2022-01-26 09:12:31] [INFO] [S-CLI] - Start the pre-action
    [2022-01-26 09:12:31] [INFO] [S-CLI] - Action: make build
    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:0ddfc141f0f03b41822ebf6b7245f5fbfcdea89be08f7617f49a7b01a157****
    Status: Downloaded newer image for google/dart:2.8.4
    Generated: /tmp/code/index.exe
    chmod +x code/bootstrap
    [2022-01-26 09:13:23] [INFO] [S-CLI] - End the pre-action
     Checking Service, Function (2.19s)
     Creating Service, Function (27.94s)
    
    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
    
    helloworld:
      region:   cn-hangzhou
      service:
        name: hello-world-service
      function:
        name:       dartEventFunc
        runtime:    custom
        handler:    index.handler
        memorySize: 1024
        timeout:    60
  5. 执行以下命令,远程调用函数。
    s invoke -e "hello"
    输出示例:
    ========= FC invoke Logs begin =========
    FC Initialize Start RequestId: a8732093-72c6-4c24-97b5-bd17f0dd****
    init
    FC Initialize End RequestId: a8732093-72c6-4c24-97b5-bd17f0dd****
    FC Invoke Start RequestId: a8732093-72c6-4c24-97b5-bd17f0dd****
    hello
    FC Invoke End RequestId: a8732093-72c6-4c24-97b5-bd17f0dd****
    
    Duration: 0.94 ms, Billed Duration: 1 ms, Memory Size: 1024 MB, Max Memory Used: 17.70 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    OK
    
    
    End of method: invoke