Skip to main content
Version: 3.2

openfunction

描述#

openfunction 插件用于将开源的分布式无服务器平台 CNCF OpenFunction 作为动态上游集成至 APISIX。

启用 openfunction 插件后,该插件会终止对已配置 URI 的请求,并代表客户端向 OpenFunction 的 function 发起一个新的请求,然后 openfunction 插件会将响应信息返回至客户端。

属性#

名称类型必选项默认值有效值描述
function_uristringOpenFunction function uri,例如 https://localhost:30858/default/function-sample
ssl_verifybooleantrue当设置为 true 时执行 SSL 验证。
authorizationobject访问 OpenFunction 的函数的授权凭证。
authorization.service_tokenstringOpenFunction service token,其格式为 xxx:xxx,支持函数入口的 basic auth 认证方式。
timeoutinteger3000 ms[100,...] msOpenFunction action 和 HTTP 调用超时时间,以毫秒为单位。
keepalivebooleantrue当设置为 true 时,保持连接的活动状态以便重复使用。
keepalive_timeoutinteger60000 ms[1000,...] ms当连接空闲时,保持该连接处于活动状态的时间,以毫秒为单位。
keepalive_poolinteger5[1,...]连接断开之前,可接收的最大请求数。
注意

timeout 字段规定了 OpenFunction function 的最大执行时间,以及 APISIX 中 HTTP 客户端的请求超时时间。

因为 OpenFunction function 调用可能会耗费很长时间来拉取容器镜像和启动容器,如果 timeout 字段的值设置太小,可能会导致大量请求失败。

前提条件#

在使用 openfunction 插件之前,你需要通过以下命令运行 OpenFunction。详情参考 OpenFunction 安装指南

请确保当前环境中已经安装对应版本的 Kubernetes 集群。

创建并推送函数#

你可以参考 OpenFunction 官方示例 创建函数。构建函数时,你需要使用以下命令为容器仓库生成一个密钥,才可以将函数容器镜像推送到容器仓库 ( 例如 Docker Hub 或 Quay.io)。

REGISTRY_SERVER=https://index.docker.io/v1/ REGISTRY_USER=<your_registry_user> REGISTRY_PASSWORD=<your_registry_password>
kubectl create secret docker-registry push-secret \
--docker-server=$REGISTRY_SERVER \
--docker-username=$REGISTRY_USER \
--docker-password=$REGISTRY_PASSWORD

启用插件#

你可以通过以下命令在指定路由中启用该插件:

curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/hello",
"plugins": {
"openfunction": {
"function_uri": "http://localhost:3233/default/function-sample/test",
"authorization": {
"service_token": "test:test"
}
}
}
}'

测试插件#

使用 curl 命令测试:

curl -i http://127.0.0.1:9080/hello -X POST -d'test'

正常返回结果:

hello, test!

配置路径转发#

OpenFunction 插件还支持 URL 路径转发,同时将请求代理到上游的 OpenFunction API 端点。基本请求路径的扩展(如路由 /hello/** 的部分)会被添加到插件配置中指定的 function_uri

重要

路由上配置的 uri 必须以 * 结尾,此功能才能正常工作。APISIX 路由是严格匹配的,* 表示此 URI 的任何子路径都将匹配到同一路由。

下面的示例配置了此功能:

curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/hello/*",
"plugins": {
"openfunction": {
"function_uri": "http://localhost:3233/default/function-sample",
"authorization": {
"service_token": "test:test"
}
}
}
}'

现在,对路径 hello/123 的任何请求都将调用 OpenFunction 插件设置的对应的函数,并转发添加的路径:

curl  http://127.0.0.1:9080/hello/123
Hello, 123!

禁用插件#

当你需要禁用 openfunction 插件时,可以通过以下命令删除相应的 JSON 配置,APISIX 将会自动重新加载相关配置,无需重启服务:

curl http://127.0.0.1:9180/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"methods": ["GET"],
"uri": "/hello",
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1980": 1
}
}
}'