可以使用 s local start 命令把 HTTP 函数部署在本地,方便调试。但是此命令并不会执行 pre-deploy 中的 actions。
我尝试了 s build 命令,发现其也不会执行 pre-deploy,可能 Go 语言并不在 s build 命令的考虑中。这里只能自己编译(省略部分输出结果,用 ... 代替):
$ GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o code/main code/main.go
$ s local start
...
[2023-10-26 23:18:36] [INFO] [FC-LOCAL-INVOKE] - CustomDomain auto of http-handler-builtin-runtime-example-service/http-handler-builtin-runtime-example-function was registered
url: http://localhost:7342/
methods: GET,POST
authType: anonymous
...
function compute app listening on port 7342!
使用 curl 命令进行本地请求:
$ curl http://localhost:7342/
2023-10-27 09:31:05
Request Method: GET
$
$ curl -X POST -d 'test POST body' http://localhost:7342/
2023-10-27 09:32:25
Request Method: POST
test POST body
使用 s deploy --use-local -y 命令部署(省略部分输出结果,用 ... 代替):
$ s deploy --use-local -y
[2023-10-27 09:46:38] [INFO] [S-CORE] - Start the pre-action
[2023-10-27 09:46:38] [INFO] [S-CORE] - Action: go mod tidy
[2023-10-27 09:46:39] [INFO] [S-CORE] - Action: GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o main main.go
[2023-10-27 09:46:39] [INFO] [S-CORE] - End the pre-action
...
Tips for next step
======================
* Display information of the deployed resource: s info
* Invoke remote function: s invoke
...
url:
system_url: https://http-hafunction-http-ha-service-syfmwcasfs.cn-zhangjiakou.fcapp.run
system_intranet_url: https://http-hafunction-http-ha-service-syfmwcasfs.cn-zhangjiakou-vpc.fcapp.run
custom_domain:
-
domain: http://http-handler-builtin-runtime-example-function.http-handler-builtin-runtime-example-service.1810657881264284.cn-zhangjiakou.fc.devsapp.net
部署后,输出了云函数的公网 url(system_url)和自定义域名 url(custom_domain);我们也可以使用 s info 命令查看云函数信息;也可以在阿里云控制台查看。
本地调用云函数
因为这是一个 HTTP handler,我们可以用各种方式发起 HTTP 请求,但这里还是介绍下用 s invoke 命令进行远程调用。
首先使用 s cli fc-event http 命令生成调用参数的模版:
$ s cli fc-event http
👓 Parameter Template Path: event-template/http-parameter.json
You could user fc component invoke method and specify the event.
E.g: [s projectName invoke --event-file event-template/http-parameter.json]
$ cat event-template/http-parameter.json
{
"path": "string",
"method": "POST",
"headers": {
"key": "value"
},
"queries": {
"key": "value"
},
"body": "body"
}
$ s instance list
http-handler-builtin-runtime-example-service:
http-handler-builtin-runtime-example-function:
instances:
-
instanceId: c-653b221b-67d7232869314a88a7f9
versionId: 0
$ s instance exec c-653b221b-67d7232869314a88a7f9 -it /bin/bash
root@sr-653ae858-9b5d81f96fda4b2bbebf:/# ls
bin code etc lib media opt root sbin sys usr
boot dev home lib64 mnt proc run srv tmp var
root@sr-653ae858-9b5d81f96fda4b2bbebf:/# cd code/
root@sr-653ae858-9b5d81f96fda4b2bbebf:/code# ls
main main.go
基本信息
请求处理程序分为事件请求处理程序(Event Handler)和 HTTP 请求处理程序(HTTP Handler),其中事件请求由各种事件源触发生成,HTTP 请求则由 HTTP 触发器触发生成。
本文关注 HTTP Handler。编程语言使用 Go。
HTTP Handler
内置运行时
示例项目
这是一个使用内置运行时 go1 实现的 HTTP Handler。
项目结构如下,其中 main.go 实现了 handler,s.yaml 描述云函数的资源、行为:
s.yaml 内容如下:
需要注意的是,
codeUri
目录中的内容是最终的交付物,函数计算最终会把此目录下的内容拷贝到容器/code
目录下。使用 Go 语言时的交付物是一个二进制可执行文件,因此我们要确保这个二进制文件出现在codeUri
指定的目录中,而且文件名是handler
所指定的值。这里通过pre-deploy
指定部署之前的 actions,在部署之前进行编译。main.go 内容如下:
本地调试
可以使用
s local start
命令把 HTTP 函数部署在本地,方便调试。但是此命令并不会执行 pre-deploy 中的 actions。我尝试了
s build
命令,发现其也不会执行 pre-deploy,可能 Go 语言并不在s build
命令的考虑中。这里只能自己编译(省略部分输出结果,用 ... 代替):使用
curl
命令进行本地请求:第一次请求时,可能需要拉取 go1 运行时的镜像并且创建容器,速度可能会慢点。
关于本地调试,详见 Local 命令。
部署
本地调试好后,可以将函数部署到线上了。
这里介绍使用
s
命令进行部署。因为涉及到本地和远端通信,要先用s config
命令配置密钥。使用
s deploy --use-local -y
命令部署(省略部分输出结果,用 ... 代替):部署后,输出了云函数的公网 url(system_url)和自定义域名 url(custom_domain);我们也可以使用
s info
命令查看云函数信息;也可以在阿里云控制台查看。本地调用云函数
因为这是一个 HTTP handler,我们可以用各种方式发起 HTTP 请求,但这里还是介绍下用
s invoke
命令进行远程调用。首先使用
s cli fc-event http
命令生成调用参数的模版:把模版修改成这样:
发起远程调用:
登录实例
可以在阿里云控制台登录实例,也可以在本地登录。如果现在没有实例,可以发起一个请求,让函数计算创建一个实例。
自定义运行时
使用运行时 custom.debian10 部署 gin 框架。
目录结构:
s.yaml:
vars
的使用,见变量赋值customRuntimeConfig.command
)和启动参数(customRuntimeConfig.args
),函数计算把启动命令和启动参数拼接成完整的启动命令。启动后的 HTTP Server 会接管来自函数计算的所有请求。函数配置中的监听端口(caPort
)和 HTTP Server 的监听端口必须一致。handler
的值无关紧要,不设置也可以main.go:
本地调试、部署、本地调用云函数、登录实例等同内置运行时。
本地调试,第一次请求时,可能需要拉取 custom.debian10 运行时的镜像并且创建容器,速度可能会慢点。
自定义容器运行时
TBD
参见