开发函数计算的正确姿势 —— Http Trigger 本地运行调试

本文涉及的产品
简介: 前言 首先介绍下在本文出现的几个比较重要的概念: __函数计算(Function Compute)__: 函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传。

前言

首先介绍下在本文出现的几个比较重要的概念:

__函数计算(Function Compute)__: 函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传。函数计算准备计算资源,并以弹性伸缩的方式运行用户代码,而用户只需根据实际代码运行所消耗的资源进行付费。函数计算更多信息 参考

__Fun__: Fun 是一个用于支持 Serverless 应用部署的工具,能帮助您便捷地管理函数计算、API 网关、日志服务等资源。它通过一个资源配置文件(template.yml),协助您进行开发、构建、部署操作。Fun 的更多文档 参考

__Fun Local__: Fun Local 作为 Fun 的一个子命令存在,可以直接通过 fun local 命令使用。Fun Local 工具可以将函数计算中的函数在本地完全模拟运行,并提供单步调试的功能,旨在弥补函数计算相对于传统应用开发体验上的短板,并为用户提供一种解决函数计算问题排查的新途径。

《开发函数计算的正确姿势 —— 使用 Fun Local 本地运行与调试》中,我们介绍了通过事件的方式在本地触发函数运行以及调试的方法。

而这一次,我们本地调试运行 Http Trigger 函数的方法。

备注: 本文介绍的技巧需要 Fun 版本大于等于 2.8.0。

Fun Local Start 命令格式

使用 fun local invoke -h 可以查看 fun local invoke 的帮助信息:

  Usage: fun local start [options]

  Allows you to run the Function Compute application locally for quick development & testing.
    It will start an http server locally to receive requests for http triggers and apis.
    It scans all functions in template.yml. If the resource type is HTTP, it will be registered to this http server, which can be triggered by the browser or some http tools.
    For other types of functions, they will be registered as apis, which can be called by sdk in each language or directly via api.

    Function Compute will look up the code by CodeUri in template.yml.
    For interpreted languages, such as node, python, php, the modified code will take effect immediately, without restarting the http server.
    For compiled languages ​​such as java, we recommend you set CodeUri to the compiled or packaged localtion.
    Once compiled or packaged result changed, the modified code will take effect without restarting the http server.

  Options:

    -d, --debug-port <port>      specify the sandboxed container starting in debug mode, and exposing this port on localhost
    -c, --config <ide/debugger>  output ide debug configuration. Options are vscode
    -h, --help                   output usage information

本地运行 Http Trigger 函数

运行命令格式为:

fun local start [options]

其中 options 是可以省略的。

执行 fun local start 后,fun 会首先启动一个 http server,以提供 http 的服务。然后 fun 会扫描 template.yml 中描述的所有配置了 Http Trigger 的函数,注册到 http server 中。注册成功后,就可以通过浏览器或者其他的 http 工具访问了。

比如,对于在 local_http 下执行 fun local start 后,会显示所有的 http trigger 信息:

打开任意一个 url,即可通过 http trigger 的方式触发函数运行。

以下是一个访问 nodejs http trigger 的动态演示(其他类型的 runtime,比如 python、php 使用体验一致):

本地调试 Http Trigger 函数

备注:Fun Local 涉及到的 debugging 技术全部都基于各个语言通用的调试协议实现的,因此无论什么语言的开发者,即使不喜欢用 VSCode,只要使用对应语言的 remote debugging 方法都可以进行调试。

Http Trigger 本地调试的方法与使用事件触发函数的方法一致 ———— 通过 -d, --debug-port <port> 选项。同时还支持 -c, --config <ide/debugger>,支持在调试时,显示调试 ide 配置。

调试方法为,首先通过 fun local start --debug 3000 --config vscode 启动服务,然后会看到 template.yml 中声明的函数都被注册成功:

根据服务名、函数名或者 http trigger 触发器名称选择合适的 url,使用浏览器打开会看到浏览器页面一直无响应,但在终端会看到日志输出:

skip pulling image aliyunfc/runtime-python3.6:1.2.0...
you can paste these config to .vscode/launch.json, and then attach to your running function
///////////////// config begin /////////////////
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "fc/local-http-demo/python3",
            "type": "python",
            "request": "attach",
            "host": "localhost",
            "port": 3000,
            "pathMappings": [
                {
                    "localRoot": "/Users/tan/fun_local_http_demo/python3",
                    "remoteRoot": "/code"
                }
            ]
        }
    ]
}
///////////////// config end /////////////////
FunctionCompute python3 runtime inited.
FC Invoke Start RequestId: 04c57fba-cbe9-4c1f-8c57-f8e0b539fa08

将日志中的配置信息复制到 vscode 调试器中,在代码中下好断点,单击开始调试即可。更多信息,请参考

