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

前言

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

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

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

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

《开发函数计算的正确姿势》系列中,我们介绍过通过事件触发函数的本地运行调试以及 Http Trigger 触发函数的本地运行函数调试。而这一次,我们主要介绍通过 api 的方式本地调用调试运行函数。

备注: 本文介绍的技巧需要 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

本地通过 api 触发函数运行

命令格式与 Http Trigger 一致,即:

fun local start [options]

其中 options 是可以省略的。

执行 fun local start 后,fun 会首先启动一个 http server,以提供 http 的服务。然后 fun 会扫描 template.yml 中描述的所有的函数,并将所有没有配置 Http Trigger 的函数,注册到 http server 中。注册成功后,就可以通过 InvokeFunction API 或者 SDK 进行调用了。

直接通过 API 访问需要进行签名,需要实现 签名认证。这里推荐使用 SDK 进行调用。

我们选择 《开发函数计算的正确姿势 —— 使用 Fun Local 本地运行与调试》 中的代码示例,代码托管在 github

首先,我们通过 fun local start 将服务运行起来,执行日志如下:

$ fun local start

api localdemo/php72 was registered
    url: http://localhost:8000/2016-08-15/services/localdemo/functions/php72/invocations/
api localdemo/python27 was registered
    url: http://localhost:8000/2016-08-15/services/localdemo/functions/python27/invocations/
api localdemo/python3 was registered
    url: http://localhost:8000/2016-08-15/services/localdemo/functions/python3/invocations/
api localdemo/nodejs6 was registered
    url: http://localhost:8000/2016-08-15/services/localdemo/functions/nodejs6/invocations/
api localdemo/nodejs8 was registered
    url: http://localhost:8000/2016-08-15/services/localdemo/functions/nodejs8/invocations/
api localdemo/java8 was registered
    url: http://localhost:8000/2016-08-15/services/localdemo/functions/java8/invocations/

function compute app listening on port 8000!

启动服务后,我们就可以通过 python sdk 进行调用了。

首先安装 fc pytohn sdk

pip install aliyun-fc2

然后编写代码:

import fc2

client = fc2.Client(endpoint='http://localhost:8000', accessKeyID='<your access key id>', accessKeySecret='your access key secret')

resp = client.invoke_function('localdemo', 'php72')

print resp.headers
print resp.data

注意: sdk 代码中配置的 accessKeyId、accessKeySecret 要求与 fun 配置一致,否则调用时,会导致签名认证失败。

执行效果如下图所示:

通过 api 单步调试函数

调试方法与 开发函数计算的正确姿势 —— Http Trigger 本地运行调试 中描述的一致,这里不再重复阐述。

本文作者:小默

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值