通过函数计算的Custom Runtime+HTTP Trigger的方式,您可以将Python的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. 可选:修改python37-http-demo目录中的示例代码,实现您的业务逻辑。
  3. 执行以下命令进入克隆的示例项目中。
    1. 执行以下命令进入fc-custom-demo目录。
      cd fc-custom-demo
    2. 执行以下命令进入python37-http-demo目录。
      cd python37-http-demo
  4. 执行以下命令安装依赖。
    fun install -v
    预期输出。
    using template: template.yml
    start installing function dependencies without docker
    
    building python37-demo/fc-python37-http
    Funfile exist, Fun will use container to build forcely
    Step 1/3 : FROM registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-custom:build-1.9.6
     ---> a9bade1e4f9c
    Step 2/3 : RUN fun-install pip -i https://pypi.tuna.tsinghua.edu.cn/simple install flask
     ---> Using cache
     ---> c15874870502
    Step 3/3 : RUN fun-install pip -i https://pypi.tuna.tsinghua.edu.cn/simple install gunicorn
     ---> Using cache
     ---> dc1f3f69d494
    sha256:dc1f3f69d494e914e7a6c73b64eec5d4744baca6d976a203a179d3ef2dd9691c
    Successfully built dc1f3f69d494
    Successfully tagged fun-cache-08b1a909-bc37-4a1c-bb4a-4cca273f759f:latest
    copying function artifact to C:\Users\demo\fc-custom-demo\python37-http-demo
    
    Install Success
  5. 执行以下命令将项目部署至云上。
    fun deploy -y
    预期输出。
    Waiting for service python37-demo to be deployed...
            Waiting for function fc-python37-http to be deployed...
                    Waiting for packaging function fc-python37-http code...
                    The function fc-python37-http has been packaged. A total of 368 files were compressed and the final size was 1.49 MB
                    Waiting for HTTP trigger http_t to be deployed...
                    triggerName: http_t
                    methods: [ 'GET', 'POST' ]
                    trigger http_t deploy success
            function fc-python37-http deploy success
    service python37-demo deploy success
    
    Detect 'DomainName:Auto' of custom domain 'my_domain'
    Fun will reuse the temporary domain http://********-*********.test.functioncompute.com, expired at 2020-12-20 15:18:04, limited by 1000 per day.
    
    Waiting for custom domain my_domain to be deployed...
    custom domain my_domain deploy success
  6. 执行以下命令调用部署的函数。
    curl -v http://********-*********.test.functioncompute.com
    说明
    • http://********-*********.test.functioncompute.com可以从步骤5的返回结果中获取。
    • 如果您使用的是Windows操作系统,则需将bootstrap文件格式修改为UNIX格式。
    预期输出。
    * Connected to ********-****************.test.functioncompute.com (47.141.XX.XX) port 80 (#0)
    > GET / HTTP/1.1
    > Host: ********-****************.test.functioncompute.com
    > User-Agent: curl/7.64.1
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Access-Control-Expose-Headers: Date,x-fc-request-id,x-fc-error-type,x-fc-code-checksum,x-fc-invocation-duration,x-fc-max-memory-usage,x-fc-log-result,x-fc-invocation-code-version
    < Content-Length: 12
    < Content-Type: text/html; charset=utf-8
    < X-Fc-Code-Checksum: 6465955828********0
    < X-Fc-Invocation-Duration: 3
    < X-Fc-Invocation-Service-Version: LATEST
    < X-Fc-Max-Memory-Usage: 85.20
    < X-Fc-Request-Id: 1a26ecd3-3a6b-4b1c-9c33-********c7ec
    < Date: Thu, 10 Dec 2020 07:23:21 GMT
    <
    * Connection #0 to host ********-****************.test.functioncompute.com left intact
    Hello World!* Closing connection 0