通过函数计算的 Custom Runtime + HTTP Trigger 的方式,可以将 F# 的 ASP.NETCore 项目一键迁移,并可直接使用浏览器或者 curl 等 HTTP 客户端工具访问函数。

前提条件

您已完成以下操作:
说明
  • 本文提供的示例适用于安装 Docker 的场景。如果您不想使用 Docker,那么您需要安装 .NET Core 3.1。安装详情请参见 dot.net.core3.1。运行命令详情请参见以下命令。
    https://github.com/awesome-fc/fc-custom-demo/blob/master/FSharp-demo/Makefile#L10-L18
  • 如果您已安装 Funcraft 可直接跳转至步骤二:部署和调用函数

步骤一:准备环境

安装 Funcraft,最简单的方式就是直接下载可执行的二进制文件。

  1. 安装 Funcraft 到本机。详情请参见安装文档
  2. 执行 fun --version 检查安装是否成功。
  3. 执行 fun config 配置 Funcraft。然后按照提示依次配置 Account IDAccess Key IDAccess Key SecretDefault region name
    $ fun config
    Aliyun Account ID 1234xxx
    Aliyun Access Key ID xxxx
    Aliyun Access Key Secret xxxx
    Default region name cn-xxxx
    The timeout in seconds for each SDK client invoking 300
    The maximum number of retries for each SDK client 5
    Allow to anonynously report usage statistics to improve the tool over time? (Y/n)

步骤二:部署和调用函数

  1. 执行以下命令克隆示例工程到本地。
    git clone https://github.com/awesome-fc/fc-custom-demo          
    说明 如果您没有安装 git,可以直接在浏览器地址栏输入 https://github.com/awesome-fc/fc-custom-demo,然后单击下载按钮直接下载。
  2. 执行以下命令进入克隆的示例项目中。
    cd FSharp-demo
  3. 执行以下命令部署函数。
    $ make deploy
    docker run -it -v $(pwd):/tmp mcr.microsoft.com/dotnet/core/sdk:3.1 bash -c "cd /tmp/FSharpDemo && dotnet publish -r linux-x64 -c Release --self-contained true && cd /tmp/FSharpDemo/bin/Release/netcoreapp3.1/linux-x64/publish && mv FSharpDemo bootstrap && chmod +x bootstrap"
    Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Core
    Copyright (C) Microsoft Corporation. All rights reserved.
    
      Restore completed in 15.2 sec for /tmp/FSharpDemo/FSharpDemo.fsproj.
      FSharpDemo -> /tmp/FSharpDemo/bin/Release/netcoreapp3.1/linux-x64/FSharpDemo.dll
      FSharpDemo -> /tmp/FSharpDemo/bin/Release/netcoreapp3.1/linux-x64/publish/
    fun deploy -y
    ...
    Waiting for service fsharp_demo to be deployed...
            Waiting for function fc_fsharp to be deployed...
                    Waiting for packaging function fc_fsharp code...
                    The function fc_fsharp has been packaged. A total of 338 files were compressed and the final size was 39.3 MB
                    Waiting for HTTP trigger http_t to be deployed...
                    triggerName: http_t
                    methods: [ 'GET', 'POST', 'PUT', 'DELETE' ]
                    url: https://19861144305****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/fsharp_demo/fc_fsharp/
                    Http Trigger will forcefully add a 'Content-Disposition: attachment' field to the response header, which cannot be overwritten 
                    and will cause the response to be downloaded as an attachment in the browser. This issue can be avoided by using CustomDomain.
    
                    trigger http_t deploy success
            function fc_fsharp deploy success
    service fsharp_demo deploy success
    
    Detect 'DomainName:Auto' of custom domain 'my_domain'
    Fun will reuse the temporary domain 16225220-19861144****.test.functioncompute.com, expired at 2020-04-17 10:07:00, limited by 1000 per day.
    
    Waiting for custom domain my_domain to be deployed...
    custom domain my_domain deploy success
    						
  4. 执行以下命令调用部署的函数。

    在该示例中,直接通过curl 或浏览器访问部署成功后输出的临时域名 16225220-198611443****.test.functioncompute.com

    $ curl 16225220-1986114430****.test.functioncompute.com/weatherforecast 
    [{"date":"2020-04-07T07:46:29.4315198+00:00","temperatureC":49,"summary":"Hot","temperatureF":120},{"date":"2020-04-08T07:46:29.431527+00:00","temperatureC":11,"summary":"Bracing","temperatureF":51},{"date":"2020-04-09T07:46:29.4315276+00:00","temperatureC":45,"summary":"Bracing","temperatureF":112},{"date":"2020-04-10T07:46:29.431528+00:00","temperatureC":13,"summary":"Chilly","temperatureF":55},{"date":"2020-04-11T07:46:29.4315284+00:00","temperatureC":-8,"summary":"Mild","temperatureF":18}]