以下是一个调试 python http trigger 的动态演示(其他类型的 runtime,比如 nodejs、php 使用体验基本一致):

热加载

本地运行、调试 Http Trigger 时,支持热加载。

当通过 fun local start 启动本地服务后,即使修改了代码,也无需重启本地服务,可以直接刷新网页或者重新触发函数即可运行变更后的函数。

以下是一个 nodejs 热加载的动态演示(其他类型的 runtime,比如 php、python 类似)。

备注:请提前在函数目录执行 npm install,用于初始化函数依赖的 nodejs 模块。

其他功能的支持

本文主要介绍了本地运行、调试 Http Trigger 函数的体验,其他功能,比如环境变量、Initializer、Credentials 等,请参考

示例代码

本文讲解涉及到的示例代码,托管在 github 上。项目目录结构如下:

.
├── nodejs6
│   ├── README.md
│   ├── index.js
│   └── package.json
├── nodejs8
│   ├── README.md
│   ├── index.js
│   └── package.json
├── php7.2
│   ├── README.md
│   └── index.php
├── python2.7
│   ├── README.md
│   └── index.py
├── python3
│   ├── README.md
│   └── index.py
└── template.yml

template.yml 定义了函数计算模型,其中定义了一个名为 local-http-demo 的服务,并在该服务下,定义了 5 个函数,名称分别是 nodejs6、nodejs8、php72、python27、python3。它们对应的代码目录由 template 中的 CodeUri 定义,分别位于 nodejs6、nodejs8、php7.2、python2.7、python3 目录。

本文作者:小默

相关实践学习
基于函数计算一键部署掌上游戏机
本场景介绍如何使用阿里云计算服务命令快速搭建一个掌上游戏机。
建立 Serverless 思维
本课程包括: Serverless 应用引擎的概念, 为开发者带来的实际价值, 以及让您了解常见的 Serverless 架构模式
目录
相关文章
|
3月前
|
监控 Serverless
在函数计算中,如果你设置了每40分钟运行一次任务
【1月更文挑战第23天】【1月更文挑战第111篇】在函数计算中,如果你设置了每40分钟运行一次任务
40 9
|
4月前
|
JavaScript 前端开发 Serverless
函数计算只支持Node.js,我用C++写的程序怎么运行?
函数计算只支持Node.js,我用C++写的程序怎么运行?
90 1
|
4月前
|
JavaScript
如何让Vue项目本地运行的时候,同时支持http://localhost和http://192.168.X.X访问?
如何让Vue项目本地运行的时候,同时支持http://localhost和http://192.168.X.X访问?
|
1月前
|
JavaScript 前端开发 Serverless
函数计算新功能— 支持 Node.js 18 、Node.js 20 运行时
从2024年2月起,函数计算正式发布 Node.js 18 运行时和 Nodejs.20 运行时,函数计算2.0和函数计算3.0都支持新的运行时,目前新运行时处在公测状态,欢迎大家来体验。
454 0
|
2月前
|
人工智能 Serverless API
AI 绘画平台难开发,难变现?试试 Stable Diffusion API Serverless 版解决方案
AI 绘画平台难开发,难变现?试试 Stable Diffusion API Serverless 版解决方案
|
2月前
|
小程序 IDE Serverless
【经验分享】支付宝小程序serverless云开发拓荒
【经验分享】支付宝小程序serverless云开发拓荒
80 0
|
3月前
|
人工智能 Serverless API
AI 绘画平台难开发,难变现?试试 Stable Diffusion API Serverless 版解决方案
AI 绘画平台难开发,难变现?试试 Stable Diffusion API Serverless 版解决方案
7680 132
|
3月前
|
存储 编解码 Serverless
Serverless应用引擎部署问题之项目无法运行如何解决
Serverless部署是指将应用程序部署到无服务器架构中,该架构允许开发者专注于代码而无需关心底层服务器的运行和维护;针对Serverless部署过程中可能遇到的挑战,本合集提供全面的指南和最佳实践,帮助开发者顺利实现应用的无服务器化部署。
|
3月前
|
人工智能 Serverless API
AI 绘画平台难开发,难变现?试试 Stable Diffusion API Serverless 版解决方案
为了帮助用户高效率、低成本应对企业级复杂场景,函数计算团队正式推出 Stable Diffusion API Serverless 版解决方案,通过使用该方案,用户可以充分利用 Stable Diffusion +Serverless 技术优势快速开发上线 AI 绘画应用,期待为广大开发者 AI 绘画创业及变现提供思路。
87307 4
|
3月前
|
Serverless
函数计算里FC 3.0中,http触发器配置域名
函数计算里FC 3.0中,http触发器配置域名

相关产品

  • 函数计算